Shannia
Shannia

Reputation: 71

docker-compose file: How to run service A and tell service B that A is already running

I'm new to deployment world and having this issue when I try to deploy an app. The application I tried to deploy is consists of 2 services. First service is an AI model and the second one is the web app. In order to run the web app, the AI model is has to run first. This is the docker-compose.yml that I tried to make:

version: '3.8'
services:
        max-image-caption-generator:
                image: quay.io/codait/max-image-caption-generator
                ports:
                        - "5000"
        app:
                build: .
                depends_on:
                        - max-image-caption-generator
                ports:
                        - "8088"

Here are my questions:

  1. Am I defining the docker-compose.yml right?
  2. How do I tell app to run the max-image-caption-generator first?

I was able to build from the file above, I could curl the http://localhost:5000 and it gave me the right html of the AI model, but I couldn't curl http://localhost:8088. It's either connection was reset by peer or it can't connect to the http://localhost:5000 which means the AI model is not running.

Upvotes: 1

Views: 1799

Answers (1)

rzlvmp
rzlvmp

Reputation: 9364

Here is couple misunderstandings in your question:

  1. depends_on means that app will be run after max-image-caption-generator, but! Docker will not check if service inside max-image-caption-generator properly started or not. You have to add healthcheck to be sure that max-image-caption-generator is running properly, and after that add condition service_healthy to app.

  2. or it can't connect to the http://localhost:5000

    and it can't. Because localhost:5000 only accessible from Docker host but not from container's inside. You have to use container name to be able communicate between containers.

Your docker compose should be like:

version: '3.9'

services:
  max-image-caption-generator:
    image: quay.io/codait/max-image-caption-generator
    ports:
      - "5000"
    # networks is optional parameter
    networks:
      service_network:
        aliases:
          - generator.hostname
    # use it if you want to start app after max-image-caption-generator will be ready get requests
    # healthcheck:
    #   test: ["CMD", "some_test_script", "--params"]
    #   interval: 30s
    #   timeout: 10s
    #   retries: 2

  app:
    build: .
    # networks is optional parameter
    networks:
      - service_network
    depends_on:
      max-image-caption-generator:
        # set this condition if you added healthcheck to max-image-caption-generator container
        # condition: service_healthy
        # this condition just run app after max-image-caption-generator, and no matter is max-image-caption-generator running properly or not
        condition: service_started
    ports:
      - "8088"

# optional block that may be deleted (docker will use default network)
networks:
  service_network:
    name: service_network
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.0.10.240/28
          gateway: 10.0.10.241

After that you will be able to connect to max-image-caption-generator container from app container using http://generator.hostname:5000 url (if networks block is not provided service may be accessed by http://max-image-caption-generator:5000 (same as service key))

Here you can find information how healthcheck works.

Upvotes: 2

Related Questions