简体   繁体   English

docker 无法使用 docker-compose 将主机目录安装到 docker 容器

[英]docker unable to mount host directory to docker container with docker-compose

I'm unable to mount a host directory (on a rasberry pi) to a docker container api_service .我无法将主机目录(在 rasberry pi 上)挂载到 docker 容器api_service Even with host chmod -R 777 .即使使用主机chmod -R 777

I was able to mount it running the api_service from commandline docker start --mount type=bind,src=/data/yarmp-data,target=/data/yarmp-data docker_api_service_1 and docker inspect containerId in this case the mount section was telling me the mount was done and inside the container it was the case.能够从命令行docker start --mount type=bind,src=/data/yarmp-data,target=/data/yarmp-data docker_api_service_1docker inspect containerId挂载它运行api_service我已经完成了安装,并且在容器内就是这种情况。 But I'd like to achieve that with docker-compose .但我想用docker-compose来实现。

I tried different syntaxes into the docker-compose.yaml file but never achieving it.我在docker-compose.yaml文件中尝试了不同的语法,但从未实现。 Every time removing all containers, images, then docker-compose build and docker-compose up .每次删除所有容器、图像,然后docker-compose builddocker-compose up

What am I missing?我错过了什么? is there a way to trace the mount options at startup of the container?有没有办法在容器启动时跟踪挂载选项? Should the target directory have been created into the target image before mounting it on docker-compose.yaml ?在将目标映像挂载到docker-compose.yaml之前,是否应该在目标映像中创建目标目录?

docker-compose.yaml docker-compose.yaml

#Doc: https://github.com/compose-spec/compose-spec/blob/master/spec.md
version: '3.2'
services:
        api_service:
                build: ./api_service
                restart: always
                ports:
                        - target: 8080
                        published: 8080
                depends_on:
                        - postgres_db
                links:
                        - postgres_db:yarmp-db-host # database is postgres_db hostname into this api_service
                volumes:
                        - type: bind 
                        source: $HOST/data/yarmp-data #Host with this version not working
                        source: /data/yarmp-data #Host absolute path not working
                        #source: ./mount-test #not working either
                        target: /data/yarmp-data
                        #- /data/yarmp-data:/data/yarmp-data # not working either
        postgres_db:
                build: ./postgres_db
                restart: always
                ports:
                        - target: 5432
                        published: 5432
                env_file:
                        - postgres_db/pg-db-database.env # configure postgres
                volumes:
                        - database-data:/var/lib/postgresql/data/ 

postgres_db/Dockerfile postgres_db/Dockerfile

FROM postgres:latest
LABEL maintainer="me@mail.com"

RUN mkdir -p /docker-entrypoint-initdb.d
COPY yarmp-dump.sql /docker-entrypoint-initdb.d/

api_service/Dockerfile api_service/Dockerfile

FROM arm32v7/adoptopenjdk
LABEL maintainer="me@mail.com"

RUN apt-get update
RUN apt-get -y install git curl vim
CMD ["/bin/bash"]


#csv files data
RUN mkdir -p /data/yarmp-data #Should I create it or not??

RUN mkdir -p /main-app
WORKDIR /main-app
# JAVA APP DATA
ADD my-api-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar","/main-app/app.jar"]

Seems my entire docker-compose.yaml file was not correct.似乎我的整个docker-compose.yaml文件不正确。

As pointed out by @xdhmoore there was an indentation issue , and others.正如@xdhmoore 所指出的,存在缩进问题等。

I figured out by:我想通了:

  1. validating the docker-compose.yaml with docker-compose config使用docker-compose config验证docker-compose.yaml
  2. Tabs are NOT permitted by the YAML specs, USE ONLY SPACES FOR INDENTATION YAML 规范不允许使用制表符,只能使用空格进行缩进
  3. Note that vim default configuration file /usr/share/vim/vim81/ftplugin/yaml.vim was right replacing tabs with spaces...请注意, vim默认配置文件/usr/share/vim/vim81/ftplugin/yaml.vim用空格替换制表符是正确的...
  4. The indentation of long syntax was done on my editor with tabs when 2 spaces before were working.长语法的缩进是在我的编辑器上使用制表符完成的,而之前的 2 个空格正在工作。 Here my final docker-compose.yaml这是我的最终 docker-compose.yaml

docker-compose.yaml docker-compose.yaml

version: '3.2'
services:
api_service:
    build: ./api_service
    restart: always
    ports:
    - target: 8080
        published: 8080 #2 spaces before published
    depends_on:
    - postgres_db
    links:
    - postgres_db:yarmp-db-host
    volumes:
    - type: bind 
        source: /data/yarmp-data #2 spaces before source, meaning same level as previous '- types:...' and add 2 spaces more
        target: /data/yarmp-data #2 spaces before target
postgres_db:
    build: ./postgres_db
    restart: always
    ports:
    - target: 5432
        published: 5432 #2 spaces before published
    env_file:
    - postgres_db/pg-db-database.env # configure postgres
    volumes:
    - database-data:/var/lib/postgresql/data/
volumes:
    database-data: 

This is based on the YAML in your answer.这是基于您的答案中的 YAML 。 When I plug it into this yaml to json converter , I get:当我将它插入这个 yaml 到 json 转换器时,我得到:

{
   "version": "3.2",
   "services": null,
   "api_service": {
      "build": "./api_service",
      "restart": "always",
      "ports": [
         {
            "target": "8080\npublished: 8080"
         }
      ],
      "depends_on": [
         "postgres_db"
      ],
      "links": [
         "postgres_db:yarmp-db-host"
      ],
      "volumes": [
         {
            "type": "bind\nsource: /data/yarmp-data"
         }
      ]
   },
   "postgres_db": {
      "build": "./postgres_db",
      "restart": "always",
      "ports": [
         {
            "target": "5432\npublished: 5432"
         }
      ],
      "env_file": [
         "postgres_db/pg-db-database.env"
      ],
      "volumes": [
         "database-data:/var/lib/postgresql/data/"
      ]
   },
   "volumes": {
      "database-data": null
   }
}

You can see several places where the result is something like "type": "bind\nsource: /data/yarmp-data" .您可以看到几个地方的结果类似于"type": "bind\nsource: /data/yarmp-data" It appears that YAML is interpreting the source line here as the 2nd line of a multiline string. YAML 似乎将此处的source行解释为多行字符串的第二行。 However, if you adjust the indentation to line up with the t in - type , you end up with:但是,如果您调整缩进以与t in - type对齐,您最终会得到:

...
      "volumes": [
         {
            "type": "bind",
            "source": "/data/yarmp-data",
            "target": "/data/yarmp-data"
         }
      ]
...

The indentation in YAML is tricky (and it matters), so I've found the above and similar tools helpful to get what I want. YAML 中的缩进很棘手(这很重要),所以我发现上述工具和类似工具有助于获得我想要的东西。 It also helps me to think about YAML in terms of lists and objects and strings.它还帮助我从列表、对象和字符串的角度考虑 YAML。 Here - creates a new item in a list, and type: bind is a key-value in that item (not in the list).这里-在列表中创建一个新项目,然后type: bind是该项目中的键值(不在列表中)。 Then source: blarg is also a key-value in the same item, so it makes sense that it should line up with the t in type .那么source: blarg也是同一个 item 中的 key-value,所以它应该与type中的t对齐是有道理的。 Indenting more indicates you are continuing a multiline string, and I think if you indented less (like aligning with - ), you would get an error or end up adding a key-value pair to one of the objects higher up the hierarchy.缩进更多表示您正在继续一个多行字符串,我认为如果您缩进更少(例如与-对齐),您会收到错误或最终将键值对添加到层次结构更高的对象之一。

Anyway, it's certainly confusing.无论如何,这肯定令人困惑。 I've found such online tools to be helpful.我发现这样的在线工具很有帮助。

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

相关问题 如何使用 docker-compose 将 Docker 目录挂载到主机目录中 - How to mount Docker directory into host directory with docker-compose 无法使用 docker-compose 将 docker 文件夹安装到主机中 - Unable to mount docker folder into host using docker-compose 使用 docker-compose 在 docker 容器中安装 Cifs - Cifs mount in docker container with docker-compose 如何使用 docker 组合将主机目录作为卷安装在 docker 容器中 - How to mount a host directory as volume in docker container using docker compose 无法将主机目录挂载到 docker 容器 - Unable to mount host directory to docker container docker-compose 只将主机上存在的文件挂载到容器 - docker-compose only mount files that exist on host to container 无法将文件从docker-compose mount复制到主机 - Unable to copy file from docker-compose mount to host 无法从 docker-compose 文件将本地目录挂载到容器中 - Cannot mount local directory into container from docker-compose file 如何使用docker-compose挂载主机目录,运行主机时指定“~/path/on/host”,不在docker-compose文件中 - How to mount a host directory with docker-compose, with the "~/path/on/host" to be specified when running the host, not in the docker-compose file docker-compose 挂载单个文件带目录 - docker-compose mount single file with directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM