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"