0

I have a pipeline that attempts to create a docker image, upload it to docker hub, then I SSH to an EC2 instance and run a shell script that will download the image and launch a container.

The problem is that when the shell script is finished downloading docker image and attempting to launch the container, the pipeline thinks the deployment succeeded. However, there might be a problem with the container ( it is a Node JS application, and sometimes there are errors that prevent it from compiling).

Ideally, I would like to add to the shell script so that the pipeline checks to make sure the container is actually up and running.

Below is the script thus far. I was thinking of pulling the status of newly launched container and see whether it is running or has exit code (1) as status. I know the Image, but can't get the container ID ( this did not work : Get docker container id from container name )

Or is there a better way of doing this?

echo "Starting Docker container replacement script"

ENV=$1
USERNAME=$2

echo "this is the env: ${ENV}"

if [ $ENV = "dev" ]; then
    PORT=9111
    CMD="npm run dev"

elif [ $ENV = "dev2" ]; then
    PORT=9222
    CMD="npm run dev2"

elif [ $ENV = "dev3" ]; then
    PORT=9333
    CMD="npm run dev3"

else
     echo "ERROR: No valid environment parameter was passed"
     exit 1
fi

for i in $(docker ps -a -q --filter ancestor=someaccount/registry:$ENV); do docker rm $(docker stop $i); done
docker rmi someaccount/registry:$ENV
cat docker_pass.txt | docker login -u $USERNAME --password-stdin
docker pull someaccount/registry:$ENV
docker run -p $PORT:7665 -d someaccount/registry:$ENV $CMD

echo "Finishing  script"

1 Answers1

1

You could add a Healthcheck to the Docker image. This can also be specified on the command-line if you do not want to put it in the image. Basically, this tells Docker what command to run and how often to check if the container is still working properly.

For example, from the documentation you could check if a website in the container is up every 5 minutes with the following:

HEALTHCHECK --interval=5m --timeout=3s \
  CMD curl -f http://localhost/ || exit 1

You can then check on the health:

docker inspect --format='{{json .State.Health}}' <container-id>

When you run docker with the -d flag, it outputs the container-id to standard out.

Nick Rundle
  • 579
  • 3
  • 8