Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 689
Anshul Sharma Avatar asked Jun 14 '20 05:06

Anshul Sharma


1 Answers

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.

like image 53
Anshul Sharma Avatar answered Nov 15 '22 08:11

Anshul Sharma