windows10+docker desktop使用docker compose编排多容器构建dnmp环境

2023-06-01 00:00:00 容器 构建 编排

之前的文章都是单独搭建运行php容器、nginx容器、mysql容器、redis容器然后每个都要执行一条命令,比较麻烦。今天就来介绍一下docker compose容器编排工具,它就是把单独执行的容器命令组合起来放到一个文件docker-compose.yml中编排,使用它来自动部署多容器开发环境,这样就方便多了,以防命令多了混乱。


刚好我根据之前写的nginx,php,mysql,redis教程中的命令结合起来使用docker compose编排起来

实现一个由docker-compose.yml文件组合的自动化执行的demo


下面的单独安装nginx/php/mysql/redis教程我就不写了,

在本站docker分类下,截张图放下面,有兴趣的自行搜索

docker-lnmp.png


进入步骤:

创建docker-compose.yml编排文件,我已创建好

C:\Users\htz>cd dockers
C:\Users\htz\dockers>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 82E1-16B0
 C:\Users\htz\dockers 的目录
2022/08/11  17:06    <DIR>          .
2022/08/11  17:06    <DIR>          ..
2022/08/11  17:10             1,292 docker-compose.yml
2022/08/10  17:52    <DIR>          mysql
2022/08/04  16:07    <DIR>          nginx
2022/08/09  11:09    <DIR>          php
2022/08/11  18:08               279 php.dockerfile
2022/08/08  13:45    <DIR>          redis
               2 个文件          1,571 字节
               6 个目录 61,237,252,096 可用字节


把nginx,php,mysql,redis容器执行命令按规则写入docker-compose.yml文件中,

我已写好。  如下:

#版本号
version: '2.2'
services:
  #redis服务
  redis:
    #镜像名称
    image: redis
    ports:
      - "6379:6379"
    #容器名称  
    container_name: "redis-server"
    networks: 
      - dev
  mysql:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
    ports: 
      - "3306:3306"
    container_name: "mysql-server"
    networks: 
      - dev
  nginx:
    image: nginx
    ports: 
      - "80:80"
      - "8080:8080"
    container_name: "nginx-server"
    volumes:
      - C:\Users\htz\dockers\nginx\conf\nginx.conf:/etc/nginx/nginx.conf
      - C:\Users\htz\dockers\nginx\logs:/var/log/nginx
      - C:\Users\htz\dockers\nginx\www:/usr/share/nginx/html
    links:
      - "php74:7.4-fpm"
    networks: 
      - dev
  php74:
    build:
      context: ./
      dockerfile: php.dockerfile
    #image: php:7.4-fpm
    privileged: true
    ports: 
      - "9000:9000"
    container_name: "php74"
    volumes:
      - C:\Users\htz\dockers\nginx\www:/var/www/html
      - C:\Users\htz\dockers\php\conf\www.conf:/usr/local/etc/php-fpm.d/www.conf
      - C:\Users\htz\dockers\php\conf\php.ini:/usr/local/etc/php/php.ini
    networks:
      - dev
networks:
  dev:
    driver: bridge
  pro:
    driver: bridge

ps:

有看php.dockerfile这个文件吗,php74用了它,主要是用于安装一些依赖、扩展包使用build加载


在C:\Users\htz\dockers>目录中执行docker-compose命令构建项目

docker-compose up -d

以下是执行该命令后的过程,我直接复制下来

C:\Users\htz>cd dockers
C:\Users\htz\dockers>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 82E1-16B0
 C:\Users\htz\dockers 的目录
2022/08/11  17:06    <DIR>          .
2022/08/11  17:06    <DIR>          ..
2022/08/11  17:10             1,292 docker-compose.yml
2022/08/10  17:52    <DIR>          mysql
2022/08/04  16:07    <DIR>          nginx
2022/08/09  11:09    <DIR>          php
2022/08/11  18:08               279 php.dockerfile
2022/08/08  13:45    <DIR>          redis
               2 个文件          1,571 字节
               6 个目录 61,237,252,096 可用字节
C:\Users\htz\dockers>docker-compose up -d
WARNING: Some networks were defined but are not used by any service: pro
Creating network "dockers_dev" with driver "bridge"
Pulling redis (redis:)...
latest: Pulling from library/redis
1efc276f4ff9: Already exists
adf5288e41ff: Pull complete
2f387b0b685f: Pull complete
1b3d2bd4ccba: Pull complete
937c39fc1973: Pull complete
751dcbc45baa: Pull complete
Digest: sha256:9bc34afe08ca30ef179404318cdebe6430ceda35a4ebe4b67d10789b17bdf7c4
Status: Downloaded newer image for redis:latest
Pulling mysql (mysql:)...
latest: Pulling from library/mysql
32c1bf40aba1: Pull complete
3ac22f3a638d: Pull complete
b1e7273ed05e: Pull complete
20be45a0c6ab: Pull complete
410a229693ff: Pull complete
1ce71e3a9b88: Pull complete
c93c823af05b: Pull complete
c6752c4d09c7: Pull complete
d7f2cfe3efcb: Pull complete
916f32cb0394: Pull complete
0d62a5f9a14f: Pull complete
Digest: sha256:ce2ae3bd3e9f001435c4671cf073d1d5ae55d138b16927268474fc54ba09ed79
Status: Downloaded newer image for mysql:latest
Building php74
[+] Building 1.7s (7/7) FINISHED
 => [internal] load build definition from php.dockerfile                                                           0.0s
 => => transferring dockerfile: 36B                                                                                0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/php:7.4-fpm                                                     1.5s
 => [1/3] FROM docker.io/library/php:[email protected]:776f10bf1cf29c4b2ca81247f633e8b3c1385966ad888eef96ac5abb847c0  0.0s
 => CACHED [2/3] RUN apt-get update     && apt-get install -qq git curl libmcrypt-dev libjpeg-dev libpng-dev libf  0.0s
 => CACHED [3/3] RUN docker-php-ext-install pdo pdo_mysql                                                          0.0s
 => exporting to image                                                                                             0.0s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:4395a90d68809758149c2682e2bff6b1ead18189c280cdef267f88a18f0caf9b                       0.0s
 => => naming to docker.io/library/dockers_php74                                                                   0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service php74 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling nginx (nginx:)...
latest: Pulling from library/nginx
1efc276f4ff9: Already exists
baf2da91597d: Pull complete
05396a986fd3: Pull complete
6a17c8e7063d: Pull complete
27e0d286aeab: Pull complete
b1349eea8fc5: Pull complete
Digest: sha256:790711e34858c9b0741edffef6ed3d8199d8faa33f2870dea5db70f16384df79
Status: Downloaded newer image for nginx:latest
Creating redis-server ... done
Creating mysql-server ... done
Creating php74        ... done
Creating nginx-server ... done

在docker desktop软件中看看

3.png

ok到此就完成nginx、php、mysql、redis容器的构建了

注意:

宿主机映射容器的文件夹、配置文件等操作就自行看第一张截图里面的单独安装容器的文章教程

我这里就不说了,上面是成品环境了(底部有我的git链接),直接访问我的一个laravel网站项目。

最后一步,在navicat导入我laravel网站项目的数据库,就可以访问网站了。

看看效果

2.png

源码在这里有兴趣的可以自行拉取 -->自用dnmp环境git地址

相关文章