Home > Net >  Docker container is shut down immediately unless I change the `mount` `target`
Docker container is shut down immediately unless I change the `mount` `target`

Time:09-21

I'm trying to start a Docker container with the mysql image with a mount pointing to a single database directory (because I don't want all those other databases and log files in my project).

I tried these commands:

db=test

install -d db-data

docker run \
  -d \
  --rm \
  --mount src=$(pwd)/db-data,target=/var/lib/mysql/$db,type=bind \
  --name $db-db \
    -p 33060:3306 \
    -e MYSQL_ROOT_PASSWORD=root \
    -e MYSQL_DATABASE=$db \
mysql

When I do this, the container starts and terminates immediately. But if I use a target of target=/var/lib/mysql, only removing /$db, it works, but then this is what I'm trying to avoid.

CodePudding user response:

You must not have a mount below /var/lib/mysql in the container. You will have to create a mount for /var/lib/mysql.

This is the error you are facing:

0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
0 [ERROR] [MY-010119] [Server] Aborting
0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26)  MySQL Community Server - GPL.

If /var/lib/mysql is not fully populated at startup, the entrypoint will initialize the /var/lib/mysql directory. This will fail, if there are already files in it, which is the case when you mount a file/directory into the data directory.

CodePudding user response:

To be sure that all initialization down on the container, you can add volume after create container.

  1. create container without mount:
docker run \
  -d \
  --rm \
  --name $db-db \
    -p 33060:3306 \
    -e MYSQL_ROOT_PASSWORD=root \
    -e MYSQL_DATABASE=$db \
mysql
  1. commit your existing container (that is create a new image from container’s changes):
docker commit 5a8f89adeead newcontainername
  1. run the new container with volume:
docker run -ti --mount src=$(pwd)/db-data,target=/var/lib/mysql/$db,type=bind newcontainername /bin/bash
  1. start the new container (you can find it with docker ps -a) ans stop the old one (already running):
  • Related