目录
二、docker常用部署脚本Dockerfile、docker-compose
前言:
新手小白的常用笔记,长期更新这篇文章
一、数据库备份 sh
#!/bin/bash
# 配置数据库连接信息
DB_USER="root"
DB_PASSWORD="password"
DB_NAME="mydatabase"
BACKUP_PATH="/path/to/backup/directory"
# 生成备份文件名
TIMESTAMP=$(date +"%F")
BACKUP_FILE="${BACKUP_PATH}/${DB_NAME}_${TIMESTAMP}.sql"
# 执行备份
mysqldump -u ${DB_USER} -p${DB_PASSWORD} ${DB_NAME} > ${BACKUP_FILE}
# 检查备份是否成功
if [ $? -eq 0 ]; then
echo "数据库备份成功: ${BACKUP_FILE}"
else
echo "数据库备份失败"
exit 1
fi
二、docker常用部署脚本Dockerfile、docker-compose
1.mysql-Dockerfile
# Use the official MySQL image from the Docker Hub
FROM mysql:8.0
# Set environment variables
ENV MYSQL_ROOT_PASSWORD=root_password
ENV MYSQL_DATABASE=my_database
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=password
# Expose the default MySQL port
EXPOSE 3306
# Create volumes for data, configuration, and logs
VOLUME ["/var/lib/mysql", "/etc/mysql/conf.d", "/var/log/mysql"]
2.redis-Dockerfile
# Use the official Redis image from the Docker Hub
FROM redis:6.2
# Expose the default Redis port
EXPOSE 6379
# Create volumes for data, configuration, and logs
VOLUME ["/data", "/usr/local/etc/redis", "/var/log/redis"]
3.springboot-Dockerfile
# Use the official OpenJDK image as the base image
FROM openjdk:11-jre-slim
# Set the working directory
WORKDIR /app
# Copy the jar file to the working directory
COPY target/my-spring-boot-app.jar /app/my-spring-boot-app.jar
# Expose the port the app runs on
EXPOSE 8080
# Create volumes for logs and configuration
VOLUME ["/app/logs", "/app/config"]
# Command to run the application
CMD ["java", "-jar", "my-spring-boot-app.jar"]
4.vue-Dockerfile
# Build stage
FROM node:16-alpine as build-stage
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Build the application
RUN npm run build
# Production stage
FROM nginx:alpine
# Copy the built files from the build stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
# Expose the port Nginx will run on
EXPOSE 80
# Create volumes for logs and configuration
VOLUME ["/var/log/nginx", "/etc/nginx"]
# Command to run Nginx
CMD ["nginx", "-g", "daemon off;"]
5.nginx-cnofig
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
error_log /var/log/nginx/error.log warn;
access_log /var/log/nginx/access.log main;
location /api {
proxy_pass http://backend:8080; # 配置后端API的代理地址
}
}
6.docker-compose.yml
version: '3.8'
services:
mysql:
build: ./mysql
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: my_database
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./mysql/conf.d:/etc/mysql/conf.d
- mysql_logs:/var/log/mysql
redis:
build: ./redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
- ./redis/conf:/usr/local/etc/redis
- redis_logs:/var/log/redis
spring_boot_app:
build: ./spring-boot-app
ports:
- "8080:8080"
volumes:
- spring_logs:/app/logs
- ./spring-boot-app/config:/app/config
vue_app:
build: ./vue-app
ports:
- "80:80"
volumes:
- vue_logs:/var/log/nginx
- ./vue-app/nginx.conf:/etc/nginx/nginx.conf:ro
volumes:
mysql_data:
redis_data:
spring_logs:
mysql_logs:
redis_logs:
vue_logs:
7.项目结构目录
project-root/
│
├── docker-compose.yml
│
├── mysql/
│ ├── Dockerfile
│ └── conf.d/
│ └── my.cnf
│
├── redis/
│ ├── Dockerfile
│ └── conf/
│ └── redis.conf
│
├── spring-boot-app/
│ ├── Dockerfile
│ ├── config/
│ │ └── application.properties
│ └── target/
│ └── my-spring-boot-app.jar
│
└── vue-app/
├── Dockerfile
├── package.json
├── package-lock.json
├── nginx.conf
├── config/
│ └── some-config-file.json
└── ...(Vue 项目的其他文件)