53

I am trying learn docker and created my first docker compose file. Basically I am trying to create a Django web microservice and use docker compose file to manage it. Here is my code for my docker-compose.yml:

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "py manage.py runserver 0.0.0.0:8000"

I don't understand the use of context: . Can someone please explain me this

Aryan Pandhare
  • 533
  • 1
  • 4
  • 5
  • [How to include files outside of Docker's build context?](https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context) includes some examples of cases where you will need to specify a different `context:` directory. – David Maze Jan 08 '21 at 03:03
  • Does this answer your question? [What is happening when using ../ with docker-compose volume](https://stackoverflow.com/questions/51049460/what-is-happening-when-using-with-docker-compose-volume) – NotTheDr01ds Jan 08 '21 at 03:13
  • Also, when providing no other attributes: you can provide the context to build directly: `build: .` – Chris Becke Jan 08 '21 at 08:06

3 Answers3

60

CONTEXT

Either a path to a directory containing a Dockerfile, or a url to a git repository.

When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file. This directory is also the build context that is sent to the Docker daemon.

Compose builds and tags it with a generated name, and uses that image thereafter.

build:
  context: ./dir

That mean, its a path to the directory that contains Dockerfile file. It purposes to build a new image for the service.

Reference: https://docs.docker.com/compose/compose-file/compose-file-v3/

In your case:

   context: .

Dockerfile file should be existing in the current directory (in the same folder of the docker-compose.yml file).

Yury Kirienko
  • 1,810
  • 1
  • 22
  • 32
Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53
4

From the official documentation we know that:

context defines either a path to a directory containing a Dockerfile, or a URL to a git repository.

In your case, . is a relative path representing the current directory where you run docker-compose command and where Compose can find a Dockerfile (and obviously also the docker-compose.yaml file).

The Dockerfile can also be elsewhere using the dockerfile keyword like that in this example:

version: "3.3"
services:
  
  service-A
    build:
      context: ./all-service
      dockerfile: ./service-A/Dockerfile

  service-B
    build:
      context: ./all-service
      dockerfile: ./service-B/Dockerfile

  service-C
    build:
      context: ./all-service
      dockerfile: ./service-C/Dockerfile 

The additional context keyword here is to tell the Dockerfile where to find its dependent files.

For instance, in one of your Dockerfile there are these lines:

WORKDIR /my-app
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

These three files must be in the context directory or git repository: package.json, package-lock.json, requirements.txt.

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
0

It may not be too clear, but its the same idea as the location a python module is run. Setting the context property allows the docker-compose command to find a Dockerfile AND any other files needed, for example with a file structure such as the one below. Think of the layout as /root containing multiple microservices. If context is commented out, the Dockerfile is not found, neither is the start.shscript , without using absolute paths

root
|-micro_service-1
|  |-compose
|    |-django
|      |- Dockerfile.dev
|      |- start.sh    
|  |-app    
|-docker-compose.yml
services:
[..]
  app:
    container_name: my_container
    build:
      dockerfile: ./compose/django/Dockerfile.dev
      context: microservice-1 
    command: compose/django/start.sh

Replace Dockerfile.dev with Dockerfile( either works)

enjoi4life411
  • 568
  • 5
  • 11