0

What I want to do:

  • Create a container that runs a mariadb server
  • Run some custom query before the container is built. (Not at the start up)

What I've done:

  1. I've created a docker-compose.yml file where I describe my service and for each component, I've created a dockerfile inserting the command to run. I obtain the shown error.
  2. I've added some sleep second. Nothing changed
  3. I've tried to create a second service depending on the database and run from there the queries. Nothing changed, still the same error.

Where is the problem?

I cannot execute any command until every service is built and running. In the following dockerfile:

FROM mariadb:latest
ARG MYSQL_ROOT_PASSWORD
RUN mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE test"

I get the following error:

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

Here is the docker-compose.yml file:

version: '3.1'

services:

  db:
    image: mariadb:latest
    build: 
      context: ./db
      args:
        MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
    container_name: IUB_sql
    restart: unless-stopped
    environment:
       MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
        MYSQL_DATABASE: "useless_test_db"
        MYSQL_USER: "${MYSQL_USER}"
        MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
    ports:
      - ${MYSQL_PORT}:3306

Note:

I do not want to create only a database, I want to run some arbitrary queries.

Note 2:

If I delete the RUN mysql ... line the service goes up. Then if I try to run the following command I'm able to run successfully the query: docker exec -umysql -it IUB_sql mysql -uroot -p -e "CREATE DATABASE test_cmdline"

Community
  • 1
  • 1
Timmy
  • 693
  • 2
  • 8
  • 26
  • 2
    [You can't create a `mysql` derived image with prepopulated data](https://stackoverflow.com/questions/32482780/how-to-create-populated-mysql-docker-image-on-build-time); the standard pattern is to put your SQL scripts in `/docker-entrypoint-initdb.d` and let them get run on first startup. Is there a specific reason you're trying to avoid this pattern? – David Maze Dec 28 '19 at 17:27
  • @DavidMaze Thank you, I didn' know about this pattern. I've tried to use it now but it seems to do not work with mariadb since this image doesn't have the file: `/entrypoint.sh`. Do you have any suggestion? – Timmy Dec 28 '19 at 18:10
  • I'm not sure what that specific file would be or why you'd need to reference it. There's a brief paragraph about the pattern in [the `mariadb` Docker Hub image page](https://hub.docker.com/_/mariadb) (under "Initializing a fresh instance"). – David Maze Dec 28 '19 at 20:26

1 Answers1

2

See "Initializing a fresh instance" in https://hub.docker.com/_/mariadb. You can add a folder with SQL files that will be run ONCE only. that can be done with docker-compose adding this configuration:

volumes:
  - /your-directory-with-sql-files:/docker-entrypoint-initdb.d

see https://github.com/docker-library/mariadb/blob/2345e98dc89edae8f11c35ad838887f45859de75/10.4/docker-entrypoint.sh#L314 for detail.

Diego Dupin
  • 1,106
  • 8
  • 9