Adam Ranganathan
Adam Ranganathan

Reputation: 1717

How to automate docker to run script within container and notify completion to host?

I would like to automate the build process of my application through the following steps:

  1. Launch my build-system container that has all dependencies and scripts for making the final exe of my application.
  2. Within the build-system container fire a bash script that starts the build process.
  3. When the build completes, transfer the exe to the host machine either by using docker cp or by attaching a volume while launching the container.
  4. Transfer the exe to a new dist-system container, which is essentially the final image that will be stored in Docker hub.
  5. Install the application within the dist-system, make custom configurations by firing another bash script in that container.
  6. When step 5 completes, get back to the host machine and run docker commit of the dist-system container and then docker push the image to Docker hub from the host machine.

My question relates specifically to points 3 and 6 where I need to know that the bash scripts have completed execution within the containers. Only then I would like to fire docker commands in the host machine. Is there a way in which docker can be notified of bash script execution within containers?

Upvotes: 0

Views: 2634

Answers (1)

4Ply
4Ply

Reputation: 36

docker run is synchronous, and by default will block until the docker container exits.

For example, if the build-system Dockerfile looks like this:

FROM alpine:latest

COPY ./build.sh /build.sh

VOLUME /data

CMD ["/build.sh", "/data/test.out"]

With build.sh as follows:

#!/bin/sh

sleep 5
echo "Hello world!" >> "$1"
exit 0 # docker run will exit with this code, you can use this to check if your build was successful

Running docker run --rm -v /your/work/directory/build-output:/data build-system will wait 5 seconds and then exit, and /your/work/directory/build-output/test.out will have been created.

Upvotes: 2

Related Questions