docker file for tomcat - docker

I wrote one docker file for tomcat installation, the file is
FROM openjdk:8-jre-alpine
RUN wget http://mirrors.fibergrid.in/apache/tomcat/tomcat-8/v8.5.34/bin/apache-tomcat-8.5.34.tar.gz
RUN gzip apache-tomcat-8.5.34.tar.gz
ADD https://github.com/spagop/quickstart/raw/master/management-api-examples/mgmt-deploy-application/application/jboss-as-helloworld.war /apache-tomcat-8.5.34/*/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
And I built the above docker file by using
$ docker build -t tomacat -f docker file name .
And created the container by using above docker image, command is
$ docker run --name=tom1 -d -it -p 9090:8080 tomcat
After running the docker run container is up and running
But tomcat server is not up and running in background
I replaced the catalina.sh with startup.sh in CMD area and also i getting same problem
Please help me for resolving problem

As mentioned in the question, I did everything as stated and started a container. After that I accessed the tomcat through HOST_IP:HOST_PORT and I was able to access the Tomcat. The issue may be that you are not accessing the Tomcat correctly like HOST_IP is not correct.

Related

Docker on windows can mount a folder for nginx container but not for ubuntu

I am building an image from this docker file for NGinx
FROM nginx
COPY html /usr/share/nginx/html
I then run the container using this command
docker run -v /C/nginx/html:/usr/share/nginx/html -p 8081:80 -d --name cntr-mynginx mynginx:abc
This works and I am able to mount the folder and the changes made in the html folder on the host can be seen when within the container file system. The edits made on the container filesystem under the /usr/share/nginx/html folder are visible on the host as well.
Why does the same not work when I use an Ubuntu base? This is the docker file for the Ubuntu container I am trying to spin up.
FROM ubuntu:18.04
COPY html /home
I used this command to run it
docker run -v /C/ubuntu-only/html:/home -p 8083:8080 --name cntr-ubuntu img-ubuntu:abc
The command above runs and when I do a docker ps -a, I see that the container stopped as soon as it started.
I removed the copy of the html and made the ubuntu image even more smaller by keeping just the first line FROM ubuntu:18.04 and even then I get the same result. Container Exited almost soon as it started. Any idea why this works for NGINX but not for Ubuntu and what do I need to do to make it work?
The issue you are experiencing does not have to do with mounting a directory into your container.
The command above runs and when I do a docker ps -a, I see that the container stopped as soon as it started.
The container is exiting due to the fact that there is no process being specified for it to run.
In the NGINX case, you can see that a CMD instruction is set at the end of the Dockerfile.
CMD ["nginx", "-g", "daemon off;"]
This starts NGINX as a foreground process, and prevents the container from exiting immediately.
The Ubuntu Dockerfile is different in that it specifies bash as the command the container will run at start.
CMD ["/bin/bash"]
Because bash does not run as a foreground process here, the container exits immediately.
Try augmenting your docker run command to include a process that stays in the foreground, like sleep.
docker run -v /C/ubuntu-only/html:/home -p 8083:8080 --name cntr-ubuntu img-ubuntu:abc sleep 9000
If you run docker exec -it cntr-ubuntu /bin/bash you should find yourself inside the container and verify that the mounted directory is present.

Docker can't find OpenJDK

I am experimenting with Docker for the first time, and am trying to get a Spring Boot web app to run inside a Docker container. I am building the app (which packages up into a self-contained jar) and then adding it to the Docker image (which is what I want).
I am following the instructions from the OpenJDK Docker base image here. You can find my SSCCE at this Bootup repo on GitHub, whose README has all the instructions to reproduce what I'm seeing. But basically:
I build the web app into a jar
Run docker build -t bootup . which succeeds
Run docker run -it --rm --name bootup bootup which gives me the error below and then exits
The error:
/bin/sh: 1: /bin/sh: [java,: not found
According to the Google Gods, this used to be a problem with the Oracle JDK images, but should not be happening with OpenJDK images.
Looking at my Dockerfile (which is also up in that GitHub repo), can anyone spot where I'm going awry:
FROM openjdk:8
RUN mkdir /opt/bootup
ADD build/libs/bootup.jar /opt/bootup
WORKDIR /opt/bootup
ENTRYPOINT ['java', '-jar', 'bootup.jar']
CMD ['']
Thanks in advance!
Update:
Output of docker ps:
CONTAINER ID IMAGE COMMAND CREATED
16bde964ff6b bootup "/bin/sh -c 'java -ja" 2 days ago
STATUS PORTS NAMES
Up 14 seconds 0.0.0.0:8080->8080/tcp bootup
I had it working fine using this dockerfile:
FROM openjdk:8
RUN mkdir /opt/bootup
ADD build/libs/bootup.jar /opt/bootup
WORKDIR /opt/bootup
EXPOSE 8080
ENTRYPOINT java -jar bootup.jar
It runs just fine with this commad:
docker run -it -p 8080:8080 --name bootup bootup
I am no java developer and I don't know why it ignores your configuration that requires it to start on port 9200, since your app starts on port 8080, but from a docker point of view everything is working with my config and I can connect to the app from my browser on localhost:8080
Here the screenshot:
Also, since you posted your github repo I suggest you to modify the readme so that users can start gradle from docker without the need of a java environment in the host machine running this one time command:
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:8 /usr/src/myapp/gradlew clean build

How to deploy webapp or war file in tomcat that is running in docker container

I have created Docker container and Tomcat is running in this container. How can I deploy a webapp or war file in Tomcat that is running in docker container.
First create a Dockerfile:
FROM library/tomcat
RUN rm -rf /usr/local/tomcat/webapps/*
ADD ./relative/path_to_war.war /usr/local/tomcat/webapps/ROOT.war
Then build Docker image
$ docker build -t user/image_name .
And finally run docker container.
$ docker run --name container_name -p 80:8080 -d user/image_name
After that your webapp should be responding on Docker host's ip on default http 80 port.
You might need to link a database container to your webapp, see more on Docker documentation
You can implement an effective way of using Tomcat Docker.
docker run -d -p 80:8080 -v <mount-path>:/usr/local/tomcat/webapps/ tomcat:8.0
And then copy the .war files to the mounted volume. This would eliminate the need for restarting the Tomcat Docker each time when there is a code change. A zero downtime implementation could be achieved.
If it's a one-time deployment then you may create a custom Docker and copy the war file into /usr/local/tomcat/webapps/ and start the Docker.

Docker add warfile to official Tomcat image

I pulled official Docker image for Tomcat by running this command.
docker run -it --rm tomcat:8.0
By using this as base image I need to build new image that contains my war file in the tomcat webapps folder. I created Dockerfile like this.
From tomcat8
ADD warfile /usr/local/tomcat
When I run this Dockerfile by building image I am not able to see Tomcat front page.
Can anybody tell me how to add my warfile to official Tomcat images webapp folder.
Reading from the documentation of the repo you would do something like that
FROM tomcat
MAINTAINER xyz
ADD your.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
Then build your image with docker build -t yourName <path-to-dockerfile>
And run it with:
docker run --rm -it -p 8080:8080 yourName
--rm removes the container as soon as you stop it
-p forwards the port to your host (or if you use boot2docker to this IP)
-it allows interactive mode, so you see if something get's deployed
Building on #daniel's answer, if you want to deploy your WAR to the root of tomcat, I did this:
FROM tomcat:7-jre7
MAINTAINER xyz
RUN ["rm", "-fr", "/usr/local/tomcat/webapps/ROOT"]
COPY ./target/your-webapp-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
It deletes the existing root webapp, copies your WAR to the ROOT.war filename then executes tomcat.
docker run -it --rm --name MYTOMCAT -p 8080:8080 -v .../wars:/usr/local/tomcat/webapps/ tomcat:8.0
where wars folder contains war to deploy
How do you check the webapps folder?
The webapps folder is within the docker container.
If you want to access your webapps container you could mount a host directory within your container to use it as webapps folder. That way you can access files without accessing docker.
Details see here
To access your logs you could do that when you run your container e.g.
docker run -rm -it -p 8080:8080 **IMAGE_NAME** /path/to/tomcat/bin/catalina.sh && tail -f /path/to/tomcat/logs
or you start your docker container and then do something like:
docker exec -it **CONTAINER_ID** tail -f /path/to/tomcat/logs
If you are using spring mvc project then you require server to run your application suppose you use tomcat then you need base image of tomcat that your application uses which you can specify through FROM command.
You can set environment variable using ENV command.
You can additionally use RUN command which executes during Docker Image buiding.
eg to give read write execute permissions to webapps folder for tomcat to unzip war file
RUN chmod -R 777 $CATALINA_HOME/webapps
And one more command is CMD. Whatever you specifying in CMD command it will execute at a time of container running. You can specify options in CMD command using double quotes(" ") seperated by comma(,).
eg
CMD ["catalina.sh","start"]
(NOTE : Remember RUN command execute at a time of image building and CMD execute at a time of running container this is confusing for new users).
This is my Dockerfile -
FROM tomcat:9.0.27-jdk8-openjdk
VOLUME /tmp
RUN chmod -R 777 $CATALINA_HOME/webapps
ENV CATALINA_HOME /usr/local/tomcat
COPY target/*.war $CATALINA_HOME/webapps/myapp.war
EXPOSE 8080
CMD ["catalina.sh","run"]
Build your image using command
docker build -t imageName <path_of_Dockerfile>
check your docker image using command
docker images
Run image using command
docker run -p 9999:8080 imageName
here 8080 is tomcat port and application can access on 9999 port
Try accessing your application on
localhost:9999/myapp/

Why is Docker Tomcat failing to start?

I am trying to build a Tomcat image from a Dockerfile. This is what my Dockerfile looks like:
FROM dockerfile/java
RUN sudo apt-get update
RUN sudo apt-get install tomcat7
EXPOSE 8086
CMD sudo service tomcat7 start && tail -f /var/log/tomcat7/catalina.out.
but when I build an image from this and run the image with
$ docker run tomcat7-test
it gives the following:
Starting Tomcat servlet engine tomcat7 …fail!
I don’t know what is causing the problem. How can I check the logs of this Docker Tomcat? Can anybody tell me what commands I should use in the Dockerfile to run Tomcat?
There is an official Tomcat image you can use. There are links to the Dockerfiles there to checkout and install Tomcat.
If you want to inspect what is going on when you build your dockerfile, just perform the same steps (apt-getting tomcat7 and starting the service) manually after starting an interactive shell inside the dockerfile/java container with this command:
docker -it dockerfile/java bash
There you will be able to check the logs and see what could be going on.
I did install tomcat server in the docker container instead of using the official Tomcat image.
When I start the server I get the fail response but was curl the tomcat server index page.
Also instead of exiting the container, if you detach from it back to your terminal by typing:
ctrl-p then ctrl-q (source)
You can access your webapps from browser using the below URL:
http://<< boot2docker_ip >>:8080
Try running the image with following command
docker run -dt --cap-add SYS_PTRACE -p 8082:8080 tomcat7-test

Resources