DEV Community

Discussion on: PHP + MySQL using Docker Compose

Collapse
 
valenzuelatalo profile image
Gonzalo Valenzuela

Thanks, Aly. I have a question: if the container is restarted, are the MySQL databases lost? In other words, is there persistence in the database?

Collapse
 
alysivji profile image
Aly Sivji • Edited

The setup I describe above stores data inside of the container. This data is lost when the container is deleted (docker-compose down)

You could mount a volume so the data is persisted on your local machine.

Looking at hub.docker.com/_/mysql/ (search for Where to Store Data), we can update the docker-compose.yml with another volume mount as follows:

# ./docker-compose.yml

version: '3'

services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_pw_shh
      MYSQL_DATABASE: test_db
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    volumes:
      - ./sql:/var/lib/mysql
    ports:
      - "9906:3306"
  web:
    image: php:7.2.2-apache
    container_name: php_web
    depends_on:
      - db
    volumes:
      - ./php/:/var/www/html/
    ports:
      - "8100:80"
    stdin_open: true
    tty: true

Now data will persist in the ./sql directory.

Collapse
 
valenzuelatalo profile image
Gonzalo Valenzuela

Thank you very much!