简体   繁体   English

如何使用docker-compose.yml文件命名卷?

[英]How to name a volume using a docker-compose.yml file?

I'm new to Docker and I'm trying to find out how to set the name of the created data volume. 我是Docker的新手,我正在尝试找出如何设置创建的数据卷的名称。 Currently the directory is automatically named as a long hash under /var/libs/docker which is far from user friendly. 目前,该目录在/ var / libs / docker下自动命名为长哈希,远非用户友好。

I'm attempting to set up a development environment for MODX as shown here: https://github.com/modxcms/docker-modx 我正在尝试为MODX设置开发环境,如下所示: https//github.com/modxcms/docker-modx

Currently my docker-compose.yml file is as follows: 目前我的docker-compose.yml文件如下:

web:
  image: modx
  links:
    - db:mysql
  ports:
    - 80:80
db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: example
  ports:
    - 3306:3306
  command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
  image: phpmyadmin/phpmyadmin
  links:
    - db:db
  ports:
    - 8080:8080

This works perfectly but I'm unsure as to how to name the data volume that I would edit directly with my IDE. 这很好用,但我不确定如何命名我将直接使用IDE编辑的数据量。

(As a side question, does it have to be created under /var/libs/docker ? Or is there a way of setting it to a directory in my home folder?) (作为一个附带问题,它是否必须在/ var / libs / docker下创建?或者有没有办法将它设置到我的主文件夹中的目录?)

Update: Thanks to the help from @juliano I've updated my docker-compose.yml file to: 更新:感谢@juliano的帮助我已将docker-compose.yml文件更新为:

version: '2'

services:
    web:
      image: modx
      volumes: 
        - html:/home/muzzstick/dev/modxdev
      links:
        - db:mysql
      ports:
        - 80:80
    db:
      image: mysql
      environment:
        MYSQL_ROOT_PASSWORD: example
      ports:
        - 3306:3306
      command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
    myadmin:
      image: phpmyadmin/phpmyadmin
      links:
        - db:db
      ports:
        - 8080:8080

volumes:
    html:
        external: false

Unfortunately this seems to stop the web container from running. 不幸的是,这似乎阻止了Web容器的运行。 db and myadmin containers show they're running ok. db和myadmin容器显示它们运行正常。 There weren't any errors... if I type docker start docker_web_1 it appears to start but docker ps -a shows it exited as soon as it started. 没有任何错误...如果我键入docker start docker_web_1它似乎开始但是docker start docker_web_1 docker ps -a显示它一开始就退出。

Update 2 更新2

Running docker-compose up -d appears to run without issue. 运行docker-compose up -d似乎运行没有问题。 But then as you can see below, the web container exits as soon as it's created. 但是,正如您在下面看到的那样,Web容器会在创建后立即退出。

    CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                     PORTS                            NAMES
a1dd6d8ac94e        modx                    "/entrypoint.sh apach"   10 seconds ago      Exited (1) 5 seconds ago                                    docker_web_1
ee812ae858dc        phpmyadmin/phpmyadmin   "/run.sh phpmyadmin"     10 seconds ago      Up 5 seconds               80/tcp, 0.0.0.0:8080->8080/tcp   docker_myadmin_1
db496134e0cf        mysql                   "docker-entrypoint.sh"   11 seconds ago      Up 10 seconds              0.0.0.0:3306->3306/tcp           docker_db_1

Update 3 OK the error logs for this container shows: 更新3确定此容器的错误日志显示:

error: missing MODX_DB_HOST and MYSQL_PORT_3306_TCP environment variables
Did you forget to --link some_mysql_container:mysql or set an external db
with -e MODX_DB_HOST=hostname:port?

This error appears to be originating from https://github.com/modxcms/docker-modx/blob/master/apache/docker-entrypoint.sh#L15-L20 此错误似乎源自https://github.com/modxcms/docker-modx/blob/master/apache/docker-entrypoint.sh#L15-L20

Could it be something like linking is handled differently in docker-compose version 2? 是不是像docker-compose版本2中的链接处理方式不同?

To create a named data volume using the version 2 of compose files you will have a separated area: 要使用撰写文件第2版创建命名数据卷您将有一个单独的区域:

version: '2'

services:
  db:
    image: postgres
    volumes:
      - amazingvolume:/var/lib/postgresql/data

volumes:
  amazingvolume:
    external: true

So you can define the volume name (amazingvolume), if it's external or not and under your service (db in this example) you can define which directory you gonna mount. 因此,您可以定义卷名称(amazingvolume),如果它是外部的或在您的服务下(在此示例中为db),您可以定义要安装的目录。

Just search in the docker documentation for hosted mounted volumes: 只需在docker文档中搜索托管安装卷:

version: '2'

services:
  web:
    image: modx
    environment:
      - MYSQL_PORT_3306_TCP=3306
      - MODX_DB_HOST=mysql:3306
    volumes: 
      - /home/muzzstick/dev/modxdev/html:/var/www/html

    links:
      - db:mysql
    ports:
      - 80:80
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    ports:
      - 3306:3306
    command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
  myadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - db:db
    ports:
      - 8080:8080

Change /var/www/html to the directory where the html files will be inside the container. /var/www/html更改为html文件将在容器内的目录。 And also create the directory at the left in your host and give read permission to all users. 并在主机左侧创建目录,并为所有用户授予读取权限。

Regards 问候

暂无
暂无

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

相关问题 在 docker-compose.yml 文件中为卷添加标签或名称 - Adding a label or name to volume in docker-compose.yml file 如何使用 docker-compose.yml 中的值将文件添加到 Docker 映像中的卷? - How can I add a file to a volume in a Docker image, using values from the docker-compose.yml? 如何通过在 docker-compose.yml 文件中定义卷来访问 docker 容器内生成的文件 - How to access the file generated inside docker container by defining the volume in docker-compose.yml file 错误:在文件“ ./docker-compose.yml”中,卷必须是映射,而不是数组 - ERROR: In file './docker-compose.yml', volume must be a mapping, not an array 在文件 './docker-compose.yml' 中,卷必须是映射,而不是数组 - In file './docker-compose.yml', volume must be a mapping, not an array 如何创建 docker-compose.yml 文件 - How To Create docker-compose.yml file MacOS-在文件'./docker-compose.yml'中,卷必须是映射,而不是数组 - MacOS - In file './docker-compose.yml', volume must be a mapping, not an array 如何在 docker-compose.yml 中为命名卷设置主机路径 - How to set a path on host for a named volume in docker-compose.yml 如何在docker-compose.yml中选择卷挂载点? - How to select volume mountpoint in docker-compose.yml? 如何使用 docker-compose.yml 在映射为卷的目录中克隆 GitHub 存储库? - How to clone a GitHub repo inside a directory mapped as a volume using docker-compose.yml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM