I have the following docker-compose:
version: '3.8'
services:
mariadb_test:
image: mariadb/server:10.5
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=root
- MYSQL_ROOT_HOST=10.5.0.1
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
interval: 1m30s
timeout: 10s
retries: 3
networks:
my_network:
ipv4_address: 10.5.0.5
my_service_dev:
build:
context: .
dockerfile: Dockerfile_my_service
depends_on:
mariadb_test:
condition: service_completed_successfully
environment:
- DB_HOST=10.5.0.5
- DB_USER=root
- DB_PASSWORD=password
tty: true
networks:
my_network:
ipv4_address: 10.5.0.6
networks:
my_network:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1
Also this is the Dockerfile:
FROM debian:11.6
ENV DEBIAN_FRONTEND=noninteractive
ARG DB_HOST
ARG DB_USER
ARG DB_PASSWORD
ENV DB_HOST=${DB_HOST}
ENV DB_USER=${DB_USER}
ENV DB_PASSWORD=${DB_PASSWORD}
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get install -y \
libmariadb3 \
wget
WORKDIR /usr/local/bin
RUN wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh
RUN chmod +x wait-for-it.sh
RUN ./wait-for-it.sh 10.5.0.5:3306 --timeout=40 -- echo "MariaDB is up"
RUN mariadb --host=${DB_HOST} --port=3306 --user=${DB_USER} --password=${DB_PASSWORD} -e "CREATE USER 'test_user'@'' IDENTIFIED BY '12345';"
When running the command docker-compose up -d my_service_dev
I want to wait for my mariadb_test to be fully ready before starting to build my image of my_service.
I've tried 3 options so far and none of them worked:
Option 1
Trying to add this inside of my_service
depends_on:
mariadb_test:
condition: service_completed_successfully
Option 2:
Adding the atkrad/wait4x component like this:
wait-for-db:
image: atkrad/wait4x
depends_on:
- mariadb_test
command: tcp mariadb_test:3306 -t 30s -i 250ms
And then also changing the depend_on for my_service to this:
depends_on:
wait-for-db:
condition: service_completed_successfully
Option 3:
Trying to run wait-for-it.sh inside my Dockerfile like this:
RUN ./wait-for-it.sh 10.5.0.5:3306 --timeout=40 -- echo "MariaDB is up"
But in this case is just going to get stuck for 40 seconds (or whatever other period of time
wait-for-it.sh: timeout occurred after waiting 40 seconds for 10.5.0.5:3306
Is there any other way in which I can wait for a container to be fully ready before I can start to build my image?