Mount a Docker Volume With Docker Compose

Working with volumes when it comes to containers is probably essential for almost any application.

This post will show how to mount volumes with Docker-compose and connect them to applications.

In the example below taken from a docker-compose.yml file, we create a named volume called data in line number 5. The volume will map to the var/lib/mysql path on the actual container.

At line number 13, we declare the volume and make it available to other applications that are part of the docker-compose configuration.

Configuration

Below is the full compose file. The application is MySql 7.7 server.

services:
  backend:
    image: mysql:5.7
    volumes:
      - dbdata:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: set
      MYSQL_DATABASE: set
      MYSQL_USER: set
      MYSQL_PASSWORD: set
 
volumes: 
  dbdata:

To mount a Docker volume with a Dockerfile please visit this post.


Posted

in

by