Docker compose Invalid volume destination path: '.' mount path must be absolute
Asked Answered
C

7

20

Following is my dockerfile that works to run the H2 Database: I want to create a docker compose file for this.

FROM klousiaj/oracle-java:7.79
MAINTAINER J.P. Klousia <klousiaj>

ENV DOWNLOAD http://www.h2database.com/h2-2016-10-31.zip
ENV DATA_DIR /opt/h2-data

RUN curl ${DOWNLOAD} -o h2.zip \
    && unzip h2.zip -d /opt/ \
    && rm h2.zip \
    && mkdir -p ${DATA_DIR}

EXPOSE 8082 9092

CMD java -cp /opt/h2/bin/h2*.jar org.h2.tools.Server \
    -web -webAllowOthers -webPort 8082 \
    -tcp -tcpAllowOthers -tcpPort 9092 \
    -baseDir ${DATA_DIR}

VOLUME ${DATA_DIR}

Following is the docker compose i am trying to perform:

version: '2'


services:
    db:
        image: klousiaj/oracle-java:7.79
        environment: 
            DOWNLOAD: http://www.h2database.com/h2-2016-10-31.zip
            DATA_DIR: /opt/h2-data
        command: curl ${DOWNLOAD} -o h2.zip \ && unzip h2.zip -d /opt/ \ && rm h2.zip \ && mkdir -p ${DATA_DIR}
        expose: 
            - "8082-9092"
        command: java -cp /opt/h2/bin/h2*.jar org.h2.tools.Server \ -web -webAllowOthers -webPort 8082 \ -tcp -tcpAllowOthers -tcpPort 9092 \ -baseDir ${DATA_DIR}
        volumes: 
            - ${DATA_DIR}

Im getting error as :

ERROR: for db Cannot create container for service db: Invalid volume spec ".": Invalid volume destination path: '.' mount path must be absolute.

Coughlin answered 3/1, 2017 at 14:20 Comment(3)
This is not going to work - you can't have multiple command entries per service. You probably want to keep using your Dockerfile and then refer to that using Docker Compose's build option: docs.docker.com/compose/compose-file/#/buildPhiloprogenitive
@Philoprogenitive can you tell me how i can transform the h2 dockerfile to docker compose ?Coughlin
docker-compose is not a replacement to docker, but to run docker image that build by docker. you need to build it first with docker.Benempt
S
18

This is just not allowed in the Compose file, since you do not have a template engine there.

You will not need to define

volumes: 
        - /opt/h2-data

Since that will be done automatically (anonymous volume). If you want to have a named volume use

volumes: 
        - myname:/opt/h2-data

or a host mount

volumes: 
        - /path/on/the/host:/opt/h2-data

So ${DATA_DIR} is not expanded in the volumes ( from the ENV ) in a compose file. There are dialects like rancher-compose providing this, but in general that is not possible

UPDATED: Updated my answer since I somehow mixed the Dockerfile/docker-compose.yml file. It makes sense in the Dockerfile, since it is just used as a variable. Thank you for hinting me on that @Bmitch (once again)

Seabrook answered 3/1, 2017 at 14:43 Comment(3)
Variable expansion should be allowed for the Dockerfile VOLUME command. But for the compose file, you're spot on. Ref: docs.docker.com/engine/reference/builder/…Laticialaticiferous
Ok thanks, i have removed the volumes but it says could not find or load main class org.h2.tools.Server in the docker compose, it is working in dockerFile but why its not accepting the library in docker compose ?Coughlin
@ShanKhanlook at my answer, i corrected it due to BMitch advice, i just horribly mixed it up. What you want it not offered by docker-compose, only by dialects like rancher-compose, see docs.rancher.com/rancher/v1.3/en/cattle/rancher-compose/…Seabrook
B
12

I had the same error message, it was a silly mistake. I forgot to put the leading forward slash on container directory /app

Before

volumes:
      - ./api/app:app

After

volumes:
      - ./api/app:/app
Brittaneybrittani answered 19/11, 2020 at 14:50 Comment(0)
S
10

Remove the . before app Change this

volumes:
      - ./api:/app
      - ./app/node_modules

to

volumes:
      - ./api:/app
      - /app/node_modules

Because the /app or whatever you named your working directory in created in /.

Sew answered 19/5, 2023 at 4:3 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Theine
F
4

Sometimes just an extra space can cause the problem (my case): Shouldn't be like that:

volumes:
      - ./api/app: /app

but :

volumes:
      - ./api/app:/app
Floater answered 17/8, 2021 at 13:10 Comment(0)
U
2

I got the same problem and inside my docker-compose.yaml I got the code inside the volumes which is given below:
---Before(I got the error message in the terminal)---

volumes:
      - ./api:/app
      - ./app/node_modules

---After(The problem is fixed)---

volumes:
      - ./api:/app
      - /app/node_modules
Unbelieving answered 13/6, 2023 at 14:20 Comment(0)
K
0

My error was that the path had a colon as part of it.
To make it work, I enclosed the path with single quotes:

volumes:
  - type: bind
    source: './path-with-colon:2.4.10-server/bundle'
    target: '/usr/local/bundle'
Karlakarlan answered 9/8, 2023 at 5:21 Comment(0)
C
-1

The error i encountered indicates that the mount path for the volume in the Docker configuration is incorrect. The mount path must be an absolute path. Here’s how you can fix it: step1:Edit the docker-compose.yml File: step2:Update the mount path to be absolute

for avoiding any hustle Restart Docker Compose

Chicago answered 6/8, 2024 at 22:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.