Dockerizing web app (WAR file) - docker

I want to Containerize a web application which is a WAR file along with Postgres as database and Tomcat as Server.
What will be the procedure to do that?
I am using the following dockerfile:
FROM tomcat:8-jre8 MAINTAINER lpradel
RUN echo "export JAVA_OPTS=\"-Dapp.env=staging\"" > /usr/local/tomcat/bin/setenv.sh
COPY ./application.war /usr/local/tomcat/webapps/staging.war
CMD ["catalina.sh", "run"]

Write a dockerfile for each application.
E.g: Base a dockerfile on a tomcat server docker image, copy over your warfile and start the tomcat in the cmd part of the dockerfile.
For postgres you should be able to use an existing image.

Updated answer:
The dockerfile you are using is correct. This should prepare a tomcat image which has the web application you want. However you may want to connect that tomcat container with postgress container.
Easier way to connect multiple containers would be to use a docker-compose file. To use docker-compose, refer to docker-compose#build.
Original answer
You can mount your war file in tomcat container at /usr/local/tomcat/webapps/name_of_your_file.war inside container. This will enable the war file to be automatically deployed by tomcat container.
By the way, I am doing the similar process and using mysql database. Taking a look at the my deployment file might be helpful to you.

Related

Docker setup but apache on local machine

I have a docker setup (php7,mysql and apache). This is working correctly.
However, I have to transfer the project on the server where there is already an apache running.
I was wondering how I could use the apache on the server to connect to my docker setup.
You can use either docker-compose or Dockerfile, or combination of both to use together.
You can read more about them in Docker Compose Docs and Dockerfile Docs.
In Simple Answer:
Create a docker-compose.yml file with contents you need as per above docs with a Dockerfile.
You should connect them together in your code by modifying files, like instead of localhost for database host, you should change it to the name you specify in docker-compose.yml file.
Also copying or adding some files in apache, like /etc/apache2/httpd.conf and /etc/apache2/sites-available/*.conf (all files ending with conf), and for mysql like /var/lib/mysql/ directory (database files), and of course your project files.
Then run docker-compose up -d command.

How can I deploy a MySQL database to Docker?

I want to deploy MYSQL Database to Docker container but I don't know how can I do that. I deployed my Spring Boot project and works fine. I created Dockerfile in this project:
FROM openjdk:8-jre-alpine
ADD ./target/Family-0.0.1-SNAPSHOT.jar Family-0.0.1-SNAPSHOT.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "Family-0.0.1-SNAPSHOT.jar"]
But I don't know how can I do with MySQL. I used MySQL Workbench when I created my database.
Ideally docker containers should one one service at a time, that means that you would have:
app-docker-container: Container with your Java application
db-docker-container: Container with your database
And then link the database db-docker-container to the app-docker-container. But this involves too many manual commands that might confuse you more, as an starting point I would suggest you to use docker-compose (link to an example).
You need to simply add MYSQL original image from docker-hub
https://hub.docker.com/_/mysql/ --> check for various in line commands to run
Add docker-compose.yml to link both the microservice and database.
https://github.com/thoopalliamar/Spring-Boot-And-Database-Docker -->check this to refer docker-compose.
network mode and hostname creates a network and links between them.
You need to stop the mysql in localmachine and to get into docker of mysql
sudo docker mysql -u root -p
to check the databases

Deploying Spring Boot App in Docker container without auto starting Tomcat

I was under the impression that including the line
CMD ["catalina.sh", "run"]
to the Dockerfile would be what triggers the Tomcat server to start.
I've removed that line but it still starts the server on deployment. I basically want to add the catalina.sh run and include CATALINA_OPTS all in a Kubernetes deployment to handle this stuff, but Tomcat still auto starts up when I deploy to a container.
Docker image usually has an entry point or a command already.
If you create your custom image based on another image from a registry, which is a common case, you can override base image entry point by specifying the ENTRYPOINT or the CMD directive.
If you omit the entry point in your docker file, it will be inherited from the base image.
Consider reading Dockerfile: ENTRYPOINT vs CMD article to have a better understanding of how it works.

Docker mount volumes is not working properly

I have spring application image which requires properties file to run successfully
I have started a jdk container and copied properties file to it. Now i am trying to run my spring imag(from jdk container) with properties file mounted as volume bind to the new container.
But, the volumes are not binded and the spring image is not running.
Docker command i am using is as below :
docker run -d -v /workspace/e2e-tests/resources/log4j2.xml:/opt/frauscher/message-filter/etc/log4j2.xml -v /workspace/e2e-tests/resources/message-filter-application.properties:/opt/frauscher/message-filter/etc/application.properties --name message docker-fts.rep01.frauscher.intern/message-filter:latest
I am getting below message from spring container :
log4j2.xml must be available under /opt/frauscher/message-filter/etc/log4j2.xml
application.properties must be available under /opt/frauscher/message-filter/etc/application.properties
Kindly help
here is a Dockerfile I'm using for my spring app, hopefully using this method you could run your app in docker or maybe pin point the problem
if this is not the solution you want to peruse, let me know and I will modify or delete this answer
FROM openjdk:8
WORKDIR opt
ADD app-0.0.1-SNAPSHOT.jar app.jar
ADD application.properties application.properties
ADD log4j2.xml log4j2.xml
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
all files app-0.0.1-SNAPSHOT.jar, application.properties, og4j2.xml should reside in the same folder as the Dockerfile
you can run docker build . and docker run <image> to start the container
Note: I'm using docker-compose to orchestrate multiple services like web, database and etc... this docker file is a draft taken out of my docker-compose configuration and modify to your needs (hopefully)
Thank you for answers. Finally, i figured it out. docker is unable to mount volumes as we are running container inside another container with any volumes mounted on first container. I tried copying files to new container. It worked as expected.

Add config.txt to docker container

We have a war which needs a configuration file to work.
We want to dockerize it. At the moment we're doing the following:
FROM tomcat:8.0
COPY apps/test.war /usr/local/tomcat/webapps/
COPY conf/ /config/
Our containers is losing the advantages of docker because it's dependent of the configfile. So when we want to execute the .war for other purposes we have to recreate the image which isn't a good approach.
Is it possible to give a config-file as a parameter without mounting it as a volume? Because we don't want the config on our local machine. What could be a solution?
You can pass it as an ENV but I don't see you losing the advantages of Docker. Docker is essentially all about temporary containers that are dispensable. Ideally you want to build a new image for every new app version.

Resources