Tuesday, July 11, 2017

Oracle Linux - stopping a docker container

In our examples we run the Docker engine on Oracle Linux and in this specific example we run Oracle NoSQL in a four node cluster setup. All four nodes are running on one single Docker engine with an Oracle Linux base image. Running Docker containers is great and a lot of benefits can be found in building a strategy which involves the use of Docker as the foundation of your IT footprint. Even though we are happy we can see we have started our four nodes and they are all running happily and performing the task they need to perform at one point in time we might have the need to stop them. In a normal situation and a production situation you will use tooling to control what is running and what is not running. However, in some cases you might have the need to stop a container manually.

Stopping containers manually is done based upon the container id in combination with the docker cli. The first step is to identify the container you want to stop and get the container ID. The most easy way to do so is to use the "docker ps" command which will provide you a list of all containers active on your docker engine. An example of our Docker engine in combination with the ps command is shown below:

[root@localhost etc]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
c1db637d5612        oracle/nosql        "java -jar lib/kvstor"   10 hours ago        Up 10 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_3
06fc415798e3        oracle/nosql        "java -jar lib/kvstor"   10 hours ago        Up 10 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_2
bf2d698ebcb3        oracle/nosql        "java -jar lib/kvstor"   10 hours ago        Up 10 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_1
0a52831c65e8        oracle/nosql        "java -jar lib/kvstor"   10 hours ago        Up 10 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_0
[root@localhost etc]# 

In this example case we like to stop one of the Oracle NoSQL nodes, to be more precise we want to stop nosql_node_2 using the Docker command line interface. For this we can use the "stop" command as shown in the example below.

[root@localhost etc]# docker stop 06fc415798e3
06fc415798e3
[root@localhost etc]#

If we now check the running containers  again we will see that the container with ID 06fc415798e3 is no longer active.

[root@localhost etc]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
c1db637d5612        oracle/nosql        "java -jar lib/kvstor"   11 hours ago        Up 11 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_3
bf2d698ebcb3        oracle/nosql        "java -jar lib/kvstor"   11 hours ago        Up 11 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_1
0a52831c65e8        oracle/nosql        "java -jar lib/kvstor"   11 hours ago        Up 11 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_0
[root@localhost etc]#

You can use the stop command on one single container, as shown in the example above, you can also use the stop command for multiple containers at once by providing multiple container ID's. This will make sure that you do not need to execute a single command per container and this can make your life more easy when scripting a solution to stop multiple containers at once. In the below example we also stop node 3 and node 1 of our Oracle NoSQL cluster with a single command;

[root@localhost etc]# docker stop bf2d698ebcb3 c1db637d5612
bf2d698ebcb3
c1db637d5612
[root@localhost etc]#

If we now check the running containers we will notice that only node 0 of the Oracle NoSQL cluster is active as a Docker container and the other containers have stopped.

[root@localhost etc]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
0a52831c65e8        oracle/nosql        "java -jar lib/kvstor"   11 hours ago        Up 11 hours         5000-5001/tcp, 5010-5020/tcp   nosql_node_0
[root@localhost etc]#

As stated, in a normal and more production like state where you use Docker you will most likely not use the docker CLI in a manually manner, you will use tooling around Docker for stopping, starting and managing your containers. Having stated that, knowing how to do things manually is something that is important and should be known by everyone. When resolving issues it is vital to understand how to do things manually. 

No comments: