You are here

First Docker steps with MySQL and MariaDB

The Docker version of the distributions are often quite old. On Ubuntu 16.04 for example:

shell> docker --version 
Docker version 1.13.1, build 092cba3

But the current docker version is 17.09.0-ce (2017-09-26). It seems like they have switched from the old version schema x.y.z to the new year.month.version version schema in February/March 2017.

Install Docker CE Repository

Add the Docker's official PGP key:

shell> curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

Add the Docker repository:

shell> echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable" > /etc/apt/sources.list.d/docker.list
shell> apt-get update

Install or upgrade Docker:

shell> apt-get install docker-ce
shell> docker --version
Docker version 17.09.0-ce, build afdb6d4

To test your Docker installation run:

shell> docker run --rm hello-world

Add Docker containers for MariaDB, MySQL and MySQL Enterprise Edition

First we want to see what Docker containers are available:

shell> docker search mysql --no-trunc --filter=stars=100
NAME               DESCRIPTION                                                                                         STARS OFFICIAL AUTOMATED
mysql              MySQL is a widely used, open-source relational database management system (RDBMS).                  5273  [OK]
mariadb            MariaDB is a community-developed fork of MySQL intended to remain free under the GNU GPL.           1634  [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Created, maintained and supported by the MySQL team at Oracle 368            [OK]
percona            Percona Server is a fork of the MySQL relational database management system created by Percona.     303   [OK]
...

OK. It seems like MySQL Server Enterprise Edition is missing. So we have to create an account on Docker Store and get the MySQL Server Enterprise Edition Image from there:

shell> docker login --username=fromdual
Password:
Login Succeeded

Unfortunately one can still not see MySQL Server Enterprise Edition.

But we can try anyway:

shell> docker pull store/oracle/mysql-enterprise-server:5.7
shell> docker logout
shell> docker pull mysql
shell> docker pull mariadb
shell> docker pull mysql/mysql-server

To see what is going on on your local Docker registry you can type:

shell> docker images
REPOSITORY                           TAG    IMAGE ID     CREATED       SIZE
mariadb                              latest abcee1d29aac 8 days ago    396MB
mysql                                latest 5709795eeffa 2 weeks ago   408MB
mysql/mysql-server                   latest a3ee341faefb 5 weeks ago   246MB
store/oracle/mysql-enterprise-server 5.7    41bf2fa0b4a1 4 months ago  244MB
hello-world                          latest 48b5124b2768 10 months ago 1.84kB

I personally do not like that all those images which are tagged with latest because I want a clear control over what version is used. MariaDB and MySQL community server have implemented this quite nicely but not MySQL Enterprise Edition:

shell> docker pull mariadb:10.0
shell> docker pull mariadb:10.0.23
shell> docker pull mysql:8.0
shell> docker pull mysql:8.0.3

docker images | sort
REPOSITORY                           TAG     IMAGE ID     CREATED       SIZE
hello-world                          latest  48b5124b2768 10 months ago 1.84kB
mariadb                              10.0.23 93631b528e67 21 months ago 305MB
mariadb                              10.0    eecd58425049 8 days ago    337MB
mariadb                              latest  abcee1d29aac 8 days ago    396MB
mysql                                8.0.3   e691422324d8 2 weeks ago   343MB
mysql                                8.0     e691422324d8 2 weeks ago   343MB
mysql                                latest  5709795eeffa 2 weeks ago   408MB
mysql/mysql-server                   latest  a3ee341faefb 5 weeks ago   246MB
store/oracle/mysql-enterprise-server 5.7     41bf2fa0b4a1 4 months ago  244MB

Run a MariaDB server container

Start a new Docker container from the MariaDB image by running:

shell> CONTAINER_NAME=mariadb
shell> CONTAINER_IMAGE=mariadb
shell> TAG=latest
shell> MYSQL_ROOT_PASSWORD=Secret-123
shell> MYSQL_ROOT_USER=root

shell> docker run \
  --name=${CONTAINER_NAME} \
  --detach \
  --env=MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
  ${CONTAINER_IMAGE}:${TAG}

shell> docker ps
CONTAINER ID IMAGE          COMMAND                CREATED        STATUS        PORTS    NAMES
60d7b6de7ed1 mariadb:latest "docker-entrypoint..." 24 seconds ago Up 23 seconds 3306/tcp mariadb

shell> docker logs ${CONTAINER_NAME}

shell> docker exec \
  --interactive \
  --tty \
  ${CONTAINER_NAME} \
  mysql --user=${MYSQL_ROOT_USER} --password=${MYSQL_ROOT_PASSWORD} --execute="status"

shell> docker image tag mariadb:latest mariadb:10.2.10

shell> docker exec --interactive \
  --tty \
  ${CONTAINER_NAME} \
  bash 

shell> docker stop ${CONTAINER_NAME}
shell> docker rm ${CONTAINER_NAME}

Run a MySQL Community server container

shell> CONTAINER_NAME=mysql
shell> CONTAINER_IMAGE=mysql/mysql-server
shell> TAG=latest
shell> MYSQL_ROOT_PASSWORD=Secret-123

shell> docker run \
  --name=${CONTAINER_NAME} \
  --detach \
  --env=MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
  ${CONTAINER_IMAGE}:${TAG}

shell> docker stop ${CONTAINER_NAME}
shell> docker rm ${CONTAINER_NAME}

Run a MySQL Server Enterprise Edition container

shell> CONTAINER_NAME=mysql-ee
shell> CONTAINER_IMAGE=store/oracle/mysql-enterprise-server
shell> TAG=5.7
shell> MYSQL_ROOT_PASSWORD=Secret-123

shell> docker run \
  --name=${CONTAINER_NAME} \
  --detach \
  --env=MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
  ${CONTAINER_IMAGE}:${TAG}

shell> docker ps --all
CONTAINER ID IMAGE                                    COMMAND                CREATED        STATUS                  PORTS               NAMES
0cb4e6a8a621 store/oracle/mysql-enterprise-server:5.7 "/entrypoint.sh my..." 37 seconds ago Up 36 seconds (healthy) 3306/tcp, 33060/tcp mysql-ee
1832b98da6ef mysql:latest                             "docker-entrypoint..." 6 minutes ago  Up 6 minutes            3306/tcp            mysql
60d7b6de7ed1 mariadb:latest                           "docker-entrypoint..." 21 minutes ago Up 21 minutes           3306/tcp            mariadb

All my 3 docker containers are currently running as root:

shell> ps -ef | grep docker
root 13177     1 20:20 ? 00:00:44 /usr/bin/dockerd -H fd://
root 13186 13177 20:20 ? 00:00:04 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
root 24004 13186 21:41 ? 00:00:00 docker-containerd-shim 60d7b6de7ed1ff62b67e66c6effce0094fd60e9565ede65fa34e188b636c54ec /var/run/docker/libcontainerd/60d7b6de7ed1ff62b67e66c6effce0094fd60e9565ede65fa34e188b636c54ec docker-runc
root 26593 13186 21:56 ? 00:00:00 docker-containerd-shim 1832b98da6ef7459c33181e9b9ddd89a4136c3b2676335bcbbb533389cbf6219 /var/run/docker/libcontainerd/1832b98da6ef7459c33181e9b9ddd89a4136c3b2676335bcbbb533389cbf6219 docker-runc
root 27714 13186 22:02 ? 00:00:00 docker-containerd-shim 0cb4e6a8a62103b66164ccddd028217bb4012d8a6aad1f62d3ed6ae71e1a38b4 /var/run/docker/libcontainerd/0cb4e6a8a62103b66164ccddd028217bb4012d8a6aad1f62d3ed6ae71e1a38b4 docker-runc

But the user running the process IN the container is not root:

shell> docker exec \
  --interactive \
  --tty \
  ${CONTAINER_NAME} \
  grep ^Uid /proc/1/status
Uid:    27      27      27      27

shell> docker exec \
  --interactive \
  --tty \
  ${CONTAINER_NAME} \
  bash -c "id 27"
uid=27(mysql) gid=27(mysql) groups=27(mysql)

Run a Docker container from mysql user

shell> id
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
shell> docker images
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.32/images/json: dial unix /var/run/docker.sock: connect: permission denied

shell> sudo adduser mysql docker
Adding user `mysql' to group `docker' ...
Adding user mysql to group docker
Done.

Taxonomy upgrade extras: