Hosting··5 min read

Import a Large SQL Dump Into a Docker MySQL Container

Three ways to load a big .sql file into a MySQL or MariaDB container — including the entrypoint trick that imports automatically on first run.

Loading a database into a containerised MySQL trips people up because the file lives on the host and MySQL lives in the container. Here are the three reliable patterns.

Method 1: Pipe the file into the container

The cleanest one-liner. Stream the dump from the host straight into mysql inside the container — no copying required:

docker exec -i my_mysql sh -c \
  'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" your_database' < dump.sql

The -i flag keeps STDIN open so the redirect works. Using the container's own $MYSQL_ROOT_PASSWORD env var avoids leaking the password into your shell history.

gunzip < dump.sql.gz | docker exec -i my_mysql mysql -uroot -psecret your_database — no need to unzip to disk first.

Method 2: Auto-import on first container start

The official mysql and mariadb images run any .sql, .sql.gz, or .sh file in /docker-entrypoint-initdb.d/ the first time the data directory is empty. Perfect for seeding a dev database:

services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: your_database
    volumes:
      - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql:ro
      - db_data:/var/lib/mysql
volumes:
  db_data:
The init scripts execute only when /var/lib/mysql is empty. If you already have a named volume with data, the dump is ignored. Run `docker compose down -v` to wipe the volume and re-seed.

Method 3: Copy in, then source

If you'd rather have the file inside the container (handy for re-running):

docker cp dump.sql my_mysql:/tmp/dump.sql
docker exec -it my_mysql mysql -uroot -psecret \
  -e "USE your_database; SOURCE /tmp/dump.sql;"

When the import is huge

  • Bump the container's packet limit: add --max_allowed_packet=1G to the command, or set it in a my.cnf mounted into /etc/mysql/conf.d/
  • Give Docker Desktop enough memory (Settings → Resources) or InnoDB will crawl
  • For multi-GB dumps, split the file so a failed import doesn't mean starting over

Split before you seed

Smaller chunks import faster into a container and make failures recoverable.

Open SQLSplit

Frequently Asked Questions

How do I import a SQL file into a running MySQL Docker container?

Pipe it in with docker exec -i: `docker exec -i container_name mysql -uroot -psecret dbname < dump.sql`. The -i flag keeps STDIN open so the file redirect reaches mysql inside the container.

Why is my docker-entrypoint-initdb.d script not running?

Those scripts only run when MySQL's data directory is empty — i.e. on a fresh volume. If the named volume already has data, the init scripts are skipped. Recreate the container with `docker compose down -v` to reset the volume.

How do I import a gzipped dump into Docker MySQL?

Decompress on the fly and pipe it in: `gunzip < dump.sql.gz | docker exec -i container mysql -uroot -psecret dbname`. No need to extract the file to disk first.

Related articles

View all

Advertisement