01 Main Container Docker commands


Federico Sartoris

Just to start the Docker series, I want to resume the main commands used to start working with Docker containers. Docker has changed the way to invoke the commands some time ago, but there is no matter, because the backward compatibility was work too.

Docker version returns the version of your client (CLI) and the server (ENGINE). The server is running in the background on the machine. Ideally that the Client and Server should be the same, but they dont have to be as mandatory.

docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
OS/Arch: linux/amd64
Experimental: falseServer: Docker Engine – Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
OS/Arch: linux/amd64
Experimental: false

Docker info command returns the configuration values for the server (ENGINE). You can check the number of containers (running, paused or stopped), images stored, etc.

docker info
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 45
Server Version: 18.09.0
Logging Driver: json-file
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: c4446665cb9c30056f4998ed953e6d4ff22c7c39
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: fec3683
Kernel Version: 4.4.0-130-generic

Going into docker container commands, you can run the container using an image just in one line. If the image is not locally, this command go to Docker Hub and download & install into the container the latest image for Nginx.

docker container run –publish 80:80 nginx
Unable to find image ‘nginx:latest’ locally
latest: Pulling from library/nginx
177e7ef0df69: Pull complete
ea57c53235df: Pull complete
bbdb1fbd4a86: Pull complete
Digest: sha256:b543f6d0983fbc25b9874e22f4fe257a567111da96fd1d8f1b44315f1236398c
Status: Downloaded newer image for nginx:latest

To understand what is happening using this command, we can define step by step all the actions:

  1. Download the latest image ‘nginx’ from Docker Hub. If exists locally, this step should be disregarded.
  2. Start the new container from the image defined.
  3. Open port 80 on the host IP (localhost)
  4. Routes the traffic to the container IP to port 80. The left part is the host port and the right the container port. If the port is not available in your host, maybe changing the left side with another which is available to route the trafic. (HOST:CONTAINER format)

To run the container in the background, you can use the same command but adding “detach”. You get back the unique container ID as result.

docker container run –publish 80:80 –detach nginx
edd2221c8cfade9caa73ad6348c4d3be805f033f04f294208ea5adefefa67438

To list all the containers in the server you have to use “ls”. You will find the container ID created using detach command. And check the image name which is used, the publish and port redirect, etc.

sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edd2221c8cfa nginx “nginx -g ‘daemon of…” About a minute ago Up About a minute 0.0.0.0:80->80/tcp determined_bardeen

Above command just show the running containers. If you want to see all the containers in the server you have to add “-a” as argument.

To stop the containers the command is really simple but before you have to know what is the container ID. Just a trick, you can use a part of the container ID (if it is unique in your list) and the run the stop command. As result, you can the the same ID defined to stop.

docker container stop edd2221c8cfa
edd2221c8cfa

How can I start again the container stopped? There is a difference to run and start. As we can see in the last command, run command can setup and start a new container. But it can only works using new containers, if you are looking to start a container that exists, you have to use start. First of all, use ls -a to list all and the execute using the container ID.

docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edd2221c8cfa nginx “nginx -g ‘daemon of…” 11 minutes ago Exited (0) 7 minutes ago
determined_bardeen#to start again the container
docker container start edd2221c8cfa

If you are asking about the NAME, this should be unique but it is not mandatory to define when we create the container. For this cases, Docker will setup a random name. To setup a name you can run.

docker container run –publish 80:80 –detach –name webhost nginx
adbb611e8d28cc092c3e89b0ab85033ad4916283789e472d28fc19bd9eb28482docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adbb611e8d28 nginx “nginx -g ‘daemon of…” About a minute ago Up About a minute 0.0.0.0:81->80/tcp webhost

To get the logs when the container is running on detach mode, you can execute the log command using the name. Using the container name defined in the last command, we can check like that:

docker container logs webhost
172.17.0.1 – – [14/Jan/2019:17:35:52 +0000] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36” “-“
172.17.0.1 – – [14/Jan/2019:17:35:53 +0000] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36” “-“

Exist another command to validate and check the logs into the container. There is a way to check the process that are running into the container and what is their status.

docker container top webhost
UID PID PPID C STIME TTY TIME CMD
root 8834 8816 0 14:30 ? 00:00:00 nginx: master process nginx -g daemon off;
syslog 8876 8834 0 14:30 ? 00:00:00 nginx: worker process

Finally you can delete the containers using rm argument. As precondition, the container to delete should be stopped, if not docker alerts about that and the only way to delete running is using the force argument. This time, you can include all the containers ID separated each one to execute in one line.

docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e82b203d9ddd nginx “nginx -g ‘daemon of…” 13 minutes ago Created host
83575793bc16 nginx “nginx -g ‘daemon of…” 38 minutes ago Exited (0) 29 minutes ago recursing_sammetdocker rm e82b 8357

Below some other commands that you can use to validate configuration, performance and monitoring all containers.

#Validate configuration file for the container (JSON format)
docker container inspect webhost#Use format to display only looking part
#NetworkSettings is a part of a node in JSON inspect returns
docker container inspect –format ‘{{ .NetworkSettings }}’ webhost
{{ 1e0c009c50e14a67a2f85ee4cc85d2a94fce2e337d6ea48699993641ea2c9e7a false 0 map[80/tcp:[{0.0.0.0 81}]] /var/run/docker/netns/1e0c009c50e1 [] []} {1c69cbe998bf1389a0ea6e4310272810eaefff16652fc65055e82e5a410c0308 172.17.0.1 0 172.17.0.2 16 02:42:ac:11:00:02} map[bridge:0xc4205be000]}#Validate the routing port traffic from container to host
docker container port webhost#Show live performance and CPU usage
docker container stats

Using the Docker CLI commands, you can access into the container like as when you logged into server using SSH. As precondition, the container should be up and running before.

docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2042334bb0af nginx “nginx -g ‘daemon of…” 39 minutes ago Up 2 minutes 0.0.0.0:81->80/tcp webhost
6abb0392706b postgres#run additional command in the existing container, like bash
docker container exec -it webhost bash
root@2042334bb0af:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

The root can be identified with the Container ID after @. Into the server, you can run and work all the commands supported by the image. For example, you can use the package manager and invoke function on demand.

To create and manage the network in Docker, you have to know some commands that will help to you in the configuration process.

#show networks
docker network ls
NETWORK ID NAME DRIVER SCOPE
071f63710e9b bridge bridge local
ed203e35c150 host host local
0e8b6a371648 none null local#inspect networks
docker network inspect bridge#create a new network
docker network create my_private_net
10c1190519cae53df015bcffbdef58b25fb4b79c24dfa08f941500ffe990ee8d#attach a new network to container
docker network connect#disconnect a network
docker network disconnect

The NAME can be bridge or docker0 depending of the version installed in your OS.

I hope that information provide to a brief introduction for some basic docker container commands. Enjoy code!

Leave a Comment

Your email address will not be published. Required fields are marked *