Docker Series Part 2

Mrigank Singh
CodeX
Published in
4 min readApr 23, 2022

Docker Series Part 1

Docker Commands

Docker run command is used to create and run a container from an image.

  • docker run image-name

A slight variation to the command is

  • docker run image-name override-command

Note: The overridden command should be for the file system of the image otherwise, it will show an error. We cannot run ls or Linux commands on the hello-world image as it is just a program written in a specific programming language.

Docker run command is a combination of 2 commands docker create and docker start.

  • docker create image-name → This will create a container of the image and return the container Id.
  • docker start containerId → This will start the container with the given container Id but will not show any output generated by the container on the screen.

Let’s understand the docker create and start with this image better:

Container Creation
Container Start
  • docker start -a containerId → This will start the container with the given container Id and print the output from running the image in the container.
  • docker ps → This command lists all the currently running containers
  • docker ps - -all → This command lists all the runner containers up to date on the system.

Note → The most important part in the output of the ps command is the container Id, as often we like to work with a single container.

The containers that are exited can be started again. These containers are not deleted they are still there; what is exited is the running processes inside these containers. These containers can be restarted using the docker start command with the container Id.

Note: When we restart these containers, the commands with which these containers were created in the first place cannot be overridden.

  • docker system prune → The exited containers also take disk space, so it is best to clear them when they are of no use. This command will also remove all the containers and the locally built cache of images.
  • docker logs containerId → This command will print all the logs of the particular container after starting it.

Two commands are used to stop a container

  • docker stop containerId → This command stops the docker container by giving it a grace period of 10 seconds in which it can do some cleanup, save up some files and then stop itself, but if the container doesn’t stop in 10 seconds, then a docker kill command is automatically executed. This command sends a SIGTERM message to the container process.
  • docker kill containerId → This command sends a SIGKILL message to the container process. This command stops the docker container immediately.

Sometimes we want to issue multiple commands to a container. While working on the Redis container, we might want to open a Redis-server and Redis-CLI; then, we will have to issue multiple commands to the running Redis container.

  • docker exec -it containerId command → This command helps us run multiple commands inside a container.
Redis-Server
Redis-CLI

We used the -it flag in the above docker exec command; let’s understand its use. Every process in the Linux environment has three channels STDIN to take standard input, STDOUT, and STDERR to show the expected output on the screen. The -it flag is a combination of two flags -i and -t. The -i flag used for STDIN and -t in crux shows the coming text in a more formatted manner.

The docker exec is a strong command, and one more common and powerful use of it is to open the terminal or shell of the container.

  • docker exec -it containerId sh

Another command that is used to have the same effect is:

  • docker run -it image-name sh

--

--