Docker is a very convenient platform that allows you to prepare virtual environments in containers.
Virtual environments are prepared for each project or for each environment test, but if all of them are started, there will not be enough memory. Therefore, it is more likely to start them each time they are needed.
When docker-compose down, the data is also deleted because it is in the container.
This time, we will store the data locally so that it can be persisted.
The directory structure of this article is as follows.
.
├── README.md
├── docker-compose.yml
├── project
└── data
Store in Docker containers.
Save the data in a Docker container.
If you do this, the data will remain unless you delete the container.
If docker-compose down
is performed, the container data is deleted.
version: '3.3'
services:
wordpress:
image: wordpress:latest
container_name: wp
depends_on:
- db
ports:
- "9901:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- ./project:/var/www/html
db:
image: mysql:5.7
container_name: wp-mysql
ports:
- "4307:3306"
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
db_data:
Store MySQL data locally.
Keep MySQL data locally by mounting it in a local directory.
This means that even if you docker-compose down
and the container is deleted, the data will remain.
( Of course, if you delete the local data, the data will disappear. )
version: '3.3'
services:
wordpress:
image: wordpress:latest
container_name: wp
depends_on:
- db
ports:
- "9901:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- ./project:/var/www/html
db:
image: mysql:5.7
container_name: wp-mysql
ports:
- "4307:3306"
volumes:
- ./data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
db_data:
Summary
It is more convenient to keep data locally for those that are occasionally activated for maintenance support.