Mega Menu

Wednesday, January 08, 2020

Docker - Docker Basic Commands - Cheat sheet

 

Can use Windows Powershell to execute these on a Windows machine. Also, make sure Docker is running and you are already logged with docker-id and password.

Official Reference -

         https://docs.docker.com/engine/reference/commandline/
 

docker run {imageName}

    Downloads image if not already exists, creates and starts the container and executes the startup command. Example imageName 'hello-world' or 'busybox' or 'redis' etc.

docker run {imageName} echo hi

    overrides the startup command with "echo hi" along with all usual run activities.    

docker pull {imageName}
       just to download the image and not to run it.

docker create {imageName}

    creates a container and prints container id

docker start -a {containerId} echo hello

    starts the container with override command.

docker ps

    lists all running containers

docker ps --all

    lists all containers running and shutdown

docker images

        lists all images in the machine

docker system prune

    deletes all unused/dangling containers, volumes and images to the local machine   

docker system prune -a

    deletes any stopped, unused/dangling containers, volumes and images to the local machine   

docker logs {containerId}

     prints the starup log of the container

docker stop {containerId}

     Issues a termination signal, allowing the processes to save data or complete any pending process before shutting down. If the shutdown does not happen in 10s, docker issues a kill command to instantly shutdown

docker rm {imageName}
    To remove one or more containers.

docker rmi {imageName}
    To remove and untag one or more containers.

docker kill {containerId}

     Issues a kill signal forcing the container to shutdown instantly

docker exec -it {containerId}

     Allows execution of commands within a running container, where is the id of the running container and is the script/command to be executed
       i  --> indicates the input
       t  --> indicates the formatted output
       can be 'ls' or 'echo hello' or any other linux command

docker exec -it {containerId} sh

  Allows to open shell prompt of the running container

docker run -it {imageName}

       Allows to run an image in an interactive mode such that input arguments can be keyed in if prompted by the image (eg: password)     

docker run -8000:5000 {imageName}

      Maps port 8000 of localhost to port 5000 of the docker image. This image can now be accessible through

                http://localhost:8000/       or     http://<docker_ip>:5000/

docker run -it {imageName} sh

  Allows to open empty shell prompt during start-up of the container
     Eg: docker run -it redis sh -->(opens an empty shell prompt on redis container)
      #redis-server   -->(starts redis server) 

docker run -v /opt/data:/var/lib/mysql {imageName}
    creates a mount point on the localhost for the data stored by the application in the docker image. This will help retain the data if the container is deleted.

docker run {imageName}:{tag} cat /etc/*release*
         eg: docker run ubuntu:17.10 cat /etc/*release*
 
docker run ubuntu sleep 300
           The container will be sleeping till 300 seconds and automatically exits.
   
docker run -d ubuntu sleep 300
            Running the container in detatched mode such that the sleep runs as a backend process.

docker attach {containerId}
        Can be used to attach it back to the process
            Eg:
                docker run timer
                docker run -d timer
                docker attach {containerId of timer image}   

 docker commit
      Allows to create an image out of an running container. 
         syntax: docker commit -c {command to execute on startup} {containerId}
             eg: docker commit -c 'CMD["redis-server"]' 03edr4344

docker inspect {containerId} or {imageName}
    displays the detailed state of the container or image.

docker logs {containerId} or {imageName}
    To print logs of a container.




No comments:

Post a Comment