Docker notes …

Basic commands:

docker image

docker image ls
(displays list of images available on this machine)
(or simply docker images instead of docker image ls)

docker image ls -a
(displays list of all images. I think it includes deleted images? intermediate images?)

docker rmi ==> will remove the image.

docker container

docker container ls
(displays all containers currently running)
(or simply docker ls)

docker container ls -a
(displays all containers including those stopped, exited)
(Question – can we restart a container?)
(Question – can we delete an image and still container be running?)

docker rm ==> will remove the container.

Image Vs Container

This link is good.
An image is an inert, immutable, file that’s essentially a snapshot of a container. Images are created with the build command, and they’ll produce a container when started with run. Images are stored in a Docker registry such as registry.hub.docker.com. Because they can become quite large, images are designed to be composed of layers of other images, allowing a miminal amount of data to be sent when transferring images over the network.

‘docker history’ is your friend

docker history command is your friend. It gives complete history of all the layers an image is built on including the size of each layer.

docker inspect is second best friend

docker inspect is awesome! It gives everything you need to know for a container.

Digging inside docker container

  1. docker attach (is one way)
    To detach try Ctrl + P / Ctrl + Q
    If it doesn’t work – try to kill the process which is attached. (pkill -9 -f 'docker.*attach')

2. docker container exec
e.g.. docker container exec 76192e17722e cd bin && ls


docker commit

This is another cool command. It makes a new docker image from running container. Take it to new machine and start running; or use it for debugging, Use the image to start container with additional options.

docker inspect

Looks like this is similar to docker history for image ;

Helpful tips for debugging container issues

  1. docker container exec <run any command like ps, top, ls>
  2. docker container exec -it <id> bash
  3. docker container exec -it -u 0 <id> bash
  4. docker container logs << Must run after you start a container>>
  5. docker container stats << view memory/CPU usage>>
  6. docker container top
  7. docker cp
  8. docker diff
  9. docker export (Gets all files as tar)

cleanup

  1. docker container prune ==> removes all stopped containers.
  2. docker image prune
  3. docker system df
  4. docker system prune (My favorite)
  5. docker rm $(docker ps -aq -f status=exited)
  6. This link is good.


Another doc link from docker to understand more about images vs containers.