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 dosomething 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 for5seconds 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, ifthe parent container exits. This child container will also automatically be exited.
Docker Flow
Listing all the docker images on the docker server
$ docker images
$ docker run -ti
View the running images (container) status
$ docker ps
$ 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
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 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
$ 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.
Archives
Categories
Recent Posts
Stopping and Removing Containers
29 May 2019Installing Amazon CLI and Terraform on Windows desktop
27 May 2019Convert DOS to UNIX and vice versa
25 May 2019Calendar