Docker Basics: Interacting with Containers using Docker Client

cem akpolat
5 min readMar 28, 2021

In the previous tutorial, I introduced simply the structure of the docker and its components that are built on top of the namespaces and controller. This tutorial shows us mostly the practical usage of the docker client, namely, docker command.

Takeaways:

docker run hello-world -> "hello-world"
docker run busybox echo “hello world” -> "hello-world"
docker run busyboxy ls -> "bin, dev,etc,proc, root,..."
docker ps -> list the running containers
docker ps — all -> all created container that are running or run once a while will be listed
docker create busybox echo hi there -> run "echo" command on the busybox imagedocker start <ID> -> returns only the container iddocker logs <ID> -> returns the console outputs of the contaier
docker system prune -> remove all container on the system
docker start -a <ID> -> starts the container and returns the container outputs
docker exec -it <container id > <command> -> starts the container along with the command and enables to interact on the container
docker run -it busybox sh -> jumps in the busybox terminal

Explaining step by step the above listed comments

A simple way of running the container and the command in that container can be performed via the following commands. The first command is a reference to the docker client software, run describes the intention, image name is the image to be converted into the container and finally command is the command to be executed inside this instantiated container if it is available.

docker run <image name>
docker run <image name> <command>

Some examples that satisfy the aforementioned structure:

docker run hello-world -> "hello-world"
docker run busybox echo “hello world” -> "hello-world"
docker run busyboxy ls -> "bin, dev,etc,proc, root,..."

In the first command, hello-world image is started and returns only “hello-world” message, whereas busybox image includes lots of Linux files and folders and echo, ls and many other commands are defined. Each image can be seen as a template and can be customized according to the requirement. Whenever the image is called with the docker start or run command, the container is placed on the disk and the executed command ls interacts with it. This container has access to the platform resources such as RAM, Network and CPU, and the communication with the running processes…

If our intention is to list the running containers, the following command would suffice for this:

docker ps

Assuming that some containers are still running, and they can be listed with the above command. The above given container examples run only a short amount of time due to their execution ways. For this reason, we will not be able to see a running list of containers if we call docker ps . The easiest way is either run i.e. busybox with a long-term process such as passing ping command as below:

docker run busybox ping www.medium.com

Now, we should be able to see the running container with ps command.

If the intention is to list all created containers without considering its status:

docker ps -all

What is the difference between docker run/start/create?

docker run is actually equivalent to the composition of these two commands.

run = create+start

When calling docker create hello-world , a container is created and an ID for the docker is generated. We start this container with this id with docker start -a <ID> . The file system is prepared and ready and then we started the primary startup command in there with docker start. The -a parameter enables us to attach to the docker container and receive the console outputs from it.

Restarting and Stopping Containers

The previously created containers can be again with their ID restarted and stopped. The important point for restarting it, the startup command of the container i.e. docker run busybox echo hi cannot be changed. Stopping a container can be performed via two approaches: docker stop <ID> and docker kill <ID> . The first one waits for closing all operations inside the container for a while, whereas the latter one stops directly. Stop command sends indeed a SIGTERM mesage, terminate signal to stop a procss and shut the running conainer down and do the rest operations such as save file, cleanup, etc. Unlike stop, kill approach shut the container down directly without a delay. A stop command can fall back to the kill command, if the process cannot be stopped. The recommended approach is the docker stop <ID> .

Removing all containers

docker system prune remove all stopped containers, all networks not used by at least one container, all dangling images and all build cache.

Receiving logs from the container

Especially tracking the issues in the container can be quite difficult and log files can facilitate this issue for a running container:

docker logs <ID>

With the above command, we would be able to see all terminal outputs.

How to interact efficiently with Containers?

Until now we focused on the execution of the single commands passed from the host terminal to the container and receive the console outputs. If our intention is directly to interact with the container or the service running in the container on the terminal, we need another docker command, namely, docker exec.

docker exec -it <container-ID> <command>

Again here the docker is a reference to the docker client, exec command opens the way of jumping in the container along with it parameter, the container id is the container with which we want to interact and final one is the command to be executed additionally if required.

A server-client interaction can be used in such a scenario. For example, redis-server and redis-client are the good candidates to show this.

docker run redis-server
docker exec -it <ID> redis-cli

By doing so, we can directly run the redis-cli inside the docker-container and it allows us to interact with this software in the container terminal.

You may ask what is the purpose of the itparameter. While i allows us to access to the terminal of the docker container, t parameter provides a nicely formatted text.

We can increase the number of examples, even jump in the container terminal and use it like Linux terminal. The special command for this is the shell(sh) as shown below:

docker exec -it <container-ID> sh

If the image includes other shell environments such as bash,zsh,etc., then it makes sense to give the right command, of course. We have also another way to realize that:

docker run -it busybox sh

In this tutorial, we covered the basic docker commands that are mostly used. We will continue by extending the docker and its usage as much as possible. Even though the vast majority of the developers focuses on the docker containers, there are also good alternatives.

--

--