Reputation: 16748
ocker 1.9 introduced a new volume API for creating data only containers (via docker volume create
).
Is it possible to create those and mount them via docker-compose?
I would like to create a data only container to store data from my MySQL database (https://hub.docker.com/_/mysql/). Sadly I couldn't find any documentation whether it is possible with docker-compose itself.
Is it possible as of Docker Compose 1.6.2. and if yes, how?
Upvotes: 1
Views: 2231
Reputation: 7100
if you want to cretae and store database data make docker-compose.yml will look like if you want to use Dockerfile
version: '3.1'
services:
php:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./src:/var/www/html/
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- mysql-data:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
mysql-data:
your docker-compose.yml will looks like if you want to use your image instead of Dockerfile
version: '3.1'
services:
php:
image: php:7.4-apache
ports:
- 80:80
volumes:
- ./src:/var/www/html/
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- mysql-data:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
if you want to store or preserve data of mysql then must remember to add two lines in your docker-compose.yml
volumes:
- mysql-data:/var/lib/mysql
and
volumes:
mysql-data:
after that use this command
docker-compose up -d
now your data will persistent and will not be deleted even after using this command
docker-compose down
extra:- but if you want to delete all data then you will use
docker-compose down -v
Upvotes: 0
Reputation: 1324935
Considering that compose/volume.py#L23 does include a volume.create()
method, it should be possible.
See the volume section of Compose file reference:
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Named volume
- datavolume:/var/lib/mysql
That would call docker volume create
.
Upvotes: 2