Docker Commands!

Notes
April 10, 2018

BUILD

  • Build a docker image with the help of Dockerfile using – docker build -t image_name directory
    • -t tag is used tag the container image. It’s followed by a desired container image name. If we do not provide this tag, the image will not have any name.
    • directory is the directory whose contents are to be copied inside the container.

RUN

  • Create a new container using – docker run image_name
    • This command creates a new container to run the application  based on the image.
    • docker run -d image_name
      • -d tag runs the container in detached mode.
      • Usually the container exits as soon as it is used or as soon as we abruptly stop the container. But running a container in the detached mode runs the container which runs forever, until we explicitly stop it.
      • This command exits after returning the container ID.
    • docker run -d -p local_port:container_port image_name
      • -p tag is used to specify the port number. local_port will be used as a proxy for the the container_port in the local environment.  Meaning to say that – requests coming to port container_port(example – 80) inside the container will come to port local_port(example – 4000) in local/host environment.

PS

docker ps
  • Just like linux ps command, this docker command also prints out the list of all running containers.
  • docker ps -a
    • -a tag prints a list of all the containers, including exited containers.
  • docker ps -f category=value
    • -f tag is used to filter out containers. It is followed by a filter category and the category value. Look at the last command in below image to see the different types of category. Pretty zsh autocomplete😊!
  • docker ps -q
    • -q tag is used to print only container ids.

RM

  • Containers can be removed with the help of docker rm container_name
    • We can even use container ID instead of container name.
    • A container can only be deleted if it is not running currently.
  • docker rm container_1 container_2
    • We can even remove multiple containers all at once, by specifying container name/ID separated by spaces.
  • docker rm $(docker ps -a -q -f category=value)
    • We can even remove multiple containers at once by the above command.
    • $(docker ps -q -f category=value) – this command will pipe IDs of containers that satisfy the given filter.

RMI

  • Container base images can be removed/deleted with the help of – docker rmi image_name.
    • We can even use image ID instead of image name.
    • An image can only be deleted if –
      • It has no active containers running.
      • No other image is dependent on it.

 

⭐To check the total number of containers or images running or exited or whatever, use this –

  1. Check number of images – docker images | wc -l
  2. Check number of containers running – docker ps | wc -l
  3. Check the number of containers running for an image – docker ps | grep imagename | wc -l

Saviours – [1]Docker – Get Started[2]Docker Curriculum