6

My goal is to have a docker container that has a running MySQL service. So each time when I start container the database is in the initial state and MySQL is running. Almost everything is fine but when I start the container the MySQL service is not running. Every time in the console I have to start it like this: service mysql start. Here are the steps how I build and run the container:

$ docker build -t executer:mysql .
Sending build context to Docker daemon 15.87 kB
Sending build context to Docker daemon 
Step 0 : FROM debian:wheezy
#
# ... Many steps without error
#
Step 17 : RUN mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/
 ---> Using cache
 ---> 1c71bf4524f0
Step 18 : RUN service mysql start
 ---> Running in b4643765b79b

......
MySQL Community Server 5.6.24 is started.
 ---> ac26b749a3c0
Removing intermediate container b4643765b79b
Successfully built ac26b749a3c0 

$ docker run --rm -it executer:mysql /bin/bash
root@1d9208c19af0:/# mysql
ERROR 2002 (HY000): Cant connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
root@1d9208c19af0:/# service mysql start
No directory, logging in with HOME=/
......
[info] MySQL Community Server 5.6.24 is started.
root@1d9208c19af0:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

And here is the content of the Dockerfile:

FROM debian:wheezy

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql

# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
# File::Basename
# File::Copy
# Sys::Hostname
# Data::Dumper
RUN apt-get update && apt-get install -y perl --no-install-recommends && rm -rf /var/lib/apt/lists/*

# gpg: key 5072E1F5: public key "MySQL Release Engineering <[email protected]>" imported
RUN apt-key adv --keyserver pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5

ENV MYSQL_MAJOR 5.6
ENV MYSQL_VERSION 5.6.24

RUN echo "deb http://repo.mysql.com/apt/debian/ wheezy mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list

# the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql)
# also, we set debconf keys to make APT a little quieter
RUN { \
        echo mysql-community-server mysql-community-server/data-dir select ''; \
        echo mysql-community-server mysql-community-server/root-pass password ''; \
        echo mysql-community-server mysql-community-server/re-root-pass password ''; \
        echo mysql-community-server mysql-community-server/remove-test-db select false; \
    } | debconf-set-selections \
    && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}"* && rm -rf /var/lib/apt/lists/* \
    && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql

RUN apt-get update && apt-get install -y procps

# comment out a few problematic configuration values
RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf

# VOLUME /var/lib/mysql
# Install database
ADD ./database.sql /var/db/database.sql

# Set Standard settings
ENV user student
ENV password secret
ENV url file:/var/db/database.sql
ENV right READ

# Install starting script
ADD ./start-database.sh /usr/local/bin/start-database.sh
RUN chmod +x /usr/local/bin/start-database.sh

#EXPOSE 3306

RUN mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/

RUN service mysql start

How should I change my Dockerfile to have a running MySQL service after running my container?

2

2 Answers 2

2

I tried your setup

see https://github.com/BITPlan/docker-stackoverflowanswers/tree/master/33299303

first I got

E: Version '5.6.24*' for 'mysql-server' was not found

which has nothing to do with your question but needs to be fixed. so i commented out most of your Dockerfile and added:

RUN apt-get install mysql-server-5.5

which gave: E: Unable to locate package mysql-server-5.5

this is distro specific so i changed the FROM line to

FROM ubuntu:14.04

which I am more familiar with

Then i modified the Dockerfile to change

RUN service mysql start

to

CMD service mysql start

as mentioned in the comment of michaelbahr and checked with

./run 

and

root@96ff34b4e0c0:/# service --status-all 2>&1 | grep mysql

showed:

[ ? ]  mysql

there you are - mysql runs. You might want to fix your Dockerfile accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

For me this solution introduced another problem. During the install I have to provide the password. But I could fix it like this: RUN DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server
You can also set the noninteractive frontend via ENV DEBIAN_FRONTEND noninteractive at the top of your Dockerfile
2

An alternative solution is to use MySQL docker image : https://hub.docker.com/_/mysql/

All you have to do is:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

where:

  • some-mysql is the name you want to assign to your container

  • my-secret-pw is the password to be set for the MySQL root user

  • tag is the tag specifying the MySQL version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.