简体   繁体   中英

Use directory in docker-compose.yml's parent folder as volume

I have the following directory structure:

.
├── README.md
├── alice
├── docker
│   ├── compose-prod.yml
│   ├── compose-stage.yml
│   ├── compose.yml
│   └── dockerfiles
├── gauntlet
├── nexus
│   ├── Procfile
│   ├── README.md
│   ├── VERSION.txt
│   ├── alembic
│   ├── alembic.ini
│   ├── app
│   ├── poetry.lock
│   ├── pyproject.toml
│   └── scripts

nexus.Dockerfile

FROM python:3.10

RUN addgroup --system app && adduser --system --group app
WORKDIR /usr/src/pdn/nexus

COPY ../../nexus/pyproject.toml ../../nexus/poetry.lock* ./

ARG INSTALL_DEV=true
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"

COPY ../../nexus .

RUN chmod +x scripts/run.sh
ENV PYTHONPATH=/usr/src/pdn/nexus
RUN chown -R app:app $HOME
USER app

CMD ["./run.sh"]

The relevant service in compose.yml looks like this:


services:
  nexus:
    platform: linux/arm64
    build:
      context: ../
      dockerfile: ./docker/dockerfiles/nexus.Dockerfile

    container_name: nexus
    restart: on-failure
    ports:
      - "8000:8000"
    volumes:
      - ../nexus:/usr/src/pdn/nexus:ro
    environment:
      - DATABASE_HOSTNAME=${DATABASE_HOSTNAME?}
    env_file:
      - .env

When I run compose up, I get the following error:

Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "./scripts/run.sh": permission denied: unknown

The service starts ok without the volume definition. I think it might be because of the the location of nexus in relation to the dockerfile or compose file, but the context is set to the parent.

I tried defining the volume as follows:

volumes:
      - ./nexus:/usr/src/pdn/nexus:ro

But I get a similar error, in this case run.sh is not found: and a directory named nexus gets created in the docker directory

Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "./run.sh": stat ./run.sh: no such file or directory: unknown

Not sure what I'm missing.

I've two comments, not sure if they can solve your issue.

First although, in your compose.yml, your are allowed to reference your parent directories, that not the case in your Dockerfile, you can't copy from outside the context which you specified in your compose.yml file (.. which resolve to your app root). So you should change those lines:

COPY ../../nexus/pyproject.toml ../../nexus/poetry.lock* ./
COPY ../../nexus .

to

COPY ./nexus/pyproject.toml ./nexus/poetry.lock* ./
COPY ./nexus .

Second the volume overrides whatever in /usr/src/pdn/nexus by the content of../nexus. This will render your whole copies, to /usr/src/pdn/nexus, useless. That may not be an issue if the contents are the same, but whatever permission you defined in your files may be gone. So if your contents are the same, the only issue you may have is your starting script, you can put it into a separate directory out of the /usr/src/pdn/nexus so that it won't be overridden, and don't forget to reference it correctly into the CMD.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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