好物优选点击查看详情 京东购买

暂无内容

docker-compose概述与简单编排部署

Docker-Compose将所管理的容器分为三层,分别是 工程(project),服务(service)以及容器(container)。

Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。

使用一个Dockerfile模板文件,可以让用户很方便的定义一个单独的应用容器。在工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个Web项目,除了Web服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。

Compose允许用户通过一个单独的docker-compose.yml模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。

Docker-Compose项目由Python编写,调用Docker服务提供的API来对容器进行管理。因此,只要所操作的平台支持Docker API, 就可以在其上利用Compose来进行编排管理。

1、YAML 文件格式

2、YAML格式的注意事项

5.1准备依赖文件

[root@localhost ~]# mkdir -p /opt/compose_nginx/nginx [root@localhost ~]# cd /opt/compose_nginx/nginx/ [root@localhost nginx]# rz [root@localhost nginx]# ls nginx-1.12.2.tar.gz vim Dockerfile cat Dockerfile FROM centos:7 RUN yum -y update RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make RUN useradd -M -s /sbin/nologin nginx ADD nginx-1.12.2.tar.gz /usr/local/src WORKDIR /usr/local/src/nginx-1.12.2 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module RUN make -j 4 && make install RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #ENV PATH /usr/local/nginx/sbin/:PATH EXPOSE 80 EXPOSE 443 RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf CMD ["/usr/local/nginx/sbin/nginx"]

5.2 编写配置文件 docker-compose.yml

[root@localhost ~]# mkdir -p /opt/compose_nginx/nginx [root@localhost nginx]# cd .. [root@localhost compose_nginx]# vim docker-compose.yml [root@localhost compose_nginx]# cat docker-compose.yml version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: - lic volumes: - ./wwwroot:/usr/local/nginx/html networks: lic:
  • 参数说明:

 version: '3' #compose编排版本为3 services: #服务: nginx: #服务名称 hostname: nginx #容器主机名 build: #创建容器过程: context: ./nginx #所需素材都在该目录(仓库)下 dockerfile: Dockerfile #指定Dockerfile文件 ports: #提供端口: - 1216:80 #对外提供(暴露)的端口 - 1217:443 networks: #指定网络 - lic #网络名称(群集名称需一致) volumes: #数据卷 - ./wwwroot:/usr/local/nginx/html #将宿主机该仓库内文件与nginx首页文件相关联(容器) networks: #对外公开需要声明networks群集名称 lic:

5.3 生成镜像容器

[root@localhost nginx]# docker-compose -f docker-compose.yml up -d ... ... Successfully built f87b84e57bd9 Successfully tagged compose_nginx_nginx:latest

5.4 访问测试

[root@localhost compose_nginx]# ls docker-compose.yml nginx wwwroot echo "hello world" > index.html curl 192.168.223.30:1216

5.5 查看结构

[root@localhost compose_nginx]# yum -y install tree[root@localhost compose_nginx]# tree ./ ./ ├── docker-compose.yml ├── nginx │ ├── Dockerfile │ └── nginx-1.12.2.tar.gz └── wwwroot └── index.html 2 directories, 4 files
cd /opt mkdir compose-tomcat cd compose-tomcat mkdir tomcat cd tomcat ==拖入安装包apache-tomcat-9.0.16.tar.gz 、jdk-8u91-linux-x64.tar.gz== vim Dockerfile FROM centos:7 ADD apache-tomcat-9.0.16.tar.gz /usr/local ADD jdk-8u91-linux-x64.tar.gz /usr/local WORKDIR /usr/local RUN mv apache-tomcat-9.0.16 tomcat RUN mv jdk1.8.0_91 java ENV JAVA_HOME /usr/local/java ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar ENV PATH $JAVA_HOME/bin:$PATH EXPOSE 8080 CMD ["/usr/local/tomcat/bin/catalina.sh","run"] ====>:wq cd .. vim tomcat-compose.yml version: '3' services: tomcat: hostname: tomcat build : context: ./tomcat dockerfile: Dockerfile ports: - 7788:8080 networks: - cluster networks: cluster: ====>:wq docker-compose -f tomcat-compose.yml up -d ==真机访问http://IP:7788==

7.1 编写 Dockerfile 文件

  • 存放目录为:

 [root@localhost ~]#mkdir -p /opt/compose_lnmp/nginx/ [root@localhost ~]#mkdir -p /opt/compose_lnmp/mysql/ [root@localhost ~]#mkdir -p /opt/compose_lnmp/php/
  • 文件配置:

 [root@localhost ~]#ls /opt/compose_lnmp/nginx/ Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz [root@localhost ~]#ls /opt/compose_lnmp/mysql/ Dockerfile my.cnf mysql-boost-5.7.20.tar.gz [root@localhost ~]#ls /opt/compose_lnmp/php/ Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf

7.2编写配置文件 docker-compose.yml

[root@localhost ~]#cat docker-compose.yml version: '2' services: nginx: hostname: nginx build: context: /opt/compose_lnmp/nginx/ dockerfile: Dockerfile ports: - 1111:80 - 2222:443 container_name: nginx_server networks: lnmp: ipv4_address: 172.111.0.10 mysql: hostname: mysql build: context: /opt/compose_lnmp/mysql/ dockerfile: Dockerfile ports: - 3306:3306 container_name: mysql_server networks: lnmp: ipv4_address: 172.111.0.20 php: hostname: php build: context: /opt/compose_lnmp/php/ dockerfile: Dockerfile ports: - 9000:9000 container_name: php_server networks: lnmp: ipv4_address: 172.111.0.30 networks: lnmp: driver: bridge ipam: config: - subnet: 172.111.0.0/16

7.3生成镜像容器

[root@localhost /opt/compose_lnmp]#docker-compose -f docker-compose.yml up -d ...... [root@localhost /opt/compose_lnmp]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE compose_lnmp_php latest 86b0447ebabd About a minute ago 1.22GB compose_lnmp_mysql latest 59579ca01443 5 minutes ago 9.98GB compose_lnmp_nginx latest 82d760d5d811 17 minutes ago 528MB centos 7 eeb6ee3f44bd 2 months ago 204MB [root@localhost /opt/compose_lnmp]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 53c87813597f compose_lnmp_php "/bin/sh -c '/usr/lo…" About a minute ago Up About a minute 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php_server 2d671252967b compose_lnmp_nginx "/usr/local/nginx/sb…" About a minute ago Up About a minute 0.0.0.0:1111->80/tcp, :::1111->80/tcp, 0.0.0.0:2222->443/tcp, :::2222->443/tcp nginx_server 9d5428cfdf54 compose_lnmp_mysql "/usr/sbin/init" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql_server 
[root@localhost /opt/compose_lnmp]#docker exec -it mysql_server bash [root@mysql bin]# mysql -u root -p Enter password: #初始密码为空 mysql> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)

原文链接:https://blog.csdn.net/weixin_59105297/article/details/128841070?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316782425126562%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316782425126562&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-27-128841070-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享