0

I am attempting to create a docker container using the mysql:5 docker image. Once the MySQL server is up and running I want to create some databases, users and tables.

My Dockerfile looks like this;

FROM mysql:5

# Add db.data with correct permissions
RUN mkdir /server_data
WORKDIR /server_data
ADD --chown="root:root" ./db.data .

# Copy setup directory
COPY ./setup setup
COPY ./config /etc/mysql/conf.d

CMD ["./setup/setup.sh", "mysql", "-u", "root", "<", "./setup/schema.sql"]

My ./setup/setup.sh script looks like this;

#!/bin/bash
# wait-for-mysql.sh

set -e

shift
cmd="$@"

until mysql -uroot -c '\q'; do
  >&2 echo "mysql is unavailable - sleeping"
  sleep 1
done

>&2 echo "mysql is up - executing command"
exec $cmd

My docker-compose.yml looks like this;

version: "3"

services:
  db:
    build: ./db

    volumes:
      - data-db:/var/lib/mysql

    environment:
      - MYSQL_ROOT_PASSWORD=password

    restart: always

    container_name: db

volumes:
  data-db:

When I run 'docker-compose up --build' I get the following output;

Building db
Step 1/7 : FROM mysql:5
---> 0d16d0a97dd1
Step 2/7 : RUN mkdir /server_data
---> Using cache
---> 087b5ded3a53
Step 3/7 : WORKDIR /server_data
---> Using cache
---> 5a32ea1b0a49
Step 4/7 : ADD --chown="root:root" ./db.data .
---> Using cache
---> 5d453c52a9f1
Step 5/7 : COPY ./setup setup
---> 9c5359818748
Step 6/7 : COPY ./config /etc/mysql/conf.d
---> b663a380813f
Step 7/7 : CMD ["./setup/setup.sh", "mysql", "-u", "root", "<", "./setup/schema.sql"]
---> Running in 4535b2620141
Removing intermediate container 4535b2620141
---> 2d2fb7e308ad
Successfully built 2d2fb7e308ad
Successfully tagged wasdbsandbox_db:latest
Recreating db ... done
Attaching to db
db | ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) db | mysql is unavailable - sleeping
db | ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) db | mysql is unavailable - sleeping

This goes on interminably until I press Ctrl + c.

If I comment out the CMD line in my Dockerfile the output from running 'docker-compose up --build' is the output of the ENTRYPOINT command that is defined in the official mysql Dockerfile.

Why is mysql never starting when I use my own CMD command?

1 Answer 1

2

This is supported already by the official mysql image. No need to make your own custom solution.

Look at the Docker Hub README under "Initializing a fresh instance".

You can see in the official image under the 5.7 Dockerfile (for example) that it copies in a ENTRYPOINT script. That script doesn't run at build time, but at run-time right before the CMD starts the daemon.

In that existing ENTRYPOINT script you'll see that it will process any files you put in /docker-entrypoint-initdb.d/

So in short, when you start a new container from that existing official image it will:

  1. start the mysqld in local-only mode
  2. creates default user, db, pw, etc.
  3. runs any scripts you put in /docker-entrypoint-initdb.d/
  4. stops the mysqld and hands off to the Dockerfile CMD
  5. CMD will run the mysqld to listen on the network
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.