skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. 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 network

    Expose ports without publishing them to the host machine – they’ll only be accessible to linked services. Only the internal port can be specified.


    Example

    What is this example doing?

    A couple of steps that are not mandatory

    1. Set a static ip within your docker container
      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:

    1. This exposes port 8080, but do not publish it.
      expose:
            - 8080
      
    2. The network which allows static ip configuration
      networks:
        vpcbr:
          driver: bridge
          ipam:
            config:
              - subnet: 10.5.0.0/16
      

    A complete file could look similar to this:

    version: "3.8"
    services:
      first-service:
        image: <your-image>
        networks: 
          vpcbr:
            ipv4_address: 10.5.0.2
        expose:
          - 8080
    
      second-service:
         image: <your-image>
        networks: 
          vpcbr:
            ipv4_address: 10.5.0.3
        depends_on: 
          - first-service
    
    networks:
      vpcbr:
        driver: bridge
        ipam:
          config:
            - subnet: 10.5.0.0/16
    
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search