San Lee
San Lee

Reputation: 59

install mysql in docker and expose mysql service to outside

I am learning docker these days. And I want to install mysql inside docker container.

Here is my Dockerfile

FROM ubuntu:14.04

ADD ./setup_mysql.sh /setup_mysql.sh
RUN chmod 755 /setup_mysql.sh
RUN /setup_mysql.sh

EXPOSE 3306

CMD ["/usr/sbin/mysqld"]

and shell script setup_mysql.sh

apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server

sed -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

service mysql start &
sleep 5

echo "UPDATE mysql.user SET password=PASSWORD('rootpass') WHERE user='root'" | mysql
echo "CREATE DATABASE devdb" | mysql
echo "GRANT ALL ON devdb.* TO devuser @'%' IDENTIFIED BY 'devpass'" | mysql

sleep 5
service mysql stop

Something wrong happend when running sudo docker build -t test/devenv .

Setting up mysql-server-5.5 (5.5.38-0ubuntu0.14.04.1) ...
invoke-rc.d: policy-rc.d denied execution of stop.
invoke-rc.d: policy-rc.d denied execution of start.

And if I remove the second sleep 5, the command service mysql stop will throw

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Why does this happen?

Thank you!

Upvotes: 2

Views: 5223

Answers (3)

Nikhil Katre
Nikhil Katre

Reputation: 2234

Here is a good post which tries to root cause the issue you are facing.

Shorter way:

  1. RUN echo "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d should resolve your issue OR

  2. If that doesn't resolve the issue, try running your docker container with privileged option. Like this, docker run --privileged -d -ti DOCKER_IMAGE:TAG

Ideally, I would not recommend running container with privileged option unless its a test bed container. The reason being running a docker container with privileged gives all capabilities to the container, and it also lifts all the limitations enforced. In other words, the container can then do almost everything that the host can do. But this is not a good practice. This defeats the docker purpose of isolating from host machine.

The ideal way to do this is to set capabilities of your docker container based on what you want to achieve. Googling this should help you out to provide appropriate capability for your docker container.

Upvotes: 0

Aarti Dalvi
Aarti Dalvi

Reputation: 1

Add this to your dockerfile:

RUN su
RUN echo exit 0 > /usr/sbin/policy-rc.d

I was facing the same issue. This code fixed it.

Upvotes: 0

Mark O'Connor
Mark O'Connor

Reputation: 77951

I high recommend leveraging the work of others. For example checkout the Mysql image from the docker registry:

Here's the associated git repository files:

If you look into the Dockerfile you'll notice the software is being installed as expected:

.. apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}"* ..

The trick is to realize that a database instance is not the same thing as the database software, only the latter is shipped with the image. Creating DBs and loading them with data is something that is done at run-time. So that work is done by an extra script, pulled into the image and setup to be executed when you run the container:

COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

Hope this helps.

Upvotes: 1

Related Questions