如何在同一Docker容器中部署自定义Java应用与Nginx反向代理?
Alright, let's walk through how to package your Java app and Nginx together in a single Docker container with Nginx acting as a reverse proxy. First, a quick heads-up: Docker best practices usually suggest splitting services into separate containers (one for Java, one for Nginx) for scalability and maintainability, but I'll stick strictly to your requirement here.
First, set up this file structure in your project directory:
your-java-nginx-project/ ├── Dockerfile # Defines the container build process ├── nginx.conf # Nginx reverse proxy configuration ├── start.sh # Script to start both Java app and Nginx └── your-app.jar # Your compiled Java application JAR file
Create the nginx.conf file with the following content. This configures Nginx to listen on port 80 (the standard HTTP port) and forward all requests to your Java app running on port 8080 (adjust the port if your app uses a different one):
events { worker_connections 1024; } http { server { listen 80; server_name localhost; # Forward all requests to the Java app location / { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } }
Since Docker containers only stay running if the main process is active, we need a script to start the Java app in the background and run Nginx in the foreground. Create start.sh with:
#!/bin/bash # Start the Java application in the background java -jar /app/your-app.jar & # Start Nginx in the foreground (keeps the container alive) nginx -g "daemon off;"
Don't forget to make this script executable later (we'll handle that in the Dockerfile).
This file will build a container with both Java and Nginx installed, copy your files into it, and set up the runtime. Use this content:
# Use a slim Debian base image to keep the container small FROM debian:bookworm-slim # Update package lists and install required dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ openjdk-17-jre-headless \ nginx \ && rm -rf /var/lib/apt/lists/* # Create a directory for your Java app RUN mkdir -p /app # Copy your Java JAR, Nginx config, and startup script into the container COPY your-app.jar /app/ COPY nginx.conf /etc/nginx/nginx.conf COPY start.sh /app/ # Make the startup script executable RUN chmod +x /app/start.sh # Expose port 80 to the host machine EXPOSE 80 # Run the startup script when the container starts CMD ["/app/start.sh"]
Note: If your Java app requires a different JDK/JRE version, adjust the openjdk-17-jre-headless package to match (e.g., openjdk-11-jre-headless for Java 11).
Once all files are in place, run these commands from your project directory:
# Build the Docker image docker build -t java-nginx-proxy . # Run the container, mapping host port 80 to container port 80 docker run -d -p 80:80 java-nginx-proxy
You can now access your app via http://localhost in your browser—Nginx will forward the request to your Java app automatically.
- Logging: To view logs for both the Java app and Nginx, use
docker logs <container-id>(replace<container-id>with your container's ID, which you can get fromdocker ps). - Container Lifecycle: Nginx runs in the foreground, so if Nginx stops, the container will exit. The Java app runs in the background—if it crashes, you'll need to add health checks or monitoring to restart it (or consider switching to multi-container setup for better resilience).
- Best Practices Reminder: While this works for your use case, multi-container setups (using Docker Compose) are better for production—they let you scale each service independently, update them separately, and isolate failures.
内容的提问来源于stack exchange,提问作者yogisha

