Dockerizing a Java Web Application and Deploying to Alibaba Cloud Container Service
This tutorial demonstrates how to containerize a Java web application with Docker, build and test it using Maven, create a custom Tomcat‑based image, push the image to Alibaba Cloud’s registry, and finally deploy the service through Alibaba Cloud Container Service.
Docker has become extremely popular because it solves environment consistency problems and greatly improves development and operations efficiency, embodying the DevOps concept.
The tutorial uses Alibaba Cloud Code for source control, Alibaba Cloud Developer Platform as a container hub, and Alibaba Cloud Container Service for deployment.
Step 1: Clone the project and compile. First, clone the repository from Alibaba Cloud Code (replace with your username and password):
$ git clone https://code.aliyun.com/zju_lb/java-with-docker/tree/masterFor a Java project, compile the source with Maven: $ mvn clean package -D maven.test.skip=true Run a simple test to verify the build: $ mvn test The resulting boot-api.war file appears in the target directory.
Step 2: Build and push the Docker image. Pull the official Tomcat image (using Alibaba Cloud’s accelerated hub) and create a custom Dockerfile that copies the WAR file into the container:
FROM registry.aliyuncs.com/zju_lb/tomcat:latest
COPY sources.list /etc/apt/sources.list
RUN apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 40976EAF437D05B5
RUN apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 3B4FE6ACC0B21F32
# Install common tools
RUN apt-get update && apt-get -y install curl gcc automake autoconf make git wget xz-utils
# Set timezone
ENV TZ "PRC"
RUN echo "Asia/Shanghai" | tee /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata
# Copy the WAR into Tomcat
COPY ./target/boot-api.war /usr/local/tomcat/webapps
CMD ["catalina.sh","run"]Build the image locally and run it to verify: $ docker run -p 9999:8080 -it java-with-docker After confirming the service works (access dockerMachineIP:9999/boot-api), push the image to Alibaba Cloud’s registry:
$ sudo docker login [email protected] registry.aliyuncs.com
$ sudo docker tag java-with-docker registry.aliyuncs.com/crp/java-with-docker
$ sudo docker push registry.aliyuncs.com/crp/java-with-dockerStep 3: Deploy the container service. Log in to the Alibaba Cloud console, enable Container Service, create an application, select “Use image to create”, and choose the pushed image java-with-docker. Add a port‑forwarding rule (e.g., 9999 → 8080) in the Web routing settings.
After the application is created, Alibaba Cloud provides an endpoint URL that can be used to access the Java web service from the internet.
In summary, the article shows a complete end‑to‑end workflow: from source code to Maven build, Docker image creation, registry push, and cloud deployment, illustrating a practical DevOps pipeline for Java applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
