Introduction to Docker
After installing Docker, one can check its version by the following command:
The output of the above command would be somethign like below:
Hello World
We would execute the following command now:
For the above command, docker takes the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the "hello-world" image from the Docker Hub. (arm64v8)
The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
Docker CLI
docker run
The first command we would look in details is the docker run
command. The below figure provides details about this command:
The docker run
command is used for creating and running a container from an image.
Overriding Default Commands
To override the default startup command, the following figure shows how to do it:
For example, following command uses a override command with docker run
:
Output would be:
Another example is listing out the files and directories within a container:
Output of the above command would be:
Now, let's try the echo
and ls
override commands with the hello-world
image with docker run
.
As we can observe, we get an error for both the commands. This is because inside the hello-world
image file-system, the ls
and echo
programs do not exist. The only thing that exists inside the hello-world
image snaphot file-system is a single program. All this program does is print out the singular message: Hello from Docker!
.
The same commands work with the busybox
image because these programs exist inside the busybox
file-system image.
Listing Running Containers
Next, we will look at a command to list all currently running containers on a local machine.
First of all we will run a container for a meaningful amount of time, like below:
Next, we would list the running containers:
As can be observed from the output of the command, it prints out a bunch of information about the running container.
We can modify the docker ps
command to list all containers created on a local machine like the following:
Executing the above command will give the output something like below:
Container Lifecycle
The docker run
command is actually equivalent to running 2 seperate commands as detailed below:
The docker create
command creates a container while the docker start
command starts a container:
For example, the below command creates a new container using the hello-world
image:
As can be observed, the docker create
command outputs the id of the container that was created.
Next, we can execute the hello world
command inside of this container using docker start
, like below:
Like running the docker run
command, the above command will output the following:
Running docker start
without the -a
arguement does the following:
As can be observed above, without the -a
argement, the command returns back the id of the container. The -a
arguement will make docker actually watch for output from the container and print it out on the terminal.
Restarting Stopped Containers
An exited container can be started back again. For example, first we list all the containers created on a machine like below:
Then, let's say we pick the seventh entry (second last) from the list. Using the container id, we can issue the following command to start the container again.
One thing to note is that we cannot modify the start command for a container which has been already created. For example,
As can be observed, docker misinterprets the command and throws an error.
Removing Stopped Containers
To remove all containers, following is the command:
As can be observed above, before removing all the stopped containers, docker outputs a warning and requests a confirmation from the user to actually remove the stopped containers.
Retrieving Log Outputs
Another way of getting the output from a container without using the -a
flag with docker start
is using the docker logs
command. Specifically, below is the command to get logs from a container.
Following is the series of command that shows usage of docker logs
command:
Stopping Containers
There are 2 ways of stopping a running container:
Both the above commands are going to stop a running container. While both these commands kind of do the same thing, internally there is a difference between them.
When docker stop
command is issued, a hardware signal SIGTERM is sent to the primary process inside the concerned container. This message tells the container to shutdown on its own time.
On the other hand, the docker kill
command issues a SIGKILL signal to the primary process inside the concerned container. SIGKILL essentially means the conatiner has to shutdown right away without doing any additional work.
Ideally, docker stop
should be used to stop a container. If a container is non-responsive, we can use docker kill
command instead.
One thing to note about the docker stop
command is that, when this command is executed and the concerned container does not stop automatically in 10 seconds, then docker will automatically fall back to executing docker kill
command.
Following is a series of commands which creates and start a busybox container. Then it outputs the logs from the container. Finally, we stop the running container.
Multi-Command Containers
As an illustration, let's start running a new container with redis-server
running inside it. Redis is an in memory data structure store. Following is the command to do so:
Executing Commands in Running Containers
After starting the container using the redis
image, we now need to somehow start-up the the redis-cli
also inside the container. For this, we would use the docker exec
command:
Following is how to run redis-cli
in the started container:
Command Prompt in a Container
There is another usecase for the docker exec
command which is more commonly used. This usecase is getting shell or terminal access to a running container. In other words, this would be mean running shell commands inside of a container without running docker exec
repeatedly.
Following is the command for this:
sh
is a command processor which allows to type commands in and have them be executed inside of that container. There are other command processors as listed below:
Starting a Shell
We can also start a shell via the docker run
command like below:
One disadvantage of starting the shell via the docker run
command is that it does not allow to run the container's default process when starting up. In practice, generally we start the container and then use the docker exec
command to start a shell within a container.
Last updated