Use Read-Only Volume Docker Compose

When working with data and volumes within Docker and Docker Compose, there are use cases and situations where we need to make the data available to read-only operations.

This post will show you how to attach a volume to a Docker Compose application in a read-only mode.

To use Docker Compose, please check this post.

In the following configuration, We are mounting two volumes to the container (MySQL). The first volume (dbdata) gives the container read and write permissions to the container.

The second volume (readdata) gives the container read-only permission.

To check the configuration, copy-paste the code below, set the login details and run.

services:
  backend:
    image: mysql:5.7
    volumes:
      - type: volume
        source: dbdata
        target: /var/lib/mysql
      - type: volume
        source: readdata
        target: /data
        read_only: true
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: set
      MYSQL_DATABASE: wordpress
      MYSQL_USER: dadmin
      MYSQL_PASSWORD: set
 
volumes: 
  dbdata:
  readdata:
    

If I try to write something to the volume, I receive the following error.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.