docker tomcat re-deploy application

933 Views Asked by At

I have a deploy job in a jenkins docker container to deploy the application in docker tomcat container running in a port:8383 inside the jenkins container port 8080.

I can able to deploy the application successfully and start the server for the first time.

However if I want to redeploy the application , the deploy job fails as the port 8383 already allocated.

I want to check if docker tomcat container is already running , if it is running then I need to stop the tomcat container ,deploy the application and start it again.

How can I achieve this using shell script?

1

There are 1 best solutions below

0
On BEST ANSWER

First, start your container with a name (using the --name flag). This will allow you to easily find this container later, when you want to redeploy it:

docker run -d -it -p 8383:8080 --name your-application docker-tomcat

Using the name, you can check if the container already exists, and then stop and delete it if it does (the shell script is off the top of my head, there are probably better ways to build this):

[ $(docker ps -aq -f name=your-application | wc -l) -gt 0 ] && \
    docker stop your-application && docker rm your-application

After that, re-create your container with the new image as before.