3

I want to load a csv into my mysql container.

This is my docker-compose:

version: '3.7'

services: 
  db:
    image: mysql
    ports:
      - 3306:3306
    environment: 
      - MYSQL_ROOT_PASSWORD=root123
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test123
    volumes:
      - './init.sql:/docker-entrypoint-initdb.d/init.sql'
      - './init_data:/docker-entrypoint-initdb.d/init_data'

It does create the table, but it doesn't load the csv into the table.

As far as I have researched I have to pass this as an argument to mysql image, but I dont know how to add an argument by docker-compose.

--secure-file-priv=docker-entrypoint-initdb.d

This is what I am following.

But I don't want to use docker run.

Any help or tips are welcomed.

1 Answer 1

1

You can not add an argument to the docker compose for building, because docker compose can not build images. More clearly it can, by calling a docker build (using the info you gave in docker-compose.yml), but the things what you call on building the image can be defined only in the Dockerfile. You can not give commands in the docker-compose.yml for building.

What you can do: you can use the mysql image as a base to create your own image - with its own Dockerfile. Roughly so:

services:
  mymysql:
    build:
      context: ./mymysql
    ...other things in your docker compose...

That will define a new container named mymysql, whose definition is in the ./mymysql directory. Create this directory and then put a Dockerfile into it. This Dockerfile should look like

FROM mysql:latest
USER mysql
RUN ...
RUN ...

In the RUN lines you can define the commands, what will be executed - on the pulled mysql:latest image and generate mymysql for you.


Alternative option: start the container and load the csv by running it. Then stop the container. After that a docker commit fce2ea31 mymysql will save the stopped container as a new image, in the name what you want. This is easier but much lesser reproducible, so I suggest to do the first.

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

4 Comments

I tried but I dont really know what to run in the docker file. If I try to run an sql loading the data, it will give me an error like this ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement And the error was the same using docker exec FROM mysql COPY ./init_data/ /docker-entrypoint-initdb.d/init_data/ COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
@JPcode Well that is another problem. Mysql in the container is configured to allow such file loads only from a fixed directory. This is what --secure-file-priv does. Best is to turn this off. To do that, you need to check the container and modify it to execute mysql without this argument.
@JPcode I do not know that specific container well, but I know mysql and also docker in general. The --secure-file-priv is coming either from my.cnf, or from a command line argument to the mysql. Find that config and override the settings: either by overwriting the config file with a COPY, or yet more better to extend the configuration. Alternatively, you can also override it by a command line argument to the mysqld (it is either called by a CMD or by some script in the container). The source (incl. Dockerfile) of the mysql:latest container would likely help a lot, and likely is available.
Thank you for your input. I used a command in the docker file RUN sed -i 's/secure-file-priv= NULL/secure-file-priv= docker-entrypoint-initdb.d/g' /etc/mysql/my.cnf and now it works! Thank you. (sorry it took me long) @peterh

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.