How to deploy a few wars to root tomcat? - docker

I need a few deployed war on server and it
should look like:
localhost:8080/war1/run_app1
localhost:8080/war2/run_app2
I tried such approach:
FROM tomcat
MAINTAINER user1
#VOLUME /usr/local/tomcat/webapps
RUN ["rm", "-fr", "/usr/local/tomcat/webapps/ROOT"]
ADD /src/main/docker/run_app1.war /usr/local/tomcat/webapps/war1/
COPY /src/main/docker/run_app1.war /usr/local/tomcat/webapps/war1/ROOT.war
CMD ["catalina.sh", "run"]
But it's not working... Any idea how to implement such thing?

Its pretty simple, just rename the WAR files!
taking your .war file example:
If you want war1.war file to run as "localhost:8080/war1/run_app1" just rename it:
mv war1.war war1#run_app1.war
and copy this file to TOMCAT_DIR/webapps/
Accordingly you can change the commandline (filenames) in your docker.
Please let me know if you have any doubt or you are looking for a different solution.
Thanks,
Nishant

Related

Changing Tomcat config - context.xml through dockerfile

I have developed a web app (maven, tomcat) in Intellij and managed to create a container through 'Services' tab in Intellij which was straightforward thanks to easy deployment config. During the process, I encountered that the cache size was not enough so I manually changed the context.xml (added <Resources cacheMaxSize="51200" />) file of tomcat manually locally after which the app ran smoothly.
To summarize the container creation in Intellij under services tab (see the bottom for the image):
- pulling an image: tomcat:9.0.65-jre8
- container name
- bind ports: 127.0.0.1:8080:8080
- bind mounts: mount host path which contains the war WITH /usr/local/tomcat/webapps
Though not sure, I guess the war file I created took already into account the change I made in the context.xml file since my application server is the tomcat I downloaded and made the change on its context.xml.
However, I also need to create a container with a dockerfile:
My dockerfile is:
FROM maven:3.8.4-jdk-8 as maven_builder
COPY . /usr/src/maven_pdfparse
WORKDIR /usr/src/maven_pdfparse
RUN mvn clean install -f /usr/src/maven_pdfparse && mkdir /usr/src/wars/
RUN find /usr/src/maven_pdfparse/ -iname '*.war' -exec cp {} /usr/src/wars/ \;
ADD pom.xml .
FROM tomcat:9.0.65-jre8
COPY --from=maven_builder /usr/src/wars/* /usr/local/tomcat/webapps
When I ran this on docker, I again got the 'insufficient cache' issue.
So how can I make the same change on context.xml when creating a dockerfile?
Or is there a way to get the dockerfile automatically when I create the container through deployment configuration?
You have a few options to choose from if you wish to add to or modify the context.xml file.
You can modify the context.xml file already within the image as part of the image build. Add a RUN command using a command line tool like sed to add the required <Resources> element to the file.
You could also have a pre-modified version of the file and just copy it into the image to overwrite the existing one.
You could add a custom startup command that modifies the context.xml (e.g. using sed as per option 1) before invoking the usual tomcat startup script. Using this mechanism you could also get the cacheMaxSize value to use from an environment variable and thus allow run-time control of the value.
Please check this answer
Tomcat 8 throwing - org.apache.catalina.webresources.Cache.getResource Unable to add the resource
Where you'll need to update context.xml and rebuild your image with copying context.xml

Creating a dockerfile to compile source code

I am trying to follow the 2 steps mentioned below:
1) Downloaded source code of
https://sourceforge.net/projects/hunspell/files/Hyphen/2.8/hyphen-2.8.8.tar.gz/download
2) Compiled it and you will get binary named example:
hyphen-2.8.8$ ./example ~/dev/smc/hyphenation/hi_IN/hyph_hi_IN.dic
~/hi_sample.text
I have downloaded and uncompressed the tar file. My question is how to create a dockerfile to automate this?
There are only 3 commands involved:
./configure
make all-recursive
make install
I can select the official python image as a base container. But how do I write the commands in a docker file?
You can do that with a RUN command:
FROM python:<version number here>
RUN ./configure && make-recursive && make install
CMD ['<some command here>']
what you use for <some command here> depends on what the image is meant to do. Remember that docker containers only run as long as that command is executing, so if you put the configure/make/install steps in a script and use that as your entry point, it's going to build your program, and then the container will halt.
Also you need to get the downloaded files into the container. That can be done using a COPY or an ADD directive (before the RUN of course). If you have the tar.gz file saved locally, then ADD will both copy the file into the container and expand it into a directory automatically. COPY will not expand it, so if you do that, you'll need to add a tar -zxvf or similar to the RUN.
If you want to download the file directly into the container, that could be done with ADD <source URL>, but in that case it won't expand it, so you'll have to do that in the RUN. COPY doesn't allow sourcing from a URL. This post explains COPY vs ADD in more detail.
You can have the three commands in a shell script and then use the following docker commands
COPY ./<path to your script>/<script-name>.sh /
ENTRYPOINT ["/<script-name>.sh"]
CMD ["run"]
For reference, you can create your docker file as they have created for one of the projects I worked on Apache Artemis Active Mq:
https://github.com/apache/activemq-artemis/blob/master/artemis-docker/Dockerfile-ubuntu

Docker and trying to build an image using Azure Pipelines

Hopefully someone can help me see the wood for the trees as they say!
I am no Linux expert and therefore I am probably missing something very obvious.
I have a dockerfile which contains the following:
FROM node:9.8.0-alpine as node-webapi
EXPOSE 3000
LABEL authors="David Sheardown"
COPY ["package.json", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . /home/vsts/work/1/s/
CMD ["node", "index.js"]
I then have an Azure pipeline setup as the following image shows:
My issue seems to be the build process cannot find the dockerfile itself:
##[error]Unhandled: No Dockerfile matching /home/vsts/work/1/s/**/Dockerfile was found.
Again, apologies in advance for my lack of Linux knowledge.. there is something silly I have done or not done ;)
P.S: I forgot to mention in Azure Pipelines I am using "Hosted Linux Preview"
-- UPDATE --
This is the get sources stage:
I would recommend adding the exact path to where the docker file resides on your repository .
Dockerfile: subpath/Dockerfile`
You're misusing this absolute path, both within the dockerfile and in the docker build task:
/home/vsts/work/1/s/
That is a path that exists on the build agent (not within the dockerfile) - but it may or may not exist on any given pipeline run. If the agent happens to use work directory 2, or 3, or any other number, then your path will be invalid. If you want to run this pipeline on a different type of agent, then your path will be invalid.
If you want to use a dockerfile in your checked out code, then you should do so by using a relative path (based on the root of your code repository), for example:
buildinfo/docker/Dockerfile
Note: that was just an example, to show the kind of path you should use; here you should be using the actual relative path in your actual code repo.

Docker Copy - Windows

I am currently trying to copy a folder and its sub directories to a docker container but all that copies in is the folder structure "obj\Docker\empty"
I am running the command in Powershell from D:\Sites\Web.API and the command is:
docker cp . eac334ba8bf6:./inetpub/wwwroot/Web.API.
My .dockerignore file has this in it
!obj\Docker\publish\*
!obj\Docker\empty\
I'm pretty new to this so may be something silly but currently all out of ideas !
I think the issue is file system permissions. Have you tried to copy it somewhere else?

docker add "requires at least one argument" error

I have a folder which contains all the necessary components for an app, which I want to make a container of. I have everything set up so far, with the directory /home/user/Documents/App in the Dockerfile under the ADD heading. Then when Idocker build . in the App directory I get this
ADD /home/user/Documents/App
ADD requires at least one argument
I realize that this is probably a simple fix but I am new to this so any help would be greatly appreciated. Thank you
FROM alpine
ADD </home/user/Documents/App> </home/user/Documents/DockerApp>
WORKDIR /code
RUN pip install -r requirements.txt
EXPPOSE 8080
CMD ["python", "app.py"]
You need a source and destination for the ADD command. The source here is the app folder path. The destination should be where you want the dockerfile is run.
Try this I think it might work
ADD defined in Dockerfile has the following structure
ADD sourceJarName destinationJarName
e.g.
ADD target/spring-boot-rest-docker-example-0.0.1-SNAPSHOT.jar app.jar
change your ADD likewise and try it will work

Resources