Define Docker Volumes in Docker Compose
The mysql image README on dockerhub describes how to persist database content in a docker volume.
For this purpose, we will create a docker volume to store the contents of the container directory /var/lib/mysql
.
First, we will define our volume globally in the docker-compose file
volumes:
db-volume:
Next, we will mount that volume into the the my-db container
mydb:
volumes:
- db-volume:/var/lib/mysql
See the following file
Start the service
Start the server components
docker-compose \
-f examples/session2/servers-with-volume.yml \
up -d
Note that a volume is created in addition to the network and the containers
Creating network "session2_mynet" with the default driver
Creating volume "session2_db-volume" with default driver
Creating mydb ... done
Creating rubyserver ... done
Verify that the server initialization is complete
docker-compose \
-f examples/session2/servers-with-volume.yml \
logs -f
View the contents of the database from the webserver. Note that the list is empty.
Now run the loader to load the simpsons.csv
docker-compose \
-f examples/session2/servers-with-volume.yml \
-f examples/session2/loader.yml \
run data-load simpsons.csv
View the contents of the database from the webserver. Note that 5 users exist.
Bring down the stack
docker-compose \
-f examples/session2/servers-with-volume.yml \
down
Note that the containers and the network are deleted.
Stopping rubyserver ... done
Stopping mydb ... done
Removing rubyserver ... done
Removing mydb ... done
Removing network session2_mynet
Note that the volume still exists
docker volume ls | grep 'db-volume'
Note that the volume name is prefixed with “session2”. That prefix is known as the project name. By default, the name of the folder containing the docker-compose file is used as a project name.
local session2_db-volume
Recreate the stack
docker-compose \
-f examples/session2/servers-with-volume.yml \
up -d
View the contents of the database from the webserver. Note that 5 users exist.
Clean up resources
Stop the stack
docker-compose \
-f examples/session2/servers-with-volume.yml \
down
Delete the volume as well
docker volume rm session2_db-volume
In the next section, we will experiment with different project settings using this docker-compose file.