3

I'm attempting to deploy a PosgreSQL Docker container in Azure. To that end, I created in Azure a storage account and a file share to store a Docker volume.

Also, I created the Docker Azure context and set it as default.

To create the volume, I run:

volume create volpostgres --storage-account mystorageaccount

I can verify that the volume was created with docker volume ls.

ID                            DESCRIPTION
mystorageaccount/volpostgres   Fileshare volpostgres in mystorageaccount storage account

But when I try to deploy with docker compose up, I get

could not find volume source "volpostgres"

This is the YAML file that does not work. How to fix it? how to point to the volume correctly?

version: '3.7'
services:
  postgres:
    image: postgres:13.1
    container_name: cont_postgres
    networks:
      db:
        ipv4_address: 22.225.124.121
    ports:
      - "5432:5432"
    environment:  
      POSTGRES_PASSWORD: xxxxx
    volumes:
      - volpostgres:/var/lib/postgresql/data
networks:
   db:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 22.225.124.121/24
volumes:
  volpostgres:
     name: mystorageaccount/volpostgres
ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

6

You can follow the steps here. And the volumes part in the docker-compose file needs to be changed into this:

volumes:
  volpostgres:
    driver: azure_file
    driver_opts:
     share_name: myfileshare
     storage_account_name: mystorageaccount
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • `docker compose up` worked, however I'm getting this error in the Azure log. How to define the owner? `2020-11-24 03:17:42.236 UTC [84] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership 2020-11-24 03:17:42.236 UTC [84] HINT: The server must be started by the user that owns the data directory.` – ps0604 Nov 24 '20 at 03:22
  • @ps0604 This is a known issue for mounting Azure File Share. The mount was executed by root and you cannot change the ownership. So you need to mount the file share to another point and move the data manually. – Charles Xu Nov 24 '20 at 06:01
  • @ps0604 It means you need to create a script to move the data. The mount point should be a new folder which does not exist in the image. – Charles Xu Nov 24 '20 at 08:23
  • Charles, can you take a look at [this](https://stackoverflow.com/questions/64993171/copy-file-to-docker-volume-in-azure-context)? Thanks! – ps0604 Nov 25 '20 at 07:50
  • @ps0604 I will take a look. – Charles Xu Nov 25 '20 at 07:51
  • Charles, I ended up using PostgreSQL as an Azure service. That worked, but it is too expensive, so I need to install the database in a container. Basically, I'm going back to the original idea. When you say `It means you need to create a script to move the data. The mount point should be a new folder which does not exist in the image.` what is this script and how it is run? – ps0604 Dec 14 '20 at 14:51
  • @ps0604 It's a script that you use to move the data to the mount point, you need to create it yourself as you need and run it inside the container. just like you run the shell script. – Charles Xu Dec 15 '20 at 02:01
  • Move what data? From where to where? – ps0604 Dec 15 '20 at 02:37
  • @ps0604 The data you want to persist, such as the data in `/var/lib/postgresql/data`. – Charles Xu Dec 15 '20 at 02:38
  • IF I understand correctly, you are saying to map the volume to a directory that is NOT the postgres data directory. Then on container startup move the data from the volume directory to the postgres data directory. Then when the container stops move back from postgres data directory to the volume directory. If this is true then it's dangerous, what if there's a crash? you will lose all the data that was changed since startup – ps0604 Dec 25 '20 at 15:10
  • @ps0604 I mean you can use a script to start all the things you need. Crash means there is something wrong you do in the script. – Charles Xu Dec 28 '20 at 01:47
  • I'll also point out that the example... – Ed Greenberg May 13 '22 at 12:28