Docker beginner here. I have 3 services (webapp, database, and redis) that need to share the same network.
When I build my container using Docker compose (via VS Code remote containers), sometimes everything's fine. Sometimes, the redis service is not accessible (connecting to 127.0.0.1:6379. Connection refused
). And even sometimes both the database and redis are not accessible.
Running ss
on debian, I can see the 6379
port is not open.
I used the network_mode: service:[foo]
strategy, as you can see below in my docker-compose.yml file:
version: "3"
services:
app:
build:
context: ..
dockerfile: .devcontainer/service_app/Dockerfile
volumes:
- ..:/workspace:delegated
# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
db:
network_mode: service:app
env_file: service_db/.env.local
build:
context: ..
dockerfile: .devcontainer/service_db/Dockerfile
restart: unless-stopped
volumes:
- "pgdata:/var/lib/postgresql/data"
rd:
network_mode: service:app
image: redis:7
restart: unless-stopped
volumes:
- "rddata:/data"
volumes:
pgdata:
rddata:
Note that I tried another strategy which was to not use network_mode
and reach my service using rd:6379
rather than 127.0.0.1:6379
. It worked but curiously, it hanged 3 seconds before establishing the connection, which is painful.
What did I do wrong? Thanks.