In some previous post we learned how we could work on docker, its installation on CentOS or Ubuntu machine, Downloading docker images to work, working on them for some basic work. In This post we will see some important topic regarding Docker images, how we can download and do some other stuff with docker images that make us more clear for docker images.

I am using Ubuntu 18.04 machine for writing this post, with below docker version.

~ docker -v            
Docker version 18.06.1-ce, build e68fc7a

~ lsb_release -d
Description:	Ubuntu 18.04.1 LTS

Topic covered in this Post

  • How to search Image
  • How to download Image
  • How to run Docker container
  • How to connect Docker container port for services

Docker images Search

Before Downloading and start working , we should always search some official image and investigate about instead just download and start on your environment. So for search and investigating we could use docker search command. Like in case we need to use Apache software through docker image, so we should always search in docker instead download some random images like below.

docker search apache

When we ran this above command, it comes up with some images sort through STARS and their this column name official which has mark OK for some image only, these image are official images from Apache organisations uploaded on Docker hub.

We can also check it through Docker image web-page which is separately maintained through official partners like Apache manages https://hub.docker.com/_/httpd and MySQL manages https://hub.docker.com/_/mysql.

These pages manages through these organisation itself for any update on docker images. They also provide some documentation for these docker images like how to download and use these docker images.

Advantages to download these images instead of random

  • Web Page is managed through software or application official organisation, So it reliable to use
  • On time update, If there is any update in software or application announced or released that also update on these Docker site images as well
  • Well maintained documentation provided with image which cover details usage of images and software.

As these official images are so reliable, their download counts and STARS are quit high, which again provide assurance that before us many peoples using this images to use these sofwares.

Now let’s see how can we search and download and further use these images with some examples and use cases.

For download we first need to search through docker search command and need to take decision which image need to download for use. So run below command to search available MySQL images.

#  docker search mysql

This will show plenty of images available with MySQL name, but we need to see most reliable, latest and used one which must be first one coming on top.

So first one is MySQL and second is MariaDB, both are MySQL version used world widely shows on top when request for MySQL docker image, it depend upon user, could use any one of these image.

While searching for Docker image we could do many more things to refine our search.

In case we like limit our search like only see first 5 search

docker search mysql --limit 5

To search only automated images

docker search mysql --filter=is-automated=true

In case we just like to search official images only

docker search mysql --filter=is-Official=true

Here might you noticed that we are not able to see complete descriptions while searching, but yes we can see it if we use –no-trunc options with search command.

docker search mysql --filter=is-Official=true --no-trunc

We can also search as per stars count of images.

~ docker search mysql --filter=stars=200

We can also add two filters like below

docker search mysql --filter stars=200 --filter is-official=true

We also have one pretty way to print these search in our own way like below.

~ docker search --format "table {{.Name}} {{.StarCount}}" mysql --limit 5
NAME STARS
mysql 7780
mysql/mysql-server 589
zabbix/zabbix-server-mysql 166
mysql/mysql-cluster 41
circleci/mysql 10

We can also modify it bit more organize way and also use filters with them.

~ docker search --format "table {{.Name}}\t{{.StarCount}}\t{{.IsOfficial}}" mysql --filter "is-official=true"
NAME                STARS               OFFICIAL
mysql               7780                [OK]
mariadb             2564                [OK]
percona             413                 [OK]

Here we used some placeholders under double curly braces {{.Name}}, these are already defined like below.

Placeholder     Description
----------------------------
.Name           Image Name
.Description    Image description
.StarCount      Number of stars for the image
.IsOfficial     “OK” if image is official
.IsAutomated    “OK” if image build was automated

In between these, we used tab \t for space.

Docker image Download

Now we are ready to download after see lots things about docker image searching.

Let’s download MySQL docker image through docker pulland work on it

~ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
6ae821421a7d: Pull complete
a9e976e3aa6d: Pull complete
e3735e44a020: Pull complete
bfd564e9483f: Pull complete
df705f26e488: Pull complete
0c5547f73d62: Pull complete
f437382cf8a1: Pull complete
b8e2d50f1513: Pull complete
e2e3c9928180: Pull complete
b60db6d282cd: Pull complete
1d32deab69c6: Pull complete
408a40cd2e9c: Pull complete
Digest: sha256:a571337738c9205427c80748e165eca88edc5a1157f8b8d545fa127fc3e29269
Status: Downloaded newer image for mysql:latest

Now we downloaded MySQL docker image, now how to see it. With docker image command we could see all images downloaded on Host Machine,but can provide only specific images as well.

~ docker images mysql
REPOSITORY    TAG      IMAGE ID        CREATED      SIZE
mysql         latest   81f094a7e4cc    3 days ago   477MB

In above command it shows only MySQL image, because we ran docker image with mysql argument. It shows many columns all has its own importance, will check them later in post.

Now we need to start this image to work on , so instruction to use and work on same is mentioned in Docker hub MySQL page https://hub.docker.com/_/mysql. As per it this docker image could run through below code.

~ docker run --name mysql-docker -e MYSQL_ROOT_PASSWORD=passw0rd -d mysql
6285e4f9836ad272c32b9a8b0d7b2f4ca8193278bbccba7cb84ddc8295e69fbd

This would run docker container with latest docker image which we just downloaded, but in case you like to download and run from another image, you have to run this command with option -d mysql:5.7.25.i ran this command with latest version because i just downloaded it, in case we use another version it will search locally, if not found try to download and start it later, like below.

~ docker run --name mysql-docker -e MYSQL_ROOT_PASSWORD=passw0rd -d mysql:5.7.25

In this we used –name for assigning docker name -e is argument used for MySQL image for assigning password for mySQL instance -d used for MySQL image used to create Docker container.

We could also see this container running on host machine through below command.

~ docker ps -a
CONTAINER ID   IMAGE    COMMAND                  CREATED             STATUS         PORTS                 NAMES
6285e4f9836a   mysql    "docker-entrypoint.s…"   17 minutes ago      Up 17 minutes  3306/tcp, 33060/tcp   mysql-docker

Access of Docker container

Now two things how could access this docker container and access MySQL database to work. So let’s do one by one.

Docker console access

Access docker container through its bash shell.

~ docker exec -it mysql-docker bash
root@6285e4f9836a:/# hostname 
6285e4f9836a

But what if like to access MySQL database, although from its own container bash we can access MySQL prompt like below

root@6285e4f9836a:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> exit
Bye

But there is no use of it until we can connect this MySQL database from any Machine or other container.Let’s take it one by one.

As far I know presently we can access Docker container Ports or Services through three ways

From mentioned three way to connect Port or services of docker, first two ways have wide possibilities to access from outside world, but from third option it just open for particular docker container only. I have discussed first option in one previous post, so not going to repeat it again. So let’s straight try for second one.

Creation of Docker with publish port

In second way we publish our docker container port for outside world in such that Host could connect to docker container directly and outside world could connect on Host IP on publish port. For same we need re-create this docker container with published required ports, like below

~ docker run -p 3306:3306/tcp --name mysql-docker -e MYSQL_ROOT_PASSWORD=passw0rd -d mysql
be95eb4f95ccf93992d1f5d03657c63d5dcd7218750f7b136129aa820a48807a

In above commands we started docker container with MySQL published port in such way same port accessible on host to connect container MySQL.
we create docker again with -p options which can contained IP:host_port:container_port or IP::container_port or host_port:container_port.

Let’s see how we could connect on Docker container MySQL port.

~ docker port mysql-docker
3306/tcp -> 0.0.0.0:3306

~ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql-docker
172.17.0.2

~ mysql -u root -h 172.17.0.2 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

So it not just publish like above Host machine could connect on container IP, any machine in network can connect through Host 3306 port, so from now any network machine can connect on Host port will directly connect to container port as well, like below.

~ mysql -u root -h 127.0.0.1 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

In above we connect on 127.0.0.1, but on host there is no MySQL service is running. See below in case we try to connect localhost, we fail.

~ mysql -u root -h localhost -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Access docker Ports through another docker

With this method, we have to start another docker container with link to this docker container’s MySQL port like below.

~ docker run --name centos --link mysql-docker:mysql -it centos
[root@fa4f280c4d4a /]# #                                                                                                                    
~ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
fa4f280c4d4a        centos              "/bin/bash"              12 seconds ago      Up 11 seconds                             centos
6285e4f9836a        mysql               "docker-entrypoint.s…"   3 hours ago         Up 3 hours          3306/tcp, 33060/tcp   mysql-docker

Now we could try access MySQL Dokcer container from this CentOS Docker container, although MySQL client binary is not install on centOS docker container, so we need to install it and could try access it like below.

~ docker attach centos
[root@fa4f280c4d4a /]#mysql -u root -h 172.17.0.2 -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> create database dummy;
Query OK, 1 row affected (0.03 sec)

MySQL [(none)]> 

So with this complete post , we now know how to search, download , run and access Docker containers