'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:
- Am I defining the
docker-compose.ymlright? - How do I tell
appto runthe max-image-caption-generatorfirst?
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.
Solution 1:[1]
Here is couple misunderstandings in your question:
depends_onmeans thatappwill be run aftermax-image-caption-generator, but! Docker will not check if service insidemax-image-caption-generatorproperly started or not. You have to addhealthcheckto be sure thatmax-image-caption-generatoris running properly, and after that add conditionservice_healthytoapp.-
or it can't connect to the
http://localhost:5000and it can't. Because
localhost:5000only 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
