Anshul Sharma
Anshul Sharma

Reputation: 1123

Multiple docker-compose file with different context path

I have 2 docker-compose files that I need to run together, the locations of the files are like

/home/project1/docker-compose.yml

and

/home/project2/docker-compose.yml

so clearly both the services should have different contextpath

But when I run below docker compose command

docker-compose -f /home/project1/docker-compose.yml -f /home/project2/docker-compose.yml config

I get to see, Both the service are getting the same context path

app:
    build:
      context: /home/project1
      dockerfile: Dockerfile
app2:
    build:
      context: /home/project1
      dockerfile: Dockerfile

How can I resolve this issue I want both my services to have their own project path ie.

app service should have context path /home/project1

and

app2 service should have context path /home/project2

Upvotes: 7

Views: 10494

Answers (3)

Mafor
Mafor

Reputation: 10711

Option 1: Use absolute paths for the contexts in both docker-compose files

Option 2: Create a docker-compose.override.yml with the absolute paths:

version: "3"

services:
  service1:
    build:
      context: /home/project1
  service2:
    build:
      context: /home/project2

and include it in the docker-compose command:

docker-compose -f /home/project1/docker-compose.yml -f /home/project2/docker-compose.yml -f /home/docker-compose.override.yml config

On linux, to avoid hard-coding of the base path in the docker-compose.override.yml, you can use PWD environment variable:

services:
  service1:
    build:
      context: ${PWD}/project1
  service2:
    build:
      context: ${PWD}/project2

Upvotes: 6

Anshul Sharma
Anshul Sharma

Reputation: 1123

They way I found could run multiple services is:

1. First of all create images for all the services with the help of docker build command

ex: in my case it is a java application with maven build tool, so command I used:

mvn clean package docker:build -Denv=$ENV -DskipTests

2. After all the images built, create a common docker-compose file which will look like this

version: '3'

services:
  service1:
    image: <service1-image>
    ports:
      - "8100:8080"
    environment:
      - TZ=Asia/Kolkata
      - MYSQL_USER=root
      - MYSQL_PASSWORD=unroot
    ulimits:
      nproc: 65535
      nofile:
        soft: 65535
        hard: 65535
  service2:
    image: <service2-image>
    ports:
      - "8101:8080"
    environment:
      - TZ=Asia/Kolkata
      - MYSQL_USER=root
      - MYSQL_PASSWORD=unroot
    ulimits:
      nproc: 65535
      nofile:
        soft: 65535
        hard: 65535
  service3:
    image: <service3-image>
    environment:
      - TZ=Asia/Kolkata
      - MYSQL_USER=root
      - MYSQL_PASSWORD=unroot
    ulimits:
      nproc: 65535
      nofile:
        soft: 65535
        hard: 65535
networks:
  default:
    external:
      name: dev

The way I have mentioned mysql root, password, you can also add environment variables.

3.Then run below command to run all the services

docker-compose -f docker-compose.yml up

or run below command to run specific service

docker-compose -f docker-compose.yml up service1

This is working fine for me, this will require 1 time setup but after that it will be very easy and fast.

Upvotes: 4

Tim
Tim

Reputation: 2212

It seems that when using multiple docker-compose files the context is taken from the location of the first file. It is explained in detail in this issue

https://github.com/docker/compose/issues/3874

Upvotes: 0

Related Questions