Ole
Ole

Reputation: 46940

Docker compose not mounting volume?

If I run this command the volume mounts and the container starts as expected with initialized state:

docker run --name gogs --net mk1net --ip 203.0.113.3 -v gogs-data:/data -d gogs/gogs

However if I run the corresponding docker-compose script the volume does not mount. The container still starts up, but without the state it reads on startup.

  version: '3'
  services:
    gogs:
      image: gogs/gogs
      ports:
        - "3000:3000"
      volumes: 
        - gogs-data:/data
      networks: 
        mk1net:
          ipv4_address: 203.0.113.3
  volumes:
    gogs-data:
  networks:
    mk1net:
      ipam:
        config:
          - subnet: 203.0.113.0/24

Any ideas?

Upvotes: 0

Views: 3851

Answers (1)

tato
tato

Reputation: 46

Looking at your command, the gogs-data volume was defined outside the docker compose file, probably using something like:

docker volume create gogs-data

If so then you need to specify it as external inside your docker compose file like this:

  volumes:
    gogs-data:
      external: true

You can also define a different name for your external volume and keep using current volume name inside your docker compose file to avoid naming conflicts, like for example, let's say your project is about selling cars so you want the external volume to be call selling-cars-gogs-data but want to keep it simple as gogs-data inside your docker compose file, then you can do this:

  volumes:
    gogs-data:
      external:
        name: selling-cars-gogs-data

Or even better using environment variable to set the volume name for a more dynamic docker compose design, like this:

  volumes:
    gogs-data:
      external:
        name: "${MY_GOGS_DATA_VOLUME}"

And then start your docker compose like this:

env MY_GOGS_DATA_VOLUME='selling-cars-gogs-data' docker-compose up

Hope this helps, here is also a link to the docker compose external volumes documentation in case you want to learn more: https://docs.docker.com/compose/compose-file/#external

You can make pretty much everything external, including container linking to connect to other docker compose containers.

Upvotes: 1

Related Questions