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
docker create -it --name test-nginx -p 8080:80 nginx
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>
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...]
docker run -d -p 8080:80 test_image nginx -g 'daemon off;'
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.
$ 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>.
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>
docker logs --since 4m <container id | container name>
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>
docker rm <container1 name | id> <container2 name | id> <container2 name | id> ...
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
docker system prune --volumes
docker system prune --filter "componet=<microservice name>"
// here "component" is the label tagged to the resource and value is the service name.
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.