ddinchev
ddinchev

Reputation: 34693

How to set the "current" directory of a Dockerfile with Docker Compose?

I have the following project structure:

.
..
project/
docker/cli/Dockerfile
docker-compose.yml

In docker-compose.yml I have the following configuration:

cli:
  build: docker/cli

And somewhere in my Dockerfile:

COPY . /app

Now the problem is that when I do docker-compose build cli docker copies the contents of docker/cli/ in /app in my image. Which makes sense because that is the relative path to my docker/cli/Dockerfile. Is there a way however to tell in my docker-compose.yml config that the path should be different (namely the root dir of my project where the actual project files are)?

Upvotes: 11

Views: 25344

Answers (1)

johnharris85
johnharris85

Reputation: 18986

You can use the context attribute of a build instruction inside the docker-compose file.

version: '2'
services:
  cli:
    build: 
      context: .
      dockerfile: docker/cli/Dockerfile

This should work as desired. The context is what is sent to the docker daemon for build. You will also want a .dockerignore file in your project directory though, to ensure only what you need is sent.

Upvotes: 28

Related Questions