I have 2 docker containers being created by docker-compose, which I am running from WSL2 using Docker Desktop. Docker Desktop is connected to my WSL2 resource. My problem is that the api-container will only work when started up for a short time. Within an hour, or whenever I restart the container, the api-container stops connecting to the db-container and to localhost, and the log indicates it can’t connect to the db. Additionally, I cannot connect to the Docker containers via making local API calls unless my Firewall is completely off.
It’s either a docker-compose config error, or a WSL2 networking / permissions error. Next, I am going to set this up on Ubuntu to isolate the cause, but I’m curious if anyone has insight on 1) navigating networking issues using Docker from WSL2 and 2) if my docker-compose file is incorrect. I would prefer to keep using WSL2 with Docker if I can.
version: '3.9'
services:
api:
container_name: api-container
build:
context: .
dockerfile: Dockerfile
env_file: '.env'
environment:
# To connect to local your local psql db, replace DATABASE_URL with:
# postgres://postgres:postgres@host.docker.internal:5432/my-db
DATABASE_URL: postgres://postgres:postgres@db:5432/my-db
ports:
- 35001:35001
depends_on:
- db
volumes:
- .:/app
db:
image: postgres:16
container_name: db-container
restart: unless-stopped
ports:
- 35000:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my-db
volumes:
- postgres-data:/var/lib/postgresql/data # path for named volume
volumes:
postgres-data: # named volume for persistent postgres data