How to Wait for All the Services in the Container to Be up before Exiting Docker Start?

Using Docker version 20.10.18, build b40c2f6 on Ubuntu 20.04. This is how I am building the image and starting up the container.

#!/usr/bin/env bash
# encoding:utf-8
docker image build --tag=$IMAGE ./
set -e # From this point onwards, any failure must exit the script
docker container created --hostname $CONTAINER --publish $HOST_PORT:$CONTAINER_PORT --name $CONTAINER $IMAGE
docker container start $CONTAINER # Exits immediately
sleep 40s # Will explain 

Now, starting the container takes some non-negligible time (about 30 seconds, to start up some internal service and mount some directories etc.) But I have noticed the start command exits even before the start up (inside the container) is complete.

Normally, this would not be a problem, but the point is, after starting up, my script moves on to run some integration tests assuming the container services are up (in reality, which are still booting up). I went around the problem by introducing the sleep command, but that does not look nice. It leaves me to estimate the start time, and add a good amount of buffer before I proceed to run the tests.

Is there any option to the start command that will block it until the container has started up all its services inside it? Or, am I approaching the whole problem in the wrong way, and should I use some very different commands instead?

Yes you do. Your “services” should run in the foreground to keep the container alive and you don’t need to use sleep anymore. Even if somehow you manage to wait until the processes start, the container could stop while the processes are still running.

Search for “s6-init” or “Supervisor” if you still want to run multple background services in one container but usually that is not recommended.

PS.: Please, use the “Tips and HowTos” category for shaing your tips and tricks not asking for tips to solve an issue. I moved to the topic to “DockerEngine”.