docker-compose 部署项目

时游小于 1 分钟

docker-compose 部署项目

安装

# 下载 Docker Compose 的二进制文件
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 添加执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 验证安装,查看版本号
docker-compose --version

配置文件

配置 docker-compose.yml 文件

version: "3"
services:
    # 前端模块
    frontend:
        build:
            context: .
            dockerfile: Dockerfile
        # 依赖的模块
        depens_on:
            - nginx

    # nginx模块
    nginx:
        # 使用nginx镜像
        image: nginx
        # 主机端口号跟docker容器中端口号映射
        ports:
            - 9101:80
        # 挂载数据卷
        volumes:
            - ./nginx.conf:/etc/nginx/nginx.conf
            - ./dist:/user/share/nginx/html

配置 Dockerfile 文件

# 使用一个 Node.js 镜像作为基础镜像
FROM node:16

# 使用 Nginx 镜像作为最终镜像
FROM nginx:1.21.3

# 复制构建好的前端应用程序到 Nginx 默认的静态文件目录
COPY . /usr/share/nginx/html

# 暴露容器的 80 端口
EXPOSE 80

# 启动 Nginx 服务器
CMD ["nginx", "-g", "daemon off;"]

nginx 配置文件

# /nginx/default.conf
server {
    listen 80;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
    }
}

常用命令

# 启动
docker-compose up 
# 后台启动
docker-compose up -d

# 停止
Ctrl C

# 重新构建
docker-compose up --build

# 删除
docker-compose down
上次编辑于:
贡献者: minmengwei
Loading...