Hi, I started learning docker from the very basics here, today I will be playing with some commands in docker-cli. Sharing here how those command works and what they do as I get to learn them in the course.
docker run image
Docker run is the most basic command, which creates a container from the given image and runs it.
docker run hello-world
- It will print some predefined test on terminal.
docker run redis
- It will start a container with redis service.
docker run ubuntu ls
- It will start a container with ubuntu os, and print out the list of files in current working directory.
- The current working directory in this case is not the one where you run the command, but It is root directory of virtualized ubuntu filesystem created using the container.
- Overriding of startup command varies from image to image, i.e. you can not just pass
docker run hello-world ls
docker run -it ubuntu bash
- This will open up a pseudo ubuntu bash terminal from within the running image.
- There are multiple options you can provide to docker run, here is the full list.
you can create your own image and run it too, will see this going ahead.
docker ps
docker ps is used to show the details of all running containers inside the machine.
It will show
- container id -> a unique id given to a container
- image -> image from which the container is created
- command -> the startup command given while doing the docker run
- ports -> the ports which are exposed from the running service, e.g. 5432 for postgres, 6379 for redis etc.
- some others...
This command is useful to get the ID of a running container that is needed to run other commands.
To show all ( running + not running ) containers, you will have to run it with -a
/ --all
option.
You can find all available options for docker ps over here.
docker create & start
docker run hello-world
is combination of two commands, docker create & docker start, it creates a container then starts that container.
You must have observed if you have tried docker-run multiple times on hello-world image and then look at
docker ps
output, it lists multiple containers from that image. It is because docker run creates new container on each run.docker create hello-world
creates a new container with the given image and prints the newly created container ID on terminal.docker start <ID>
will start the container with the given ID.- on running this command, you won't see anything on terminal.
- you will need to attach STDIN, STDOUT, STDERR of the running container with current terminal, that can be done by
docker start -a <ID>
. - If you need to interact with terminal, i.e. run
ls
in case of ubuntu image, you will have to rundocker start -a -i <ID>
.
There are a lot of options available for these commands which you can check at here.
docker system prune
To free-up space or to reduce clutter ( containers created by multiple docker run ), we use prune to perform cleanup.
This command removes
- stopped containers
- networks ( don't know yet!! )
- images ( dangling - not associated/tagged with any container )
- local cache ( local download of image )
If provided with -a
option, this command will remove
- all unused images.
There is difference between dangling images and unused images.
Dangling images are those which are old layers of an image build. We have not learned how to build an image or what it is till now so will not know that in deeper, but it is kind of like build/dist/* files of local development versions where we don't tag the version, we only tag it once it is deployable. So the dangling images are the ones that are not tagged and serve no purpose, you cannot use them.
Unused images are those which are not referenced in any container ( running or exited ). Like, you have pulled the hello-world image but have not created any container on it.
This article on digitalocean gives some nice tips around how to use prune.
docker logs
Debugging!!!
To see logs inside a running container or an exited container, you run docker logs <ID>
and it will print all the log outputs from that container.
--follow
will show you logs as when they arrive on STDOUT and STDERR till the container is running, you can break it by pressing ctrl+c.--tail 10
will show last 10 lines from the log.
Logging in itself is a big task to get it done right, this article explains some practices regarding how one should go for it.
docker stop / kill
To stop a non-interactive (for interactive I'm very much happy in doing ctrl+c ๐) running container you can do docker stop <ID>
or docker kill <ID>
.
- docker stop will first try to emit SIGTERM to the running process within that container for 10 seconds (called grace period ) and then if the container doesn't stop it will issue SIGKILL signal to the process asking to finish immediately.
- docker kill will directly issue SIGKILL to the running process.
By issuing SIGTERM command, the language or system environment allows the process to do some cleanup task (e.g. saving a modified file), which SIGKILL doesn't allow.
docker exec
The docker exec
command runs a new command in a running container.
The second command will be needed in more or less every real-life case, like you started a redis-server and want to go to redis-cli, you started postgresql and want to do psql.
docker exec -it <ID> <COMMAND>
- most of the times you will need to run it with
-i
( interactive - attach the STDIN of container with current terminal ) &-t
( pseudo-tty - for better formatting ) options, unless you are running second command in background. - COMMAND is the command which you want to run ๐
- e.g.
docker exec -it redis-container redis-cli
Ordocker exec -it ubuntu-container bash
By learning this I just got exposed to how exactly commands which we type in a terminal works, by command processors. bash, sh, zsh are examples of them and now I know the difference between zsh and oh-my-zsh.
This is all for today, there are lots of command in docker to learn, but I will pause here for today and will learn something new as the course go ahead.
Let me know If I misunderstood anything, will be happy to correct my understandings, or just wanna have some chat ๐.