I'm trying to create a volume for my MySQL container so the data can persist. So in my docker-compose, I created a volume in which the data will be stored.
Here's my docker-compose :
version: '3'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: portfolio
ports:
- 80:80
volumes:
- .:/var/www/html
links:
- mysql
environment:
DB_HOST: mysql
DB_DATABASE: blog
DB_USERNAME: blog
DB_PASSWORD: ...
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: blog
MYSQL_USER: blog
MYSQL_PASSWORD: ...
MYSQL_ROOT_PASSWORD: ...
volumes:
- "./db:/var/lib/mysql"
The volume is defined at the end of the file. Unfortunately, that's not working. If I do a docker container ls it only shows the portfolio container, and if I do a docker container ls -a it does show my the container for MySQL, but it says it has "exited" right after I did the docker-compose up -d, so apparently it was started but then immediately crashed or something.
If I remove the last 2 lines related to the MySQL volume, it works fine. My containers are both up and the app works normally. So basically what causes the container to crash is the volume, so maybe I didn't define it correctly. What do you think ?