1

I have the following yml file:

version: '2'
services:
    # another container

    db:
        build: ./path/to/dockerfile/
        environment:
            MYSQL_ROOT_PASSWORD: mypassword
            MYSQL_DATABASE: database_name
            MYSQL_USER: username
            MYSQL_PASSWORD: mypassword
        volumes:
            - ./Dump.sql:/db/Dump.sql:z
            - ./Dump_Test.sql:/db/Dump_Test.sql:z
            - ./big_fc.sql:/db/big_fc.sql:z
        ports:
            - "3306:3306"

and this is the Docker file:

FROM mysql:latest
LABEL maintainer "some@email.com"
RUN apt-get update -y
COPY ./solution.txt /temp/solution.txt
WORKDIR /temp
RUN cat solution.txt >> /etc/mysql/conf.d/mysql.cnf
RUN mysql -u username -pmypassword database_name -e "SOURCE /db/Dump.sql;"

However, when I run docker-compose up -d --build, I am getting the following error:

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

I have tried this, and this does not work, I am still getting the same error. What I don't understand is that if I omit the last line of the Dockerfile, and run this instead:

docker-compose exec db mysql -u username -pmypassword database_name -e "SOURCE /db/Dump.sql;" 

It works properly, but I would like to avoid running this line separately.

Edit: the following is my Dockerfile

FROM mysql:latest
LABEL maintainer "some@email.com"
RUN apt-get update -y
COPY ./solution.txt /temp/solution.txt
WORKDIR /temp
RUN cat solution.txt >> /etc/mysql/conf.d/mysql.cnf

In case you wonder the contents of solution.txt:

[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Community
  • 1
  • 1
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228

1 Answers1

1

Docker has 2 phase:

  1. build image
  2. create and run container from built image

The issue is you try to restore database on the build phase. Lets try to move it to second phase. To achive this you can use database initialization buit in script.

Initializing a fresh instance When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

You can read more on the mysql docker image official page

Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
  • I see the problem now, and could it be possible to have the lines in the Dockerfile to make this. I tried an approach using `entrypoint-initdb.d`, but it didn't work. – lmiguelvargasf May 13 '17 at 23:27
  • I tried in the morning but it was something as no file found. I will try again this approach when completing a couple of tasks. – lmiguelvargasf May 13 '17 at 23:31