docker-compose 一键部署

3.3.DockerCompose

大家可以看到,我们部署一个简单的java项目,其中包含3个容器:
  • MySQL
  • Nginx
  • Java项目
而稍微复杂的项目,其中还会有各种各样的其它中间件,需要部署的东西远不止3个。如果还像之前那样手动的逐一部署,就太麻烦了。
 
而Docker Compose就可以帮助我们实现多个相互关联的Docker容器的快速部署。它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器。
 

3.3.1.基本语法

docker-compose.yml文件的基本语法可以参考官方文档:
https://docs.docker.com/compose/compose-file/compose-file-v3/
 
docker-compose文件中可以定义多个相互关联的应用容器,每一个应用容器被称为一个服务(service)。由于service就是在定义某个应用的运行时参数,因此与docker run参数非常相似。
举例来说,用docker run部署MySQL的命令如下:
docker run -d \
        --name mysql \
        -p 3306:3306 \
        -e TZ=Asia/Shanghai \
        -e MYSQL_ROOT_PASSWORD=123 \
        -v ./mysql/data:/var/lib/mysql \
        -v ./mysql/conf:/etc/mysql/conf.d \
        -v ./mysql/init:/docker-entrypoint-initdb.d \
        --network hmall
        mysql
如果用docker-compose.yml文件来定义,就是这样:
version: "3.8"

        services:
        mysql:
        image: mysql
        container_name: mysql
        ports:
        - "3306:3306"
        environment:
        TZ: Asia/Shanghai
        MYSQL_ROOT_PASSWORD: 123
        volumes:
        - "./mysql/conf:/etc/mysql/conf.d"
        - "./mysql/data:/var/lib/mysql"
        networks:
        - new
        networks:
        new:
        name: hmall
对比如下:
docker run 参数
docker compose 指令
说明
--name
container_name
容器名称
-p
ports
端口映射
-e
environment
环境变量
-v
volumes
数据卷配置
--network
networks
网络
 
明白了其中的对应关系,相信编写docker-compose文件应该难不倒大家。
 
黑马商城部署文件:
version: "3.8"

        services:
        mysql:
        image: mysql
        container_name: mysql
        ports:
        - "3306:3306"
        environment:
        TZ: Asia/Shanghai
        MYSQL_ROOT_PASSWORD: 123
        volumes:
        - "./mysql/conf:/etc/mysql/conf.d"
        - "./mysql/data:/var/lib/mysql"
        - "./mysql/init:/docker-entrypoint-initdb.d"
        networks:
        - hm-net
        hmall:
        build:
        context: .
        dockerfile: Dockerfile
        container_name: hmall
        ports:
        - "8080:8080"
        networks:
        - hm-net
        depends_on:
        - mysql
        nginx:
        image: nginx
        container_name: nginx
        ports:
        - "18080:18080"
        - "18081:18081"
        volumes:
        - "./nginx/nginx.conf:/etc/nginx/nginx.conf"
        - "./nginx/html:/usr/share/nginx/html"
        depends_on:
        - hmall
        networks:
        - hm-net
        networks:
        hm-net:
        name: hmall

3.3.2.基础命令

编写好docker-compose.yml文件,就可以部署项目了。常见的命令:
https://docs.docker.com/compose/reference/
 
基本语法如下:
docker compose [OPTIONS] [COMMAND]
其中,OPTIONS和COMMAND都是可选参数,比较常见的有:
类型
参数或指令
说明
Options
-f
指定compose文件的路径和名称
-p
指定project名称。project就是当前compose文件中设置的多个service的集合,是逻辑概念
Commands
 
up
创建并启动所有service容器
down
停止并移除所有容器、网络
ps
列出所有启动的容器
logs
查看指定容器的日志
stop
停止容器
start
启动容器
restart
重启容器
top
查看运行的进程
exec
在指定的运行中容器中执行命令
 
教学演示:
# 1.进入root目录
        cd /root

        # 2.删除旧容器
        docker rm -f $(docker ps -qa)

        # 3.删除hmall镜像
        docker rmi hmall

        # 4.清空MySQL数据
        rm -rf mysql/data

        # 5.启动所有, -d 参数是后台启动
        docker compose up -d
        # 结果:
        [+] Building 15.5s (8/8) FINISHED
        => [internal] load build definition from Dockerfile                                    0.0s
        => => transferring dockerfile: 358B                                                    0.0s
        => [internal] load .dockerignore                                                       0.0s
        => => transferring context: 2B                                                         0.0s
        => [internal] load metadata for docker.io/library/openjdk:11.0-jre-buster             15.4s
        => [1/3] FROM docker.io/library/openjdk:11.0-jre-buster@sha256:3546a17e6fb4ff4fa681c3  0.0s
        => [internal] load build context                                                       0.0s
        => => transferring context: 98B                                                        0.0s
        => CACHED [2/3] RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo   0.0s
        => CACHED [3/3] COPY hm-service.jar /app.jar                                           0.0s
        => exporting to image                                                                  0.0s
        => => exporting layers                                                                 0.0s
        => => writing image sha256:32eebee16acde22550232f2eb80c69d2ce813ed099640e4cfed2193f71  0.0s
        => => naming to docker.io/library/root-hmall                                           0.0s
        [+] Running 4/4
        ✔ Network hmall    Created                                                             0.2s
        ✔ Container mysql  Started                                                             0.5s
        ✔ Container hmall  Started                                                             0.9s
        ✔ Container nginx  Started                                                             1.5s

        # 6.查看镜像
        docker compose images
        # 结果
        CONTAINER           REPOSITORY          TAG                 IMAGE ID            SIZE
        hmall               root-hmall          latest              32eebee16acd        362MB
        mysql               mysql               latest              3218b38490ce        516MB
        nginx               nginx               latest              605c77e624dd        141MB

        # 7.查看容器
        docker compose ps
        # 结果
        NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
        hmall               root-hmall          "java -jar /app.jar"     hmall               54 seconds ago      Up 52 seconds       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
        mysql               mysql               "docker-entrypoint.s…"   mysql               54 seconds ago      Up 53 seconds       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
        nginx               nginx               "/docker-entrypoint.…"   nginx               54 seconds ago      Up 52 seconds       80/tcp, 0.0.0.0:18080-18081->18080-18081/tcp, :::18080-18081->18080-18081/tcp
打开浏览器,访问:http://yourIp:8080
posted @ 2023-12-15 15:15  会秃头的小白  阅读(151)  评论(0编辑  收藏  举报