1

I have two containers "web" and "db". I have an existing data file in csv format.

The problem is I can initialize the MySQL database with a schema using docker-compose or just run with parameters but how can I import the existing data? I have Python script to parse and filter the data and then insert it to db but I cannot run it in the "db" container due to the single image is MySQL.

Update1

version: '3'
services:
  web:
    container_name: web
    build: .
    restart: always
    links:
      - db
    ports:
      - "5000:5000"
  db:
    image: mysql
    container_name: db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_DATABASE: "test"
      MYSQL_USER: "test"
      MYSQL_PASSWORD: "test"
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    ports:
      - "33061:3306"

There is a Python script for read data from a csv file and insert them to database, which works fine. Now I want to running the script once the MySQL container is set up. (I have done connection with Python and MySQL in container)

Otherwise, anyone has a better solution to import existing data?

4
  • 2
    You could probably spin up a third, transient container just to run the import once. Share your docker-compose.yml and we can take it from there! Commented Mar 8, 2019 at 20:57
  • Do you have an existing Docker Compose setup you can share? This sounds like a pretty typical setup connecting a Python script to a database in a different container. Commented Mar 8, 2019 at 22:11
  • @Max I have succeeded in the same method as you suggest. But would that be a little be redundant? Since once the data imported, there is no need for the third container existing. Commented Mar 8, 2019 at 22:35
  • @DavidMaze I coonect them with Python script but I want to set up them with existing data. Commented Mar 8, 2019 at 22:36

2 Answers 2

2

MySQL docker image has the ability to execute shell scripts or sql files if these script/sql files mounted under /docker-entrypoint-initdb.d for a running container as described in here and here. So I suggest you to write an SQL file that reads the CSV file (which you should mount to your container so the sql file can read it) in order to restore it to MySQL maybe something similar to this answer or write a bash script to import csv into mysql whatever works for you.

You can check Initializing a fresh instance at the official dockerhub page for mysql

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

10 Comments

In your solution, it is not possible to modify the data in the container before importing to the database. I am really a fresh hand on Docker. Is it the best way to make the separation of data and application?
Can you elaborate more ? You said that you already have the data as a CSV right ?
Yep. I have a csv file which is in someone's github. I want to git clone it and modify some formats of the data before importing to the database like converting % to decimal.
So in this case you can add a bash script that clone the data and modify it then importing it into MySQL
Do you mean modify the data with bash?
|
0

From Dockerfile, you can call a script (Entrypoint). In this script you can call your python script. For example:

DockerFile:

FROM php:7.2-apache

RUN apt-get update    
COPY ./entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

This will run your entrypoint script in the App container. Make sure you've depends on attribute in you app container compose description.

1 Comment

I want to make separation of data and application. The data should be in the "db" container but I am looking for a method to import the data to MySQL.

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.