Build a WeChat Public Account Backend with Docker: Step‑by‑Step Guide

This tutorial walks you through creating a Docker‑based backend for a WeChat public account, covering project structure, Nginx and Tomcat configuration, Maven build, Dockerfile creation, and deployment on DaoCloud with continuous integration.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a WeChat Public Account Backend with Docker: Step‑by‑Step Guide

Project Structure Overview

The project consists of several directories: \etc\nginx-conf – Nginx configuration for port forwarding. \etc\scripts – Startup script that launches Tomcat and Nginx. soft – Contains pre‑downloaded Maven and Tomcat packages. webapp – A standard Maven project holding the Java source code for the WeChat backend.

Key source files include WxApiServlet (handles incoming WeChat messages), MsgRequest, and various handler classes extending BaseHandler. The servlet uses the Turing robot API for auto‑replies, requiring an API key, and the token defined in Config.java must match the token set in the WeChat public platform.

Nginx Configuration

server {
    listen 80;
    server_name *.daoapp.io;
    location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080/;
    }
}

This forwards all requests on port 80 to Tomcat listening on port 8080.

Startup Script (start.sh)

#!/bin/sh
# Start Tomcat
$TOMCAT_HOME/bin/startup.sh
# Start nginx
nginx

Dockerfile

The Dockerfile builds an Ubuntu‑based image, installs JDK 7, Maven, Tomcat 7, copies the configuration files, compiles the Maven project, and deploys the generated WAR as ROOT.war in Tomcat.

# Base image
FROM ubuntu
# Maintainer
MAINTAINER saymagic "[email protected]"
# Install JDK and nginx
RUN apt-get update
RUN apt-get install -y openjdk-7-jre openjdk-7-jdk nginx
# Copy nginx config
ADD ./etc/nginx-conf /etc/nginx/conf.d
# Copy startup scripts
ADD ./etc/scripts /usr/local
RUN chmod a+x /usr/local/start.sh
# Add Maven and Tomcat packages
ADD ./soft /tmp
# Install Tomcat 7
RUN cd /usr/local && tar xzf /tmp/apache-tomcat-7.0.64.tar.gz
RUN ln -s /usr/local/apache-tomcat-7.0.64 /usr/local/tomcat
RUN rm /tmp/apache-tomcat-7.0.64.tar.gz
# Install Maven
RUN cd /usr/local && tar xzf /tmp/apache-maven-3.1.1-bin.tar.gz
RUN ln -s /usr/local/apache-maven-3.1.1 /usr/local/maven
RUN rm /tmp/apache-maven-3.1.1-bin.tar.gz
# Copy webapp source
RUN mkdir -p /webapp
ADD ./webapp /webapp
# Set environment variables
ENV TOMCAT_HOME /usr/local/tomcat
ENV MAVEN_HOME /usr/local/maven
ENV APP_HOME /webapp
# Build and deploy
RUN cd /webapp && /usr/local/maven/bin/mvn package
RUN rm -rf $TOMCAT_HOME/webapps/*
RUN cp target/wx_server.war $TOMCAT_HOME/webapps/ROOT.war
# Start services
CMD /usr/local/start.sh && tail -F $TOMCAT_HOME/logs/catalina.out
EXPOSE 80 8080

DaoCloud Deployment

After building the Docker image, push it to DaoCloud, which provides free CI resources. Bind your GitHub (or other) repository, create a new project named weixinserver, enable continuous integration, and deploy the latest image. DaoCloud will generate a public URL that you register as the server address in the WeChat public platform.

Testing

Subscribe to the newly created WeChat public account and send a message; if the server replies correctly, the Docker‑based backend is successfully running.

Summary

This article documents the complete process of containerizing a JavaWeb WeChat backend with Docker, covering configuration, build, and deployment steps.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendmavenNGINXTomcatWeChatJavaWeb
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.