Boris van Katwijk
Boris van Katwijk

Reputation: 3188

Move docker setup into folder

I have a docker-compose setup something like:

/
- sources/
- docker-compose.yml
- Dockerfile
- .dockerignore
- many more files

The Dockerfile contains instructions including a COPY command of the sources.

Because of all the different tools, including multiple docker setups, I'd like to organise it a bit, by either moving all files to a folder:

/
- sources/
- docker/
- many more files

or leaving just the docker-compose.yml file outside of this folder:

/
- sources/
- docker/
- many more files

I'd like to do this because:

Currently I am running into some issues which do make sense, such as:

COPY failed: Forbidden path outside the build context: ../sources/

Is it possible to achieve this setup? Thanks!

Upvotes: 0

Views: 1434

Answers (2)

yamenk
yamenk

Reputation: 51866

Inside the Dockerfile, you cannot access files that are outside the build context. In your case the build context is the directory containing the Dockerfile.

You can change the build context inside the composefile. Below is an example where the composefile is at the root and Dockerfile is under docker folder:

version: '3'

services:
  test:
    build:
      context: .
      dockerfile: docker/Dockerfile

In this case, inside the Dockerfile the file paths should be set relative to the context.

COPY sources sources

For dockerignore:

As specified in the docs for .dockerignore file:

Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context

Thus you need to add the dockerignore file to the root of the context.

Upvotes: 4

Adoni
Adoni

Reputation: 146

You can't use that within the Dockerfile however you should be able to make it work using a .env file and pulling it in from there.

https://docs.docker.com/compose/env-file/

You could try something like:

.env

SOURCE_PATH=../sources/

Dockerfile

COPY ${SOURCE_PATH}/myfile /some/destination

Upvotes: 0

Related Questions