1

I want to create a simple container that contains a MySQL server with an initialized database. My Dockerfile currently looks like this:

FROM ubuntu:16.10

RUN apt-get update

# install MySQL; root user with no password
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get install -y mysql-server mysql-client

RUN service mysql start
RUN mysqladmin -u root create mydb

CMD [ "bash" ]

However, when I build the image via docker build -t mysql-test . I get the following error:

Step 7 : RUN mysqladmin -u root create mydb
 ---> Running in a35c3d176d4f
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
The command '/bin/sh -c mysqladmin -u root create mydb' returned a non-zero code: 1

When I comment the line where I want to create the database (RUN mysqladmin -u root create mydb) I can build the image, start it, and execute the mysqladmin command from the command line:

# this works
> docker run -it mysql-test
root@...> service mysql start
root@...> mysqladmin -u root create mydb

Why do I get the error when creating the database in the Dockerfile? Thank you!

4
  • 1
    I believe this was addressed already. See here stackoverflow.com/questions/29145370/… Commented Jan 11, 2017 at 21:53
  • Possible duplicate of Docker - Initialize mysql database with schema Commented Jan 11, 2017 at 22:38
  • I tried to reformulate the question a bit: I do not see how this error is addressed in the other question Commented Jan 11, 2017 at 22:56
  • 2
    Docker is not a virtual machine. It does not have a functioning init system. Service mysql start is not the correct approach. If you want a process supervisor, you have to install/configure your own. Commented Jan 12, 2017 at 14:34

1 Answer 1

3

Reading the documentation of the RUN instruction makes it clear:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

So each run instruction creates a new image and starting the MySQL server and creating the database should be combined in the same RUN instruction:

RUN service mysql start && mysqladmin -u root create mydb

This solves it.

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

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.