I’m running a single docker-compose.yml
file that deploys multiple services;
- api_db
- api
- web_db
- php
- nginx
I also have all these services connected to a network defined below;
networks:
my-network:
name: my-network
On their own these containers work flawlessly. I can run docker and access the API from my host, as well as my web app, and the .env substitution works well for connecting web
to web_db
.
Where I’m having trouble is I want to get the IP/Host of the container api
and substitute it inside my web app’s .env file so I can query the API from my web app. Currently when attempting this, my web app takes it literally as “api” and tries to send a request to “https://api:443/api/…”. The strange thing is, this is located in exactly the same .env file that I use to substitute web_db
as the host, so I’m a bit confused as to why this is happening.
I’ve already confirmed with docker network inspect my-network
that all containers are connected to my network correctly.
Not really sure what the issue is so hopefully someone can assist.
docker-compose.yml
version: "3.5"
networks:
my-network:
name: my-network
services:
api_db:
image: mariadb:10.5
container_name: api_db
ports:
- "3307:3306"
command:
- "mysqld"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
volumes:
- "./mnt/docker/api/db:/var/lib/mysql"
restart: always
networks:
- my-network
api:
build:
context: ./dockerfiles
dockerfile: api.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: api
ports:
- "8001:8000"
depends_on:
- api_db
volumes:
- "./mnt/docker/api/data:/data"
restart: always
networks:
- my-network
web_db:
image: mariadb:10.5
container_name: web_db
ports:
- "3308:3306"
command:
- "mysqld"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
volumes:
- "./mnt/docker/web/db:/var/lib/mysql"
restart: always
networks:
- my-network
php:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: php
volumes:
- ./src:/var/www/html:delegated
networks:
- my-network
nginx:
build:
context: ./dockerfiles
dockerfile: nginx.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: nginx
ports:
- 80:80
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- web_db
networks:
- my-network