Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a maven webapp in a docker container

I'm trying to build a simple web app with Maven and run with Tomcat7, inside a Docker container.

This is my structure:

- Dockerfile
- pom.xml
- src/main/webapp/index.hmtl

This is my Dockerfile:

FROM java:8

# Install maven
RUN apt-get -y update && apt-get install -y maven

WORKDIR /code

# Prepare by downloading dependencies
ADD pom.xml /code/pom.xml

# Adding source, compile and package into a fat jar
ADD src /code/src
RUN ["mvn", "package"]

EXPOSE 8080
CMD ["mvn", "tomcat7:run"]

I'm building the Docker image with

docker build -t webapp-example .

and try to run it with

docker run -d -p 8080:8080 webapp-example

But apparently it doesn't work.

Any ideas?

like image 273
luthien Avatar asked Feb 16 '17 15:02

luthien


1 Answers

Since you shared running using tty and interactive flag like following solves your problem,

docker run -ti --rm -p 8080:8080 webapp-example

That is because your base image is java:8 which is primarily created to run application in front mode (with -ti flag) or compile only in -d mode.

Also, since maven is build tools and should not be used to run application, you should,

  1. Create you webapp using maven:latest image.
  2. Deploy it separately as tomcat container using official tomcat image.
like image 185
GauravJ Avatar answered Sep 30 '22 22:09

GauravJ