I am building an application using docker-compose (file 1) and I was wondering how I could communicate the host IP address to the docker-compose file. The application is composed of a db, a frontend and a backend. The aim of it is that I want to access my application from other computers on the local network. It works fine for the front but when I want to communicate with the backend, it does not work anymore. I suspect it's due to the fact that the front is trying to send data to localhost (which is not the correct one for another computer on the local network). Thus, I would like to communicate to the front application the IP address of the host directly in the docker-compose file, so it can communicate with the backend ! Any idea ?
File 1
version: "3.7"
services:
db:
build:
context: ./database
command: --default-authentication-plugin=mysql_native_password --sql_mode=""
restart: always
cap_add:
- SYS_NICE # CAP_SYS_NICE
volumes:
- db_data:/var/lib/mysql
ports:
- ${MYSQL_PORT}:${MYSQL_PORT}
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_TCP_PORT: ${MYSQL_PORT}
env_file: ./.env
back:
depends_on:
- db
build:
context: ./backend
target: development
volumes:
- ./backend:/app
- /app/node_modules
restart: "no"
ports:
- ${BACKEND_PORT}:${BACKEND_PORT}
env_file: ./.env
front:
depends_on:
- back
- db
build:
context: ./frontend
target: development
stdin_open: true
volumes:
- ./frontend:/app
- /app/node_modules
restart: "no"
ports:
- ${FRONTEND_PORT}:${FRONTEND_PORT}
env_file: ./.env
volumes:
db_data: {}
Here is the api url I am using on the frontend env to communicate with the backend, which I would to be replaced with the host IP dynamically.
File 2
const dev = {
...env,
apiUrl: "http://localhost:3000/api",
};
Thank you so much for you help! Best