简体   繁体   English

码头工人组成不装载卷?

[英]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. 但是,如果我运行相应的docker-compose脚本,则不会挂载该卷。 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? 有任何想法吗?

Looking at your command, the gogs-data volume was defined outside the docker compose file, probably using something like: 查看您的命令, gogs-data卷是在gogs-data compose文件外部定义的,可能使用类似以下内容的方法:

docker volume create gogs-data

If so then you need to specify it as external inside your docker compose file like this: 如果是这样,那么您需要像这样在docker compose文件中将其指定为外部:

  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: 您还可以为外部卷定义一个不同的名称,并在docker compose文件中继续使用当前卷名,以避免命名冲突,例如,假设您的项目是销售汽车,因此您希望将外部卷称为selling-cars-gogs-data但要使其简单化,如gogs-data compose文件中的gogs-data ,那么您可以执行以下操作:

  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: 甚至更好地使用环境变量为更动态的docker compose设计设置卷名,如下所示:

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

And then start your docker compose like this: 然后像这样启动您的Docker组合:

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 希望这会有所帮助,如果您想了解更多信息,这里也提供了指向docker compose外部卷文档的链接: 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. 您几乎可以将所有外部事物,包括用于链接到其他docker compose容器的容器链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM