Reputation: 930
I run many services in my docker-compse.yml. I'd like to display a message to let user know when docker-compose up is done? I tried to echo message with command but my container exited with code 0
command: bash -c "echo Congratulation! You can use your containers now"
Is there anyway to let user know when docker-compose up is done? Many thanks!
Upvotes: 6
Views: 6926
Reputation: 92617
You can use |
and while
loop
version: "3.9"
services:
cmd:
container_name: my-node-cmd-1
image: node:16.10.0
volumes:
- .:/work
command:
- bash
- -c
- |
echo "----------------------------------------------"
echo "To join to CMD, in separate console execute:"
echo " "
echo 'docker exec -it my-node-cmd-1 bash -c "cd /work; bash"'
while true; do sleep 10000; done
Above working example gives access to node cmd in current directory - if you copy it to docker-compose.yml
and run by docker compose up
you should see
The while
loop is infinite but sleep 10s on each iteration. If you remove line with this loop then container will exit with code 0
BONUS
You can get similar effect to above example by using run
instead up
on following script
version: "3.9"
services:
# run this container by: docker compose run cmd
cmd:
image: node:16.10.0
volumes:
- .:/work
command:
- bash
- -c
- |
echo "Hello World!"
cd /work
bash
Upvotes: 0
Reputation: 1765
Anything you do in CMD will only be visible in the logs, and only per service, so you may as well just watch the logs which I assume is what you're trying to avoid. If you want it to work from the local command line you're going to need to wrap the docker-compose up
command.
THE following is NOT fully tested. This is for guidance only. I use the getContainerHealth and waitContainer scripts myself, so can vouch for them. I'm fairly sure I found them on this site, but can't remember where.
Assuming you're waiting for your services to be in a usable state, you could use something similar to this compose file:
docker-compose.yml
services:
serviceOne:
// ... the usual stuff
container_name: serviceOne
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 10s
timeout: 10s
retries: 3
start_period: 1s
serviceTwo:
// ... the usual stuff
container_name: serviceTwo
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 10s
start_period: 1s
The commands in the test are the commands you use to define the service is up and running. More info here: https://docs.docker.com/compose/compose-file/#healthcheck
You will probably want different health checks (or at least intervals, timeouts, start_periods, etc) for production and development.
then some bash script that you use instead of docker-compose up:
getContainerHealth () {
docker inspect --format "{{json .State.Health.Status }}" $1
}
waitContainer () {
while STATUS=$(getContainerHealth $1); [ $STATUS != "\"healthy\"" ]; do
if [ $STATUS = "\"unhealthy\"" ]; then
echo "Failed!"
exit -1
fi
printf .
lf=$'\n'
sleep 1
done
printf "$lf"
}
waitContainers () {
waitContainer serviceOne
waitContainer serviceTwo
// ... for all services
}
docker-compose up
echo "In progress. Please wait"
waitContainers
echo "Done. Ready to roll."
Upvotes: 3
Reputation: 2035
Have you tried running something like the following directly in the Dockerfile itself ?
RUN echo -e "Your message"
Upvotes: 0
Reputation: 11
create a shell file echo.sh that contains echo "Your message"
In Docker file add CMD ["/dockerRepo/echo.sh"]
Upvotes: 1