docker学习笔记(六)-compose管理

154 阅读1分钟

一、安装、更新与卸载

库安装

sudo apt-get update
sudo apt-get install docker-compose-plugin

手动安装

# 下载
curl -SL https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose

# 加权
chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

独立安装

# 下载
curl -SL https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

# 

检查安装

sudo docker compose version

更新

sudo apt-get update
sudo apt-get install docker-compose-plugin

卸载

# apt库卸载
sudo apt-get update
sudo apt-get remove docker-compose-plugin

# 手动安装
sudo rm /usr/local/lib/docker/cli-plugins/docker-compose

二、使用

1、准备项目目录

# 项目根目录
sudo mkdir compose
# 进入根目录
cd compose

# 创建环境目录并准备各自的Dockerfile文件
# nginx环境
FROM nginx:1.25.3

2、创建compose.yaml管理文件

services:
  nginx:
    # 镜像
    image: nginx:1.25.3
    # 容器名称
    container_name: nginx_test
    # 暴露端口,用于绑定端口,相当于 -p
    ports:
      - "12000:80"
      - "12001:80"
           
 # Dockerfile构建
 services:
   nginx:
   container_name: nginx_build
   ports:
     - "13000:80"
     - "13001:80"
   build:
     context: .
     dockerfile: 
   volumes:
     # 使用sudo执行不要在~路径,建议使用相对路径
     # 主机挂载路径:镜像路径
     - ~/nginx/www:/usr/share/nginx/html
     - ~/nginx/conf.d:/etc/nginx/conf.d

3、构建

sudo docker compose up -d

4、构建nginx+php环境

services:
  nginx:
    # 容器名称
    container_name: compose1_nginx-1.25.3
    # 暴露端口
    ports:
      - "13000:80"
      - "13001:80"
    volumes:
      - ./nginx/www:/usr/share/nginx/html
      - ./nginx/conf.d:/etc/nginx/conf.d
    build:
      context: .
      dockerfile: ./nginx/Dockerfile

  php:
    container_name: compose1_php-8.3-fpm
    volumes:
      - ./nginx/www:/www
    build:
      context: .
      dockerfile: ./php/Dockerfile