Home > Database > Mysql Tutorial > How to Efficiently Set Up and Import a MySQL Dump within a Docker Container?

How to Efficiently Set Up and Import a MySQL Dump within a Docker Container?

Patricia Arquette
Release: 2024-12-03 22:28:14
Original
534 people have browsed it

How to Efficiently Set Up and Import a MySQL Dump within a Docker Container?

MySQL Setup and Dump Import within Dockerfile

Creating a Dockerfile to set up a MySQL database and import a dump is a common task in web development. However, you may encounter issues during MySQL startup.

The problem in the provided Dockerfile could lie in the missing initialization stage for the MySQL server. To resolve this, consider using the latest version of the official MySQL Docker image, which simplifies database creation and dump import during container startup.

Here's an example docker-compose.yml that demonstrates this approach:

data:
  build: docker/data/.
mysql:
  image: mysql
  ports:
    - "3307:3306"
  environment:
    MYSQL_ROOT_PASSWORD: 1234
  volumes:
    - ./docker/data:/docker-entrypoint-initdb.d
  volumes_from:
    - data
Copy after login

In this example, the data-dump.sql file is mounted to the /docker-entrypoint-initdb.d directory on the container. The Docker entrypoint script handles the import process upon container startup.

Additionally, if you wish to preserve data after stopping or removing the MySQL container, use a separate data container. The contents of the data container's Dockerfile can be simplified as follows:

FROM n3ziniuka5/ubuntu-oracle-jdk:14.04-JDK8

VOLUME /var/lib/mysql

CMD ["true"]
Copy after login

With this setup, the data is persisted regardless of the MySQL container's state.

The above is the detailed content of How to Efficiently Set Up and Import a MySQL Dump within a Docker Container?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template