Install Nginx with Docker and Register as a Service for Auto-Start on Boot (systemctl operations)
11/22/24About 1 min
Install Nginx with Docker and Register as a Service for Auto-Start on Boot (systemctl operations)
Install and Configure Nginx
- Pull the image first
docker pull dockerpull.org/nginx:1.16.1 # Use the dockerpull.org source- Create a new
docker-compose.ymlfile
version: "3"
services:
web:
# Define hostname
container_name: mynginx
# Image to use
image: dockerpull.org/nginx:1.16.1
# Port mapping for the container
ports:
- 80:80
# Define mount points
volumes:
- ./html:/usr/share/nginx/html
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./conf.d:/etc/nginx/conf.d
- ./logs:/var/log/nginx
# Auto-start container after Docker restarts
restart: always- Start Docker
docker compose up -dRegister as a Service for Container Auto-Start on Boot
- Create a systemd service file
Create a new service file in the /etc/systemd/system/ directory
sudo nano /etc/systemd/system/docker-compose-app.service- Add the following content to the file
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
WorkingDirectory=/path/to/your/docker-compose/project
ExecStart=docker compose up -d
ExecStop=docker compose down
[Install]
WantedBy=multi-user.target- Enable and start the service
sudo systemctl enable docker-compose-app.service
sudo systemctl start docker-compose-app.serviceRegister Starting Java Backend as a Service
[Unit]
Description=Spring Boot Application
After=syslog.target
[Service]
User=lzc
WorkingDirectory=/home/lzc/java/jars
ExecStart=/home/lzc/java/openlogic-openjdk-jre-17.0.13+11-linux-x64/bin/java -jar /home/lzc/java/jars/dpInspect-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
TimeoutStopSec=10
Restart=no
RestartSec=5
[Install]
WantedBy=multi-user.targetReferences
AI Translation | AI 翻译
This article was translated from Chinese to English by AI. If there are any inaccuracies, please refer to the original Chinese version.
本文由 AI 辅助从中文翻译为英文。如遇不准确之处,请以中文原版为准。
