Docker Image of CentOS-6 6.8 x86_64, MySQL 5.1.
Includes Automated password generation and an option for custom initialisation SQL. Supports custom configuration via environment variables.
The latest CentOS-6 based release can be pulled from the centos-6 Docker tag. For a specific release tag the convention is centos-6-1.7.0
for the 1.7.0 release tag.
- centos-6 (centos-6/Dockerfile)
The Dockerfile can be used to build a base image that is the bases for several other docker images.
Included in the build are the SCL, EPEL and IUS repositories. Installed packages include OpenSSH secure shell, vim-minimal, MySQL Server and client programs are installed along with python-setuptools, supervisor and supervisor-stdout.
Supervisor is used to start the mysqld server daemon when a docker container based on this image is run. To enable simple viewing of stdout for the service's subprocess, supervisor-stdout is included. This allows you to see output from the supervisord controlled subprocesses with docker logs {container-name}
.
If enabling and configuring SSH access, it is by public key authentication and, by default, the Vagrant insecure private key is required.
SSH is not required in order to access a terminal for the running container. The simplest method is to use the docker exec command to run bash (or sh) as follows:
$ docker exec -it {container-name-or-id} bash
For cases where access to docker exec is not possible the preferred method is to use Command Keys and the nsenter command. See command-keys.md for details on how to set this up.
Run up a container named mysql.pool-1.1.1
from the docker image jdeathe/centos-ssh-mysql
on port 3306 of your docker host.
$ docker run -d \
--name mysql.pool-1.1.1 \
-p 3306:3306 \
-v /var/lib/mysql \
jdeathe/centos-ssh-mysql:centos-6
Now you can verify it is initialised and running successfully by inspecting the container's logs.
$ docker logs mysql.pool-1.1.1
If it is the first run there should be additional output showing the initialisation SQL that was run and the root user's password.
The MySQL table data is persistent across container restarts by setting the MySQL data directory /var/lib/mysql
as a data volume. We didn't specify a name or docker_host path so Docker will give it a unique name and store it in /var/lib/docker/volumes/
; to find out where the data is stored on the Docker host you can use docker inspect
.
$ docker inspect \
--format '{{ json (index .Mounts 0).Source }}' \
mysql.pool-1.1.1
To access the MySQL SQL shell run the following:
$ docker exec -it mysql.pool-1.1.1 mysql -p -u root
To import the Sakila example database from the MySQL Documentation and view the first 2 records from the film table.
$ export MYSQL_ROOT_PASSWORD={your-password}
$ docker exec -i mysql.pool-1.1.1 \
mysql -p${MYSQL_ROOT_PASSWORD} -u root \
<<< $(tar -xzOf /dev/stdin \
<<< $(curl -sS http://downloads.mysql.com/docs/sakila-db.tar.gz) \
sakila-db/sakila-schema.sql \
)
$ docker exec -i mysql.pool-1.1.1 \
mysql -p${MYSQL_ROOT_PASSWORD} -u root \
<<< $(tar -xzOf /dev/stdin \
<<< $(curl -sS http://downloads.mysql.com/docs/sakila-db.tar.gz) \
sakila-db/sakila-data.sql \
)
$ docker exec -it mysql.pool-1.1.1 \
mysql -p${MYSQL_ROOT_PASSWORD} -u root \
-e "SELECT * FROM sakila.film LIMIT 2 \G;"
To run the a docker container from this image you can use the standard docker commands. Alternatively, you can use the embedded (Service Container Manager Interface) scmi that is included in the image since centos-6-1.7.1
or, if you have a checkout of the source repository, and have make installed the Makefile provides targets to build, install, start, stop etc. where environment variables can be used to configure the container options and set custom docker run parameters.
The following example uses docker to run the SCMI install command to create and start a container named mysql.pool-1.1.1
. To use SCMI it requires the use of the --privileged
docker run parameter and the docker host's root directory mounted as a volume with the container's mount directory also being set in the scmi
--chroot
option. The --setopt
option is used to add extra parameters to the default docker run command template; in the following example a named configuration volume is added which allows the SSH host keys to persist after the first container initialisation. Not that the placeholder {{NAME}}
can be used in this option and is replaced with the container's name.
Note: In most cases you will want to create an initial database, database user, (optionally a static password), and define the user's network access. If you don't define these settings using the appropriate environment variables on first run, the settings will not be parsed by the bootstrap initialisation process and only local root access will be available. To re-initialise a container that uses a named data volume mapped to /var/lib/mysql terminate the container and the data volume to allow it to be recreated.
$ docker run \
--rm \
--privileged \
--volume /:/media/root \
jdeathe/centos-ssh-mysql:centos-6-1.7.1 \
/usr/sbin/scmi install \
--chroot=/media/root \
--tag=centos-6-1.7.1 \
--name=mysql.pool-1.1.1 \
--setopt='--volume {{NAME}}.data-mysql:/var/lib/mysql'
To uninstall the previous example simply run the same docker run command with the scmi uninstall
command.
$ docker run \
--rm \
--privileged \
--volume /:/media/root \
jdeathe/centos-ssh-mysql:centos-6-1.7.1 \
/usr/sbin/scmi uninstall \
--chroot=/media/root \
--tag=centos-6-1.7.1 \
--name=mysql.pool-1.1.1 \
--setopt='--volume {{NAME}}.data-mysql:/var/lib/mysql'
If your docker host has systemd (and optionally etcd) installed then scmi
provides a method to install the container as a systemd service unit. This provides some additional features for managing a group of instances on a single docker host and has the option to use an etcd backed service registry. Using a systemd unit file allows the System Administrator to use a Drop-In to override the settings of a unit-file template used to create service instances. To use the systemd method of installation use the -m
or --manager
option of scmi
and to include the optional etcd register companion unit use the --register
option.
$ docker run \
--rm \
--privileged \
--volume /:/media/root \
jdeathe/centos-ssh-mysql:centos-6-1.7.1 \
/usr/sbin/scmi install \
--chroot=/media/root \
--tag=centos-6-1.7.1 \
--name=mysql.pool-1.1.1 \
--manager=systemd \
--register \
--env='MYSQL_SUBNET="0.0.0.0/0.0.0.0"' \
--env='MYSQL_USER="app-user"' \
--env='MYSQL_USER_PASSWORD="Passw0rd"' \
--env='MYSQL_USER_DATABASE="app-db"' \
--setopt='--volume {{NAME}}.data-mysql:/var/lib/mysql'
If your docker host has systemd, fleetd (and optionally etcd) installed then scmi
provides a method to schedule the container to run on the cluster. This provides some additional features for managing a group of instances on a fleet cluster and has the option to use an etcd backed service registry. To use the fleet method of installation use the -m
or --manager
option of scmi
and to include the optional etcd register companion unit use the --register
option.
Since release centos-6-1.7.1
the install template has been added to the image metadata. Using docker inspect you can access scmi
to simplify install/uninstall tasks.
To see detailed information about the image run scmi
with the --info
option. To see all available scmi
options run with the --help
option.
$ eval "sudo -E $(
docker inspect \
-f "{{.ContainerConfig.Labels.install}}" \
jdeathe/centos-ssh-mysql:centos-6-1.7.1
) --info"
To perform an installation using the docker name mysql.pool-1.2.1
simply use the --name
or -n
option.
$ eval "sudo -E $(
docker inspect \
-f "{{.ContainerConfig.Labels.install}}" \
jdeathe/centos-ssh-mysql:centos-6-1.7.1
) --name=mysql.pool-1.2.1"
To uninstall use the same command that was used to install but with the uninstall
Label.
$ eval "sudo -E $(
docker inspect \
-f "{{.ContainerConfig.Labels.uninstall}}" \
jdeathe/centos-ssh-mysql:centos-6-1.7.1
) --name=mysql.pool-1.2.1"
With the addition of install/uninstall image labels it is possible to use Project Atomic's atomic install
command to simplify install/uninstall tasks on CentOS Atomic Hosts.
To see detailed information about the image run scmi
with the --info
option. To see all available scmi
options run with the --help
option.
$ sudo -E atomic install \
-n mysql.pool-1.3.1 \
jdeathe/centos-ssh-mysql:centos-6-1.7.1 \
--info
To perform an installation using the docker name mysql.pool-1.3.1
simply use the -n
option of the atomic install
command.
$ sudo -E atomic install \
-n mysql.pool-1.3.1 \
jdeathe/centos-ssh-mysql:centos-6-1.7.1
Alternatively, you could use the scmi
options --name
or -n
for naming the container.
$ sudo -E atomic install \
jdeathe/centos-ssh-mysql:centos-6-1.7.1 \
--name mysql.pool-1.3.1
To uninstall use the same command that was used to install but with the uninstall
Label.
$ sudo -E atomic uninstall \
-n mysql.pool-1.3.1 \
jdeathe/centos-ssh-mysql:centos-6-1.7.1
The following example sets up a custom MySQL database, user and user password on first run. This will only work when MySQL runs the initialisation process and values must be specified for MYSQL_USER and MYSQL_USER_DATABASE. If MYSQL_USER_PASSWORD is not specified or left empty a random password will be generated.
$ docker stop mysql.pool-1.1.1 && \
docker rm mysql.pool-1.1.1
$ docker run -d \
--name mysql.pool-1.1.1 \
--publish 3306:3306 \
--env "MYSQL_SUBNET=0.0.0.0/0.0.0.0" \
--env "MYSQL_USER=app-user" \
--env "MYSQL_USER_PASSWORD=" \
--env "MYSQL_USER_DATABASE=app-db" \
--volume mysql.pool-1.1.1.data-mysql:/var/lib/mysql \
jdeathe/centos-ssh-mysql:centos-6
The environmental variable MYSQL_SUBNET
is optional but can be used to generate users with access to databases outside the localhost
, (the default for the root user). In the example, the subnet definition 0.0.0.0/0.0.0.0
allows connections from any network which is equivalent to the wildcard symbol, %
, in MySQL GRANT definitions.
Now you can verify it is initialised and running successfully by inspecting the container's logs:
$ docker logs mysql.pool-1.1.1
There are several environmental variables defined at runtime these allow the operator to customise the running container.
Note: Most of these settings are only evaluated during the first run of a named container; if the data volume already exists and contains database table data then changing these values will have no effect.
On first run the root user is created with an auto-generated password. If you require a specific password, MYSQL_ROOT_PASSWORD
can be used when running the container.
...
--env "MYSQL_ROOT_PASSWORD=Passw0rd!" \
...
To indicate MYSQL_ROOT_PASSWORD
is a pre-hashed value instead of the default plain-text type set MYSQL_ROOT_PASSWORD_HASHED
to true
.
...
--env "MYSQL_ROOT_PASSWORD=*03F7361A0E18DA99361B7A82EA575944F53E206B" \
--env "MYSQL_ROOT_PASSWORD_HASHED=true" \
...
Note: To generate a pre-hashed password you could use the following MySQL command.
$ mysql -u root -p{mysql_root_password} \
-e "SELECT PASSWORD('{mysql_user_password}');"
On first run, a database user and database can be created. Set MYSQL_USER
to a non-empty string. A corresponding MYSQL_USER_DATABASE
value must also be set for the user to be given access too.
...
--env "MYSQL_USER=app-user" \
...
On first run, if the database user MYSQL_USER
is specified then it is created with an auto-generated password. If you require a specific password, MYSQL_USER_PASSWORD
can be used when running the container.
...
--env "MYSQL_USER_PASSWORD=appPassw0rd!" \
...
To indicate MYSQL_USER_PASSWORD
is a pre-hashed value instead of the default plain-text type set MYSQL_USER_PASSWORD_HASHED
to true
.
...
--env "MYSQL_USER_PASSWORD=*4215553ECE7A18BC09C16DB9EBF03FACFF49166B" \
--env "MYSQL_USER_PASSWORD_HASHED=true" \
...
On first run, if the database user MYSQL_USER
is specified then you must also define a corresponding database name. MYSQL_USER_DATABASE
can be used when running the container.
...
--env "MYSQL_USER_DATABASE=app-db" \
...