Docker Commands For Beginners

Published On: 2019/10/12

This post is a continuation What is Docker tutorial and this article we will be looking into a few of the docker commands which help you run, manage and deallocate containers.

Create

The docker create command creates a writable container layer on top of the image and prepare it to run the specified command in the image. When you want to create a container trigger the docker create command with all required resource configuration in order to equip it with necessary resources when starting the container. The create command won’t start the container, just prepare it with required resources necessary to run.

The below docker create instruction will create a container with nginx image in dockerhub

docker create -it nginx
The problem with the above instruction is that, it creates the container without specifying the port which is exposed to the external world. As the port is not exposed, users cannot access nginx server eventhough it is running in the docker container. To resolve the issue with service port mapping, create the container using the below command where we have mentioned the port mapping.
docker create -it --name test-nginx -p 8080:80 nginx
When a user access the server on the port 8080 the request will be routed to the port 80 in the docker. The command line parameters -it (-i -t) are to be provided if in case you need to see the console log of nginx service.

Start

When you start a container, the command provided in the image will be triggered by the container on startup. This command is normally used with the command line parameter -a to get the console logs. If you need to start the container in interactive mode then adding the parameter -i to the start command will do the purpose.

 docker start -a -i <container id>
How to get the container id? Execute the below command to get the information of all the containers created.
docker ps -a 

Run

The docker run command is a combination of “docker create” and “docker start” commands. When we execute docker run it first creates the docker container with the specified image and then trigger the command “start”. What is the advantage in using docker run over docker start?. With the docker run [OPTIONS] an operator can add to or override the image defaults set by a developer. Additionally, operators can override nearly all the defaults set by the Docker runtime.

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
We can run the docker container in (1) Detached mode (2) Foreground mode. When we run the container in detached mode, the container will exit when the root process used to run the container exits. It means that the container run for nginx server will exit when the nginx server process exits.
docker run -d -p 8080:80 test_image nginx -g 'daemon off;'
If the docker container started running in the detached mode, we need to use network connections or shared volume for input/output operations to the container.

In foreground mode (the default when -d is not specified), docker run can start the process in the container and attach the console to the process’s standard input, output, and standard error.

For interactive process which is running in foreground, we should use -i and -t paramerter (-it) in order to allocate tty for the container process. If the process gets the input from some other sources then -t should not be used. In the below example the “busybox cat” root process gets the input “test” from the pipe operation.
$ echo test | docker run -i busybox cat

Exec

The docker exec command runs a new command in a running container. Note that the new command will be executed if the primary process, PID 1, is still running and another point is that the commands which are executed with “docker exec” will not be reexecuted when the container is restarted.If the container is paused then the “docker exec” command will fail to excute the new command.

Stop

Think that you are in a BBQ party arranged in a nearby park. After 10 PM police came and asked you to leave the site within 15 minutes or be faced with a penality. The docker stop command is just simlar to this. It will issue a SIGTERM signal and after a while issues SIGKILL signal.

docker stop <container name>.
If you specify the parameter rm then it will remove the container.
docker stop rm <container name>

Kill

When someone in a dance club makes too much nuisance, then the security guards come and immediatly throw him/her out of the club. Just like that the kill command won’t give a grace period but just kill the main process running in the container and then bring the container down.

Logs

This command will fetch the logs of a specific container. If your container is started on detached mode and you want to see the logs then use the docker logs command to fetch the logs.

docker logs <container id | container name>
The below command will fetch the logs since 4 minutes.
docker logs --since 4m <container id | container name>
The below command will fetch the logs until 4 minutes.
docker logs --until 4m <container id | container name>

RM

This command is used to remove one or more containers.

To remove a single container execute the command

docker rm <container name | id>
To remove multiple containers execute the command
docker rm <container1 name | id> <container2 name | id> <container2 name | id> ...
If we provide the flag -v in this command then the volume attached to the given container will be removed.
docker rm -v <container name | id>

Prune

If you want to clean the container system by deleting all unused resources like images, network, containers, volumes then issue the command:

docker system prune --all
By default the volumes are not removed to prevent the data loss. If you need to prune the volumes along with other resources then use the command:
docker system prune --volumes
Now , let us check how to prune resources which meets certain criteria. In order to do this, use the –filter flag. Let us take a scenario where we are running cloud with multiple microservices for various project. Now you need to decommision a service which has already been replaced by a new service. You will first bring that service down and then issues the below command to release all resources allocated for the service.
docker system prune --filter "componet=<microservice name>"
// here "component" is the label tagged to the resource and value is the service name.
The currently supported filters are:

  • until () - only remove containers, images, and networks created before given timestamp

  • label (label=, label==, label!=, or label!==) - only remove containers, images, networks, and volumes with (or without, in case label!=… is used) the specified labels.

Conclusion

In this article we have covered few of the frequently used docker commands. Please refer the link to know more about other docker commands.

comments powered by Disqus