Docker-Compose: Different Build Context Path and Dockerfile File Path

Sabbir Manandhar
2 min readAug 9, 2019

I was writing an application and I had to dockerize it.

I wrote a Dockerfile to build my application and run it. Things were OK when I ran it using docker command alone.

But I had to run the application using docker-compose. I wrote a docker-compose.yml file to build as well as run the application. Here I am going to show only a simple application for the demonstration. Following is my Dockerfile content.

FROM golang:alpineADD code/hello.go /code/
WORKDIR /
CMD [“go”, “run”, “code/hello.go”]

hello.go is simple echo program that will print hello world. Location of hello.go is /workspace/docker-helloworld/code and of Dockerfile is /workspace/docker-helloworld/docker.

workspace
|- docker-helloworld
|- code
|- hello.go
|- docker
|- Dockerfile

As you see, Dockerfile and my application are at different location. Build Context for my application will be /workspace/docker-helloworld while the location of Dockerfile is in different directory.

Fortunately, docker-compose allow us to define separate build context path and Dockerfile file path. Follow is definition of my docker-compose entry:

helloworld:
build:
context: /workspace/docker-helloworld
dockerfile: /workspace/docker-helloworld/docker/Dockerfile
image: hello-world

--

--