简体   繁体   中英

Run mysql instance in Docker container

I want to run a mysql container in Docker. The Dockerfile that I use is the Dockerfile defined in the official repo [here] . I only extended this Dockerfile with 2 more lines so I can import a init sql file, like this :

    ADD my-init-file.sql /my-init-file.sql 
    CMD ["mysqld", "--init-file=/my-init-file.sql"]

I want to run this instance as a daemon but when I execute this command, from the documentation :

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql

The container exits automatically. I want to run it as a daemon so I can link apps(like a wordpress site) in another container to the mysql database.

Maybe I am missing something. Can anyone show me how ?

[EDIT] I forgot to say that I ran docker logs my-container after starting the container and there is no error :

Running mysql_install_db ...
Finished mysql_install_db

docker ps shows no running container. My guess is the command executes successfully but the mysqld daemon does not start.

Your Dockerfile seems fine. Your init file may be buggy, though. If MySQL terminates, then the container will terminate.

The first debug step is to look at the logs:

docker logs some-mysql

You can use this whether the container is stopped or running. Hopefully, you'll see something obvious, like you missed some semicolons.

If the logs don't help, the next thing to try is to get inside the container and see what's happening first-hand

docker run -e MYSQL_ROOT_PASSWORD=mysecretpassword -it mysql /bin/bash

This will get you a Bash shell inside your container. Then you can run

mysqld --init-file=/my-init-file.sql

And see what happens. Maybe something in your init file tells MySQL to exit cleanly, so you get no logs but the command terminates.

Dmitri, after you made docker run with -d argument your container detached and already working as daemon if only CMD command not returned exit code.

You can check running containers by docker ps command. You can check all containers by running docker ps -a .

Also i think you will need to open mysql port outside the container. You can do it with -P argument or better way to make communication between containers is docker links .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM