How to Merge Docker Compose file with Bash - bash

I'm trying to merge the docker-compose.yml file with the docker-compose2.yml file with bash.
docker-compose.yml :
version: "3"
services:
nexus:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8081:8081"
volumes:
nexus-data: {}
docker-compose2.yml :
version: "3"
services:
nexus2:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8082:8082"
volumes:
nexus-data: {}
Output I Want:
version: "3"
services:
nexus:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8081:8081"
nexus2:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8082:8082"
volumes:
nexus-data: {}
How do I get this output with bash?

The Docker Compose config command does exactly what you need, it takes multiple compose file and merges them.
Just pass them using multiple -f flags:
docker-compose -f docker-compose.yml -f docker-compose2.yml config
or using an environment variable:
COMPOSE_FILE=docker-compose.yml:docker-compose2.yml docker-compose config
The same approach is valid for every Docker Compose command, so if your final target is, for example, to set up your project, you can directly run:
docker-compose -f docker-compose.yml -f docker-compose2.yml up
Check the documentation for further details on how to specify multiple compose files.

I don't think you can do this (easily as a one-liner) in native bash without writing a script. I was curious so I did a quick search and found a yaml manipulation tool which supports merging yaml (docker-compose) files and looks like it fits your use-case.
I used brew to install on MacOS but there are instructions for Linux as well - https://mikefarah.github.io/yq/.
brew install yq
Showing existing files:
$ cat file1.yaml
version: "3"
services:
nexus:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8081:8081"
volumes:
nexus-data: {}
$ cat file2.yaml
version: "3"
services:
nexus2:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8082:8082"
volumes:
nexus-data: {}
Merge both files outputting to stdout:
$ yq m file1.yaml file2.yaml
services:
nexus:
image: sonatype/nexus3
ports:
- 8081:8081
volumes:
- /opt/nexus3/nexus-data:/nexus-data
nexus2:
image: sonatype/nexus3
ports:
- 8082:8082
volumes:
- /opt/nexus3/nexus-data:/nexus-data
version: "3"
volumes:
nexus-data: {}
There may be a native way but I just redirected the stdout to a file:
$ yq m file1.yaml file2.yaml > file3.yaml
$ cat file3.yaml
services:
nexus:
image: sonatype/nexus3
ports:
- 8081:8081
volumes:
- /opt/nexus3/nexus-data:/nexus-data
nexus2:
image: sonatype/nexus3
ports:
- 8082:8082
volumes:
- /opt/nexus3/nexus-data:/nexus-data
version: "3"
volumes:
nexus-data: {}
There are a lot of examples in their documentation for you to explore - https://mikefarah.github.io/yq/merge/.

Related

How to configure traefik to correctly route traffic from a specific domain to a specific nginx container

I have created two container laravel webapp (project1 and project2) with own nginx/php-fpm and linked them with traefik container. Each project has its own folder and docker-compose.yaml properly configured with traefik labels.
Thanks to traefik what I expect is that when I visit the project1.laravel.test I look at the contents of project1 and when I visit the project2.laravel.test I look at the content of project2.
The issue is that when I visit project1.laravel.test, alternately, the content of project1 is shown and other times the content of project2 is shown. If I shut down the container of the project2, project 1 works fine. It seems that traefik is configured as a load balancer but I don't understand where is the issue.
How to replicate my issue?
1. git clone https://github.com/gtoto007/traefik-laravel-docker
2. cd traefik-laravel-docker
3. docker-compose -f traefik/docker-compose.yaml up -d
4. docker-compose -f project1/docker-compose.yaml up -d
5. docker-compose -f project1/docker-compose.yaml up -d
in your host file
127.0.0.1 traefik.laravel.test
127.0.0.1 project1.laravel.test
127.0.0.1 project2.laravel.test
MY DOCKER-COMPOSE FILES
Below for simplicity I put the three docker-compose of project1, project2 and traefik:
./traefik/docker-compose.yaml
version: "3.3"
networks:
my-network:
external: true
services:
traefik:
image: "traefik:v2.9"
container_name: "traefik"
networks:
- my-network
command:
- "--api"
- "--providers.docker.exposedbydefault=false"
- "--api.insecure=true"
- "--accesslog.filepath=/data/access.log"
# entrypoints
- "--entrypoints.http.address=:80"
- "--entrypoints.https.address=:443"
- "--entrypoints.traefik.address=:8888"
ports:
- "80:80"
- "443:443"
- "8888:8888"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.rule=Host(`traefik.laravel.test`)"
- "traefik.http.routers.traefik.entrypoints=traefik"
./project1/docker-compose.yaml
version: '3.8'
networks:
my-network:
external: true
services:
nginx:
build:
dockerfile: docker/nginx/Dockerfile
context: ./
image: my-nginx
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d/
- my-data_project1:/var/www
networks:
- my-network
depends_on:
- php-fpm
labels:
- "traefik.enable=true"
- "traefik.http.routers.project1.rule=Host(`project1.laravel.test`)"
- "traefik.docker.network=my-network"
php-fpm:
build:
dockerfile: docker/php/Dockerfile
context: ./
image: my-php-fpm
ports:
- "5173:5173"
volumes:
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
- my-data_project1:/var/www
networks:
- my-network
project1:
build:
dockerfile: docker/project1/Dockerfile
context: ./
image: project1:1.0
volumes:
- my-data_project1:/var/www
networks:
- my-network
volumes:
my-data_project1:
./project2/docker-compose.yaml
version: '3.8'
networks:
my-network:
external: true
services:
nginx:
build:
dockerfile: docker/nginx/Dockerfile
context: ./
image: my-nginx
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d/
- my-data_project2:/var/www
networks:
- my-network
depends_on:
- php-fpm
labels:
- "traefik.enable=true"
- "traefik.http.routers.project2.rule=Host(`project2.laravel.test`)"
- "traefik.docker.network=my-network"
php-fpm:
build:
dockerfile: docker/php/Dockerfile
context: ./
image: my-php-fpm
ports:
- "5174:5173"
volumes:
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
- my-data_project2:/var/www
networks:
- my-network
project2:
build:
dockerfile: docker/project2/Dockerfile
context: ./
image: project2:1.0
volumes:
- my-data_project2:/var/www
networks:
- my-network
# - ./:/var/www
volumes:
my-data_project2:
I resolved the issue.
The problem is caused by container name conflict because the projects share the same network and some containers have the same hostname of default.
For example, if you do a ping nginx sometimes you get the ip of the nginx service of project1 and other times you get the ip of the nginx service of project2 because both services use the same hostname.
I fixed it by overwriting the hostnames for each container with unique hostname by Hostname property of docker-compose and corrected the nginx configuration for each project.
You can watch my fixed in this commit :
https://github.com/gtoto007/traefik-laravel-docker/commit/707925465b979168448128b8b307660bde2b5aeb

How to join 2 Docker commands to one?

I read an instruction here
https://github.com/dahlsailrunner/northwind-core-api
and exec 2 commands everytime as it is written:
docker volume create --name northwind-pg-data -d local
docker-compose -f .\docker-compose-windows.yml up
But is it possible to add the 1st command to the docker-compose-windows.yml file and perform one command only?
There is the docker-compose file:
version: '3'
services:
db:
image: postgres:12
environment:
POSTGRES_DB: northwind
POSTGRES_USER: northwind_user
POSTGRES_PASSWORD: thewindisblowing
volumes:
- northwind-pg-data:/var/lib/postgresql/data
- ./northwind.sql:/docker-entrypoint-initdb.d/northwind.sql
ports:
- "5432:5432"
volumes:
northwind-pg-data:
external: true

Docker container ignoring folder within directory

I've created a docker container to run a spring boot application, that when ran on the localhots requires access to a folder containing a number files. when i built the docker image , i copied the folder with these files into the container.
As you can see the folder appears to be present but the application seems to have to trouble locating the /messaging folder
container directory
Heres the error i get:
error message
below is the docker-compose i created to connect everything together.
version: '2.1'
services:
zoo1:
image: zookeeper:3.4.9
hostname: zoo1
ports:
- "2181:2181"
environment:
ZOO_MY_ID: 1
ZOO_PORT: 2181
ZOO_SERVERS: server.1=zoo1:2888:3888
volumes:
- ./zk-single-kafka-single/zoo1/data:/data
- ./zk-single-kafka-single/zoo1/datalog:/datalog
kafka1:
image: confluentinc/cp-kafka:5.4.1
hostname: kafka1
ports:
- "9092:9092"
- "19092:19092"
environment:
KAFKA_HOSTNAME: localhost
KAFKA_ADVERTISED_LISTENERS: LISTENER_DOCKER_INTERNAL://kafka1:19092,LISTENER_DOCKER_EXTERNAL://kafka1:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: LISTENER_DOCKER_INTERNAL:PLAINTEXT,LISTENER_DOCKER_EXTERNAL:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: LISTENER_DOCKER_INTERNAL
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
KAFKA_BROKER_ID: 1
#KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
volumes:
- ./zk-single-kafka-single/kafka1/data:/var/lib/kafka/data
depends_on:
- zoo1
List_service:
image: list-service
ports:
- "8089:8089"
environment:
KAFKA_BOOTSTRAPADDRESS: kafka1:19092
#KEYCLOAK_AUTH-SERVER-URL : "localhost:8080/auth"
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
depends_on:
- kafka1
- zoo1
filter-service:
image: filter-service
ports:
- "8090:8090"
environment:
KAFKA_BOOTSTRAPADDRESS: kafka1:9092
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
FMP_HOME: /
depends_on:
- kafka1
- zoo1

Why docker sync files with map folder extremely slow? (Ubuntu)

On my local machine (Ubuntu 18.04, 8GB RAM, i5, HDD) I have two docker-compose files with my laravel project
docker-compose.yml
version: '3.7'
networks:
backend-network:
driver: bridge
frontend-network:
driver: bridge
services:
&app-service app: &app-service-template
container_name: k4fntr_app
build:
context: ./docker/php-fpm
args:
UID: ${UID?Use your user ID}
GID: ${GID?Use your group ID}
USER: ${USER?Use your user name}
user: "${UID}:${GID}"
hostname: *app-service
volumes:
- /etc/passwd/:/etc/passwd:ro
- /etc/group/:/etc/group:ro
- ./:/var/www/k4fntr
environment:
APP_ENV: "${APP_ENV}"
CONTAINER_ROLE: app
FPM_PORT: &php-fpm-port 9000
FPM_USER: "${UID:-1000}"
FPM_GROUP: "${GID:-1000}"
networks:
- backend-network
&queue-service queue:
<<: *app-service-template
container_name: k4fntr_queue
restart: always
hostname: *queue-service
depends_on:
- app
environment:
CONTAINER_ROLE: queue
&schedule-service schedule:
<<: *app-service-template
container_name: k4fntr_schedule
restart: always
hostname: *schedule-service
depends_on:
- app
environment:
CONTAINER_ROLE: scheduler
&sportlevel-listener sportlevel_listener:
<<: *app-service-template
container_name: k4fntr_sl_listener
restart: always
hostname: *sportlevel-listener
ports:
- "${SPORTLEVEL_LISTEN_PORT}:${SPORTLEVEL_LISTEN_PORT}"
depends_on:
- app
environment:
CONTAINER_ROLE: sl_listener
&php-fpm-service php-fpm:
<<: *app-service-template
container_name: k4fntr_php-fpm
user: 'root:root'
restart: always
hostname: *php-fpm-service
ports: [*php-fpm-port]
entrypoint: /fpm-entrypoint.sh
command: php-fpm --nodaemonize
networks:
- backend-network
- frontend-network
echo-server:
container_name: k4fntr_echo
image: oanhnn/laravel-echo-server
volumes:
- ./:/app
environment:
GENERATE_CONFIG: "false"
depends_on:
- app
ports:
- "6001:6001"
networks:
- backend-network
- frontend-network
redis:
container_name: k4fntr_redis
image: redis
restart: always
command: redis-server
volumes:
- ./docker/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./docker/redis/redis-data:/data:rw
ports:
- "16379:6379"
networks:
- backend-network
and docker-compose-dev.yml
version: '3.7'
volumes:
redis-data:
pg-data:
k4fntr_sync:
external: true
services:
&app-service app: &app-service-template
container_name: k4fntr_app
build:
context: ./docker/php-fpm
args:
UID: ${UID?Use your user ID}
GID: ${GID?Use your group ID}
USER: ${USER?Use your user name}
user: "${UID}:${GID}"
hostname: *app-service
volumes:
- /etc/passwd/:/etc/passwd:ro
- /etc/group/:/etc/group:ro
- k4fntr_sync:/var/www/k4fntr:nocopy
environment:
APP_ENV: "${APP_ENV}"
CONTAINER_ROLE: app
FPM_PORT: &php-fpm-port 9000
FPM_USER: "${UID:-1000}"
FPM_GROUP: "${GID:-1000}"
networks:
- backend-network
&php-fpm-service php-fpm:
<<: *app-service-template
container_name: k4fntr_php-fpm
user: 'root:root'
restart: always
hostname: *php-fpm-service
ports: [*php-fpm-port]
entrypoint: /fpm-entrypoint.sh
command: php-fpm --nodaemonize -d "opcache.enable=0" -d "display_startup_errors=On" -d "display_errors=On" -d "error_reporting=E_ALL"
networks:
- backend-network
- frontend-network
mail:
container_name: k4fntr_mail
image: mailhog/mailhog
ports:
- "1025:1025"
- "8025:8025"
networks:
- backend-network
nginx:
container_name: k4fntr_nginx
image: nginx
volumes:
- ./docker/nginx/config/default:/etc/nginx/conf.d
- k4fntr_sync:/var/www/k4fntr:nocopy
depends_on:
- *php-fpm-service
ports:
- "${NGINX_LISTEN_PORT}:80"
networks:
- frontend-network
database:
container_name: k4fntr_database
build: ./docker/postgres
restart: always
environment:
ENV: ${APP_ENV}
TESTING_DB: ${DB_DATABASE_TESTING}
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
ports:
- "15432:5432"
volumes:
- ./docker/postgres/prod/:/prod
- ./docker/postgres/pg-data:/var/lib/postgresql/data:rw
networks:
- backend-network
The problem is the fact that when I change some files in my project I have to wait a lot of time. From 15 to 40 seconds. It is impossible for local development. How can I solve this problem?
I learned some information with similar problems with other OS such as Mac or Windows, but I can't found the same problems with Linux.
The problem was that I thought that second file (docker-compose-dev.yml) overrided first file. I mean php-fpm section. If you look at docker-compose-dev you can see that there is the command
command: php-fpm --nodaemonize -d "opcache.enable=0" -d "display_startup_errors=On" -d "display_errors=On" -d "error_reporting=E_ALL"
Actually I used first file (what is very strongely, because I used the command
docker-compose -f docker-compose-dev.yml -f docker-compose.yml up
) and my opcache was cached. This was the main reason why I had to wait so long

How to start CosmosDB emulator with docker-compose?

I've got a docker-compose project in Visual Studio which starts 3 services. One of them use cosmosdb.
I've followed the instructions on https://hub.docker.com/r/microsoft/azure-cosmosdb-emulator/ to start the emulator in a docker container and it worked.
But now I want to get it up and running through docker-compose file. Following is my current configuration.
version: '3.4'
services:
gateway:
environment:
- ASPNETCORE_ENVIRONMENT=Development
image: ${DOCKER_REGISTRY-}gateway
ports:
- "7000:80"
depends_on:
- servicea
- serviceb
build:
context: .\ApiGateways\IAGTO.Fenix.ApiGateway
dockerfile: Dockerfile
servicea:
environment:
- ASPNETCORE_ENVIRONMENT=Development
image: ${DOCKER_REGISTRY-}servicea
depends_on:
- email.db
build:
context: .\Services\ServiceA
dockerfile: Dockerfile
serviceb:
environment:
- ASPNETCORE_ENVIRONMENT=Development
image: ${DOCKER_REGISTRY-}serviceb
build:
context: .\Services\ServiceB
dockerfile: Dockerfile
email.db:
image: microsoft/azure-cosmosdb-emulator
container_name: cosmosdb-emulator
ports:
- "8081:8081"
I can see the container running when I run docker container list
But requests to https://localhost:8081/_explorer/index.html fails.
Any help on this much appreciated
I was in the same situation but the container was started with the following docker-compose.yml and it became accessible.
I can browse https://localhost:8081/_explorer/index.html
version: '3.7'
services:
cosmosdb:
container_name: cosmosdb
image: microsoft/azure-cosmosdb-emulator
tty: true
restart: always
ports:
- "8081:8081"
- "8900:8900"
- "8901:8901"
- "8979:8979"
- "10250:10250"
- "10251:10251"
- "10252:10252"
- "10253:10253"
- "10254:10254"
- "10255:10255"
- "10256:10256"
- "10350:10350"
volumes:
- vol_cosmos:C:\CosmosDB.Emulator\bind-mount
volumes:
vol_cosmos:
Probably I needed to set "tty" or "volumes".
Using the linux cosmos db image, I set it up like this:
version: '3.4'
services:
db:
container_name: cosmosdb
image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
tty: true
restart: always
mem_limit: 2G
cpu_count: 2
environment:
- AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
- AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
ports:
- "8081:8081"
- "8900:8900"
- "8901:8901"
- "8979:8979"
- "10250:10250"
- "10251:10251"
- "10252:10252"
- "10253:10253"
- "10254:10254"
- "10255:10255"
- "10256:10256"
- "10350:10350"
volumes:
- vol_cosmos:/data/db
volumes:
vol_cosmos:
Part of the problem is that the emulator takes a while to start, and there is a timeout of 2 minutes before it's just stops waiting.
I'm trying to hack my way through it, but I haven't had much success.
For now the image only works stand alone (via docker run) and that's it.

Resources