1

I'm unable to mount a host directory (on a rasberry pi) to a docker container api_service. Even with host 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. But I'd like to achieve that with docker-compose.

I tried different syntaxes into the docker-compose.yaml file but never achieving it. Every time removing all containers, images, then docker-compose build and docker-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

#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

FROM postgres:latest LABEL maintainer="[email protected]" RUN mkdir -p /docker-entrypoint-initdb.d COPY yarmp-dump.sql /docker-entrypoint-initdb.d/ 

api_service/Dockerfile

FROM arm32v7/adoptopenjdk LABEL maintainer="[email protected]" 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"] 
5
  • 1
    Some of your yaml indentation doesn't look right to me. Is the line after -type: bind indented correctly? Is this just an issue with the SO post or is this how it's indented when running it? Commented May 11, 2021 at 0:19
  • When it doesn't work, what does happen? If you've docker inspected a working non-Compose setup, how is the Compose-based installation different? Docker will automatically create the target directory if it doesn't exist (and, for that matter, the host directory too) and you don't need to explicitly RUN mkdir it. Commented May 11, 2021 at 1:26
  • @xdhmoore should source and target be indented 1 more to the right? seemed to work as same tab level with ports... Commented May 11, 2021 at 1:49
  • #DavidMaze the container is running, all is working as expected with docker-compose but the shared directories are not present output of docker inspect is "Mounts": [], Commented May 11, 2021 at 12:53
  • @Pipo, yes, I think type, source, and target should be aligned? The idea is - defines an item in a list, but all those are keys in that item. The trick with YAML is if you indent too little or too much, you end up with adding to either the list's parent or adding to the previous line's value instead of creating a new key-value. Commented May 11, 2021 at 18:56

2 Answers 2

1

Seems my entire docker-compose.yaml file was not correct.

As pointed out by @xdhmoore there was an indentation issue, and others.

I figured out by:

  1. validating the docker-compose.yaml with docker-compose config
  2. Tabs are NOT permitted by the YAML specs, USE ONLY SPACES FOR INDENTATION
  3. Note that vim default configuration file /usr/share/vim/vim81/ftplugin/yaml.vim was right replacing tabs with spaces...
  4. The indentation of long syntax was done on my editor with tabs when 2 spaces before were working. Here my final 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: 
Sign up to request clarification or add additional context in comments.

1 Comment

I think in your volumes, your source needs to be aligned with the t in type. I think type and source should end up as keys in the same object. I'm using convertjson.com/yaml-to-json.htm to look at it. Right now it seems to interpreting it as type: "bind\nsource:" etc., like bind is the beginning of a multiline string
1

This is based on the YAML in your answer. When I plug it into this yaml to json converter, I get:

{ "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". It appears that YAML is interpreting the source line here as the 2nd line of a multiline string. However, if you adjust the indentation to line up with the t in - type, you end up with:

... "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. It also helps me to think about YAML in terms of lists and objects and strings. Here - creates a new item in a list, and type: bind is a key-value in that item (not in the list). 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. 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.

1 Comment

Many thanks, the formatting is strange on stackoverflow, my archive is working...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.