Docker Flow

24 May 2019 Bajrang Gupta Comments Off
The Docker Flow
An image is every file that makes up, just enough, operating system that is required for everything that you need to do.

Listing all the docker images on the docker server

$ docker images
Repository is the place where the image comes from
Tag is the version of the image
Image ID is the reference ID for the internal representation of the image
docker run command converts an image into a living and running container with a process in it that is doing what your application needs to do.
$ docker run -ti
-t – Terminal, -i – Interactive

View the running images (container) status

$ docker ps
You can format the output of many docker command by using the –format option
$ export FORMAT=\\nID\\t{{.ID}}\\nIMAGE\\t{{.Image}}\\nCOMMAND\\t{{.Command}}\\nCREATED\\t{{.RunningFor}}\\nSTATUS\\t{{.Status}}\\nPORTS\\t{{.Ports}}\\nNAMES\\t{{.Names}}\\n


$ docker ps --format=$FORMAT
Any changes to a running container is contained within and do not impact the image.
Any changes done to a container are not lost when a container is stopped. By default, docker ps does not show the stopped container.

View stopped container

$ docker ps -a (All stopped containers). The stopped container still exists and is not deleted.
$ docker ps -l (Last container to exit)
docker run command converts an image into container. Similarly, you can convert a stopped container into an image. This is useful when you have a running container and you make some changes to the container. If you would like to distribute this as an image – docker commit  is the command to use.
$ docker commit container ID is not the same as image id. This command creates the image but it does not have a name. We name the image with the tag command.
$ docker tag
Above two commands can be clubbed into one:
$ docker commit

Remove stopped container

$ docker container rm

Remove an existing image

$ docker rmi

Running an image

# Spawn a container that will do something and exit as soon as the container is completed. It is accomplished by --rm option
$ docker run --rm -ti sleep 5
# This will run the image, sleep for 5 seconds and exit the container. The container will get deleted as it finishes it's work (sleeping :))!
#Exit container by pressing Ctrl + D or typing exit
# Spawn a container that will run in detached mode
docker run -d -ti  bash


# Attaching to the container after running the container in detached mode
docker attach


# Detaching from a container after attaching to it cannot be done with exit. Use a special key sequence Ctrl + P and Ctrl + Q to detach from the container.
# Executing another process within the running container. Similar to attach
docker exec -ti bash


# After executing another process, if the parent container exits. This child container will also automatically be exited.