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!

Michael
  • 4,722
  • 6
  • 37
  • 58
  • 1
    I believe this was addressed already. See here http://stackoverflow.com/questions/29145370/docker-initialize-mysql-database-with-schema – Nabil Jan 11 '17 at 21:53
  • Possible duplicate of [Docker - Initialize mysql database with schema](http://stackoverflow.com/questions/29145370/docker-initialize-mysql-database-with-schema) – Mark O'Connor Jan 11 '17 at 22:38
  • I tried to reformulate the question a bit: I do not see how this error is addressed in the other question – Michael Jan 11 '17 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. – user2105103 Jan 12 '17 at 14:34

1 Answers1

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.

Michael
  • 4,722
  • 6
  • 37
  • 58