I’m new with docker. I have some issues when I run an image from docker-compose.
It’s a very simple thing but it’s given me headaches.
When I run the image with docker run, any problem. It works.
But when I run it with docker-compose, it not create the database I specified in environment vars.
The reason why I run mysql in docker-compose is because the final result I expetect to have is an WordPress + MySQL + PHPMyAdmin environment. For now I’m trying to setup db first.
For test it, I assigned 33061 port to docker-compose runned container, and 33062 to ‘docker run’ runned container
.
This is the command that works fine:
docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=testdb -e MYSQL_USER=user -e MYSQL_PASSWORD=password -p 33062:3306 mysql:9.0.1
This is the docker-compose.yml content:
services:
db:
image: mysql:9.0.1
ports:
- 33061:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: testdb
MYSQL_USER: user
MYSQL_PASSWORD: password
In the first case, it creates the testdb database, but in the second case it not creates the database, like you can appreciate in the following picture:
Could someone explain to me why this might be happening?
Okey. I find why this happening.
It’s not any failure.
The first time I executed docker compose up
I don’t specify environment variables.
The following times that I executed the same command docker was retrieving data from the previous containers, and not recreating the own volume.
To solve this issue is required to add -V, --renew-anon-volumes
flag, that recreate anonymous volumes instead of retrieving data from the previous containers like is specified in Options section of docker compose up documentation
It works executing docker compose run -V
Explanatory note:
I was running docker compose up
in not detached mode for testing. When I press Ctrl+C
to stop the containers, it internally is executing docker compose stop
, this preserve anonymous volumes, which are attached again to the futures container builded again. This is why I’m having this issue. For this case executing the docker compose up
command with -V
flag works for me.
Another way to solve this issue is executing docker compose down
. It removes volumes, containers, images and networks. When we execute docker compose up
it will rebuild all containers with new volumes, avoiding this issue.