8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Linux » Here

Docker : Quick Example with MySQL

This article provides a simple example of using existing Docker images to create a new Docker container. In this case it is a MySQL image, but the process is similar for other images.

Related articles.

Assumptions

This article makes the following assumptions.

Create Docker Container

The Docker run command is documented here. The basic syntax is as follows.

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

The only mandatory parameter is the "image" the container will be derived from, which can optionally use a "tag" which is typically used to identify a specific version of the image. Many images will use the "latest" tag, which not surprisingly pulls down the latest version of an image.

The following command will run a MySQL 5.7 database in a container. If the image doesn't already exist locally, or it has been updated since the last time it was pulled, it is pulled from Docker Hub.

[docker_user@docker ~]$ docker run -dit --name mysql57-con -e MYSQL_ROOT_PASSWORD=Password123 -p 3310:3306 mysql:5.7

Notice at the end of the command the image name "mysql" and the tag "5.7". The meaning of the options are as follows.

-d     : Detach. Run the container in the background, so we can connect and disconnect without the container dying.
-i     : Keep STDIN open even if not attached.
-t     : Allocate a pseudo-tty
--name : Specify a name for the container. Subsequent commands can reference that, rather than the generated container id.
-e     : Used to send in any required environment variables. In this case the MySQL root password we want the container to use.
-p     : Map a port exposed by the container to a physical port on the host, using the format host-port:container-port.

Connecting to a Docker Container

Once the relevant images have been pulled and the container started, we can connect to OS using the exec command. Here we are asking for an interactive session (-i) with a pseudo-TTY (-t) and we are saying we want to run a shell (bash).

[docker_user@docker ~]$ docker exec -it mysql57-con bash
root@7e65aedc5a49:/#

From a usability perspective, the container feels like a conventional machine once we are connected. We can see the OS associated with the container as follows.

root@7e65aedc5a49:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@7e65aedc5a49:/#

We are using an Oracle Linux 7 (OL7) host, but we can see the container is running Debian. The container includes the necessary Debian files, but the container uses the host kernel rather than the Debian kernel.

Since this is a MySQL container, we can connect to the MySQL instance in the normal way, using the password specified by the run command.

root@7e65aedc5a49:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.00 sec)

mysql> exit
Bye
root@7e65aedc5a49:/# exit
exit
[docker_user@docker ~]$

Notice we exited the MySQL command line and the container, so we are back at the host shell.

In the previous example we ran a bash shell, allowing using perform commands inside the container. An alternative is to run the command directly. In the following example we will connect to the MySQL instance in the container directly from the host.

[docker_user@docker ~]$ docker exec -it mysql57-con mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> CREATE DATABASE testdb;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT USAGE ON testdb.* TO 'myuser'@'%' IDENTIFIED BY 'MyPassword1';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[docker_user@docker ~]$

Notice that exiting the MySQL command line returned us to the host shell, not the container shell.

Since we mapped port 3306 exposed from the container to port 3310 on the host, we can also connect directly to MySQL running inside the container from the host. You are going to need the MySQL client installed on the host machine, which you can do by running the following command as the root user.

# yum install -y mysql

Once you have the client you can connect to the database as follows.

[docker_user@docker ~]$ mysql -u root --host=127.0.0.1 --port=3310 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> exit
Bye
[docker_user@docker ~]$

List Existing Containers

We can list running containers using the ps command from the host OS. Adding the "-a" option lists running and idle containers. The following list includes the MySQL 5.7 container we just created and an idle container based on one of my images.

[docker_user@docker ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7e65aedc5a49        mysql:5.7           "docker-entrypoint..."   12 minutes ago      Up 12 minutes       0.0.0.0:3306->3306/tcp   mysql57-con
[docker_user@docker ~]$

Start and Stop Containers

The start and stop commands do exactly what you would expect them to do to the container. We can specify the container using the container name, or the container ID.

[docker_user@docker ~]$ docker stop 7e65aedc5a49
a1b42c7a76b2
[docker_user@docker ~]$

[docker_user@docker ~]$ docker start mysql57-con
mysql57-con
[docker_user@docker ~]$

Remove Containers

The rm command is used to remove a container. The "-f" option allows you to force the removal of a running container. Without it you will have to manually stop the container before removing it. The "-v" option removes the associated volumes.

[docker_user@docker ~]$ docker rm -vf mysql57-con
mysql57-con
[docker_user@docker ~]$

For more information see:

Hope this helps. Regards Tim...

Back to the Top.