I have 3 containers with my bot, server and db. after docker-compose up, server and db are working. telegram bot does get-request and takes this error:
Get "http://localhost:8080/user/": dial tcp 127.0.0.1:8080: connect: connection refused
docker-compose.yml
version: "3"
services:
db:
image: postgres
container_name: todo_postgres
restart: always
ports:
- "5432:5432"
environment:
# TODO: Change it to environment variables
POSTGRES_USER: user
POSTGRES_DB: somedb
POSTGRES_PASSWORD: pass
server:
depends_on:
- db
build: .
restart: always
ports:
- 8080:8080
environment:
DB_NAME: somedb
DB_USERNAME: user
DB_PASSWORD: pass
bot:
depends_on:
- server
build:
./src/telegram_bot
environment:
BOT_TOKEN: TOKEN
restart: always
links:
- server
3
Answers
Your
bot
container is up before your server & db containers.When you use
depends_on
it’s not accually waiting them to finish setup themeselves.You should try some tricky algorithem for waiting the other container finish setup.
I remmember that when I used Nginx proxy I used something called
wait-for-it.sh
What you try is to access localhost within your container (service)
bot
.Maybe this answer will help you to solve the problem. It sound similar to your problem.
But I want to provide you another solution to your problem:
In case it’s not needed to access the containers form outside (from your host), one appraoch would be making use of the
expose
functionality and a docker network.The
expose
functionality allows to access your other containers within your networkExample
What is this example doing?
A couple of steps that are not mandatory
These Steps are not needed and can be omitted. However, I like to do this, since you have now a better control over the network. You can access the containers by their hostname (which is the container name or service name) as well.
The steps that are needed are the following:
A complete file could look similar to this:
When using compose, try using the containers hostname.. in the case your bot should try to connect to
server:8080
Compose will handle the name resolution to the IP you need