Home > Blockchain >  How can I build a shell script to build a Docker-Compose Image and start up the Docker container if
How can I build a shell script to build a Docker-Compose Image and start up the Docker container if

Time:10-29

I'm trying to write a shell script that will attempt to build a Docker-Compose image, then if that's successful, start up the Docker container with it by doing a Docker-Compose up -d. In the second step, I load some JSON files into Mongo, but then if there's a problem with that step, I want want to stop all Docker services and print out an error message saying "Could not start Docker service because there was a problem seeding the DB."

My problem is that I don't know how to check if the image was created successfully in the first step. (I want to stop and print out a message saying that there were errors in the build.)

I think I can figure out something that checks if both services are running at the end, but I'm wondering if there is a way to print out the name of the problematic JSON file that couldn't load.

Does anyone have a similar script that I can use or modify for my own use?

CodePudding user response:

My understanding is you are trying to branch out the logic of the script based on whether the build command was a success. Here is what you can do:

cat create-image.sh

#!/bin/bash

docker build -q -t test-image .

if [ $? -eq 0 ]; then
   echo OK, images created
else
   echo FAIL, image not created
   exit 1
fi

echo "...DOING OTHER THINGS... (like docker-compose up)"

Let me know if this is what you were looking for.

CodePudding user response:

You don't need a script if you just want to build images before starting containers:

docker-compose --build up

If you must use a script, you can certain call the above command and check its exit code. There is no need to call docker build individually for every service in the docker-compose file.

  • Related