I have an issue with the Docker container where the container stopped automatically after starting. I am not able to figure out what is the issue.
Here is my simple DockerFile where i am pulling the tomcat image and deploying my war file to the /usr/local/tomcat/webapps folder
RUN apt-get update && apt-get -y upgrade
ADD app.war /usr/local/tomcat/webapps/app.war
I am using the following command to start the container
docker run -p 8091:8091 -d containerName
After running the above command, i can see that tomcat server is starting. When it started completely, the container stopped automatically.
I have tried using different commands like below, but didn’t help.
docker run -p 8091:8091 -itd containerName
Am i using wrong parameters to run the container ? Should i need to update the dockerfile ? Any information would help me in solving this issue.
try this: docker run -it -p 8091:8091 containerName
I got it working after changing the COPY command instead of ADD command in my Dockerfile
Not sure how that fixed the code.
Include ENTRYPOINT command which runs in background.
You can check exit code of container if it is 0 means container exited after all exection, means you have to run the process in foreground to keep container running.
if exit code is other than 0, means it is exiting because of code issue.
ADD automatically unpacks archives of types it recognizes;
COPY directly copies files over. (See the Dockerfile ADD docs.) Since Java
.war archives are secretly zip files (containing Java
.jar files, which are also zip files) Docker will helpfully unpack them for you.
In practice, you usually want
COPY unless you’re sure you want to unpack a tar or zip file.
If you did something like
docker run --rm -it myimage /bin/sh
you’d (probably) get an interactive shell where you could verify that the file layout is what you expected.
What you said is actually correct. I had an issue of tomcat server did not get started correctly, using the COPY command instead of ADD fixed that issue. But to make the container running i had to do one more step of including the ENTRYPOINT as like below
After that, i am able to make the container stay alive.
I did not complete my answer in the first place.
One thing that can help debug a container that isn’t running is the docker logs command.
docker logs <container_name>
This will show the standard out from the running container, which usually gives a clue as to why it exited, more than just the exit code you can see from the container.
Hope this helps with future debugging issues and happy Dockering.