Deploying a Java WAR file inside a Docker container involves a few steps. You first need to write a Dockerfile that defines how to build a Docker image for your application, and then you need to run a container based on that image. Below is a general approach to accomplish this:
1. Prepare Your Dockerfile
A Dockerfile is a text file that contains all the commands needed to build a Docker image. For Java web applications, you typically start from a base image that includes a servlet container or an application server like Tomcat, Wildfly, or Jetty. Here's an example using Tomcat:
# Use an official Tomcat base image
FROM tomcat:9-jdk11-openjdk
# Delete the default ROOT web application, if you don't need it
RUN rm -rf /usr/local/tomcat/webapps/ROOT
# Copy your WAR file into the webapps directory of Tomcat
COPY target/myapp.war /usr/local/tomcat/webapps/ROOT.war
# Optional: Specify any environment variable your app might need
# Tomcat exposes port 8080, so let's make sure we expose that too
EXPOSE 8080
# No CMD needed, as the base image already specifies how to run Tomcat
In this Dockerfile, replace target/myapp.war with the path to the WAR file generated by your Maven build process. Then, customize any other parts as necessary for your application.
2. Build Your Docker Image
With your Dockerfile ready, navigate to the directory containing the Dockerfile in your terminal and run the following command to build your Docker image. Don't forget the dot . at the end, indicating the current directory.
docker build -t myapp:latest .
This command creates a new Docker image named myapp with the tag latest, based on the instructions in your Dockerfile.
3. Run Your Docker Container
Once the image is built, you can run it as a container:
docker run -d -p 8080:8080 --name myapp-container myapp:latest
This command starts a new container from your myapp image, maps port 8080 from the container to port 8080 on your host, and runs it in the background (-d). The container is given the name myapp-container.
4. Accessing Your Application
If everything went smoothly, your application should now be running inside the Docker container. You can access it by opening a web browser and navigating to http://localhost:8080.
5. Integrating with GitLab CI/CD
To integrate this process with GitLab CI/CD, you could add a deployment stage to your .gitlab-ci.yml that builds and pushes the Docker image and then optionally deploys it to your environment. Here's a simplified example:
stages:
- build
- deploy
build_war:
# Assuming this is your existing job definition
stage: build
script:
- mvn clean package
artifacts:
paths:
- target/*.war
deploy_to_docker:
stage: deploy
script:
- docker build -t myapp:$CI_COMMIT_SHORT_SHA .
- docker push myapp:$CI_COMMIT_SHORT_SHA
# Add deployment commands or scripts if needed
only:
- master
This .gitlab-ci.yml file has an added deployment stage that builds your Docker image tagged with the short commit SHA ($CI_COMMIT_SHORT_SHA) and pushes it to your Docker registry. You would need to adjust and expand upon this for a full deployment workflow, including handling credentials for pushing to your Docker registry and possibly deploying the container to a server or orchestration system like Kubernetes.
Don't forget that for pushing to a registry, you'd typically log in to the registry with a docker login command which would need credentials configured securely in your GitLab CI/CD settings.