Running Docker Containers

Running Docker Containers

In post “What Docker is?” we spoke about how Docker works and why it’s needed. So now it’s time to get our hands dirty and spin up some containers. So you would see how simple it is to use Docker.

Installing Docker

As expected to work with Docker you need to install it.

Registration on Docker Hub is required to download Docker Client. But installation is pretty straight forward after you do. So if you installed your Docker successfully: “docker -v” – typed in a terminal will show your Docker version.

codespace:~ lab$ docker -v
Docker version 19.03.1, build 74b1e89

Find The Image

Once we have our Docker setup next step is to find or build an image of the application we want to run in a container. In this example we will be using already built image of Elasticsearch from Docker Hub registry.

After an image is found on Docker Hub it is always a good idea to review the details on pages provided. In this case, Elasticsearch image has Description, Reviews, Tags tabs.

  • Description tab – usually has details about the image, examples on how to start basic image or build docker-compose file.
  • Reviews tab – is helpful when you need to make a decision on which version of the image to use or to check what problems other people are having with this image.
  • Tags tab – is the one where all the versions of the image are listed out.

Starting Container

Finally, we reached the step where we are going to start Docker container. So, as described in “Running in Development Mode” section on Elasticsearch image description we setup Docker network.

codespace:~ lab$ docker network create somenetwork
723c85e029776befbf6a6e0879c4da933eb724a47f8a4ae554cd476a964d6fe8

and we start Elasticsearch cluster by executing following command:

codespace:~ lab$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.3.1

Unable to find image 'elasticsearch:7.3.1' locally
7.3.1: Pulling from library/elasticsearch
8ba884070f61: Downloading [==>                                                ]  4.287MB/75.4MB
cc9d4578622f: Downloading [=====>                                             ]  4.471MB/44.02MB
b7b7fa6f4047: Download complete
6a765242045b: Downloading [===>                                               ]  19.26MB/286.6MB
ef93ffaca23f: Waiting
66ffcd421b07: Waiting
b88fc8b81f55: Waiting

Digest: sha256:d2b4be56552c6ff59029ddcc2d02b632cd6d0877ef08b9a6094a21ec62d18917
Status: Downloaded newer image for elasticsearch:7.3.1
9b6a03e5ab7e289d70df94e9147c875eaacbc27f36ce411fe1c2e3b6da32974b

Congratulations, you started your first container! But what all these commands really mean? Let’s go though the process step by step.

  • first command: docker network create somenetwork creates Docker network with a name “somenetwork” and returns networks ID as confirmation. More info on Docker Networks here.
  • Then we tell Docker to start Elasticsearch by executing: docker run -d –name elasticsearch –net somenetwork -p 9200:9200 -p 9300:9300 -e “discovery.type=single-node” elasticsearch:7.3.1
    • -d – tells Docker to start the process in background.
    • –name elasticsearch – sets containers name to elasticsearch.
    • net somenetwork – sets container to be started on somenetwork network.
    • -p 9200:9200 -p 9300:9300 – binds container ports to ports on host. It’s similar to tunneling to a server.
    • -e “discovery.type=single-node” – passes environment variable to a container.
    • elasticsearch:7.3.1 – sets image name and version.
  • Since required images are not found on local machine, Docker needs to download them from docker registry: Unable to find image ‘elasticsearch:7.3.1’ locally
    7.3.1: Pulling from library/elasticsearch
  • After downloading is finished ID of an image is printed out in a terminal.

Use The Container

First of all, to verify that container is really up and running execute “docker ps” in a terminal.

codespace:~ lab$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                            NAMES
9b6a03e5ab7e        elasticsearch:7.3.1   "/usr/local/bin/dock…"   3 minutes ago        Up 5 minutes        0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   elasticsearch

Then to validate that our Elasticsearch cluster is healthy we can simply open http://localhost:9200/ in a browser or run it as curl. Response should be similar to:

{
  "name" : "9b6a03e5ab7e",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "jY4vTVQDSzqu_3ARUBr1OA",
  "version" : {
    "number" : "7.3.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "4749ba6",
    "build_date" : "2019-08-19T20:19:25.651794Z",
    "build_snapshot" : false,
    "lucene_version" : "8.1.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Elasticsearch is ready to work. If container you started does not have such health check command: “docker logs elasticsearch” could be used to see logs of the container. More details in here.

Clean Up

So we did our testing and it is time to stop a container since we don’t need it anymore. That can be done by using “docker stop elasticsearch” command. Container name is returned as confirmation that stop command is executed. But what if stop command failed and container is still running? In that case we can use: “docker kill container-name” – that will stop container by force without waiting till it’s processes will be stoped.

codespace:~ lab$ docker stop elasticsearch
elasticsearch

After Elasticsearch is stopped we need to delete container and its image which was downloaded. In order to delete the container we need to know its ID. That can be accomplished by running “docker ps -a” – lists all containers active or not.

codespace:~ lab$ docker ps -a
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS                        PORTS               NAMES
9b6a03e5ab7e        elasticsearch:7.3.1                      "/usr/local/bin/dock…"   27 hours ago        Exited (143) 25 minutes ago                       elasticsearch
5f30a25ee63c        rabbitmq:3                               "docker-entrypoint.s…"   3 months ago        Exited (0) 3 months ago                           some-rabbit

When we know container ID we can delete it by executing – “docker rm 9b6a03e5ab7e“. Same has to be done to an image: “docker images” and then running – “docker rmi 3d3aa92f641f“.

codespace:~ lab$ docker rm 9b6a03e5ab7e
9b6a03e5ab7e

codespace:~ lab$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
elasticsearch                      7.3.1               3d3aa92f641f        2 weeks ago         807MB
rabbitmq                           3.7.17-management   99cbda713eff        4 weeks ago         177MB

codespace:~ lab$ docker rmi 3d3aa92f641f
Untagged: elasticsearch:7.3.1
Untagged: elasticsearch@sha256:d2b4be56552c6ff59029ddcc2d02b632cd6d0877ef08b9a6094a21ec62d18917
Deleted: sha256:3d3aa92f641fa974d71c7cf0f244c90cca19e755f43536107d0c18726b906981
Deleted: sha256:52abaedb449c7b068724c8406f3f766f332bd519d7120d73eed03eb1bb5361eb
Deleted: sha256:d97fd7bc7017b1d7bcf69591f289c227891e86b6ddb21f6308fc4d521afa443c
Deleted: sha256:93913de58ae6f5528b1b268dfce83843c99d6c683cc91d6d091aa8fb1051e2b6
Deleted: sha256:1608122062896fc6a68707912efd21e5f1d898ebe3d572c27e4ff7cc6f829950
Deleted: sha256:03f720c356936d00a0b365a300d3bc7ca9a1726e28ccac08822729a1d8adfad3
Deleted: sha256:a9a3d7e228ebdc7d57f77f678730fa1607a7f117c591a948fa4e937d49320d4c
Deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854

That is it. We have our environment reset to initial stage. Hope this post helps you to get a better understanding on how to use Docker too. All commands which we used are listed below.

Commands

  • docker -v – used to get version of Docker installed.
  • docker network create NAME – used to create Docker networks, for example: docker network create somenetwork.
  • docker network COMMAND – used to interact with Docker networks, for example: docker network ls – lists all networks created.
  • docker run – used to start Docker containers.
  • docker ps – lists all active containers.
  • docker ps -a – lists all created containers.
  • docker logs [OPTIONS] CONTAINER – used to access container logs, for example – docker logs -tail elasticsearch
  • docker stop CONTAINER – stops selected container, for example – docker stop elasticsearch
  • docker kill CONTAINER – kills selected container, for example – docker kill elasticsearch
  • docker rm CONTAINER-ID – removes container by id, for example – docker rm 9b6a03e5ab7e
  • docker images – lists all the images on a host.
  • docker rmi IMAGE-ID – removes downloaded images, for example – docker rmi 3d3aa92f641f
  • docker exec -it CONTAINER COMMAND – we didn’t used this one, but it is one if you want to access container, for example – docker exec -it elasticsearch /bin/bash

Add Comment

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