7

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

5
  • if you use docker swarm. Yon can specify replicas Commented Jun 15, 2020 at 4:18
  • How is that going to help me here, could you please elaborate this. Commented Jun 17, 2020 at 6:22
  • did you find any solution to this? Commented Sep 18, 2020 at 12:30
  • It's an open issue on Github, but there's a few suggestions for workarounds in the comments. Commented Sep 18, 2020 at 12:35
  • @Tim I have posted the way I solved this issue and it is a generic one I have seen this approach being followed at large level at my current company. Commented Sep 20, 2020 at 11:08

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

i believe option 1 will not work because all paths will be relative to the location of the first passed docker-compose file.
@Tim No, that's not the case. Relative paths are resolved relative to the location of the first docker-compose file, absolute paths stay as they were.
4
+200

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.