美文网首页DockerDocker容器
使用docker-compose构建nginx容器组 >_

使用docker-compose构建nginx容器组 >_

作者: 我是布谷鸟 | 来源:发表于2019-05-12 20:14 被阅读5次

    实验环境centos7、docker私有仓库
    主机地址192.168.200.104(又是负载均衡)

    首先编写docker-compose.yml文件

    [root@server ~]# mkdir docker-compose
    [root@server ~]# vi docker-compose/docker-compose.yml 
    version: "2"(docker-compose版本)
    services: 
        nginx: (容器名字)
          image: nginx:latest (镜像名字)
          ports: 
           - "8181:80"
        centos_http:
          build: .
          ports:
           - "9191:80"
          tty: true(打开虚拟终端,防止容器退出)
        tomcat:
          image: tomcat:latest
          ports:
           - "9292:8080"
    

    要构建centos_http镜像,所以编写dockerfile:

    [root@server ~]# cat docker-compose/dockerfile 
    FROM 192.168.200.104:5000/centos:latest
    MAINTAINER xiandian
    RUN rm -rf /etc/yum.repos.d/*
    ADD docker.repo /etc/yum.repos.d/
    RUN yum -y install httpd
    EXPOSE 80
    别忘了yum源:
    [root@server ~]# cat docker-compose/docker.repo 
    [centos]
    name=centos
    baseurl=ftp://192.168.100.10/centos
    gpgcheck=0
    enabled=1
    

    构建镜像:
    (如果容器组里有构建镜像的容器,要先建立镜像)

    [root@server docker-compose]# docker-compose build
    nginx uses an image, skipping
    Building centos_http
    Step 1 : FROM 192.168.200.104:5000/centos:latest
     ---> ff426288ea90
    Step 2 : MAINTAINER xiandian
     ---> Running in fdc344559cc1
     ---> 776f32702ee7
    Removing intermediate container fdc344559cc1
    Step 3 : RUN rm -rf /etc/yum.repos.d/*
     ---> Running in 8ada479b623c
     ---> 654ff7146b04
    ......
    Step 6 : EXPOSE 80
     ---> Running in 9995addd821b
     ---> d167d0ceb1c9
    Removing intermediate container 9995addd821b
    Successfully built d167d0ceb1c9
    tomcat uses an image, skipping
    

    运行容器组:

    [root@server docker-compose]# docker-compose up
    Creating dockercompose_nginx_1
    Creating dockercompose_centos_http_1
    Creating dockercompose_tomcat_1
    Attaching to dockercompose_nginx_1, dockercompose_centos_http_1, dockercompose_tomcat_1...
    

    验证时我打开了另一个终端(因为docker-compose up霸占了当前终端):

    [root@server docker-compose]# docker-compose ps
          Name               Command              State               Ports       
    -----------------------------------------------------------------------------
    dockercompose_cen   /bin/bash           Up                  0.0.0.0:9191->80/ 
    tos_http_1                                                  tcp               
    dockercompose_ngi   nginx -g daemon     Up                  0.0.0.0:8181->80/ 
    nx_1                off;                                    tcp               
    dockercompose_tom   catalina.sh run     Up                  0.0.0.0:9292->808 
    cat_1                                                       0/tcp             
    

    接下来要配置nginx容器的配置文件和启动http容器tomcat容器内的web服务,完成后就ok啦。

    相关文章

      网友评论

        本文标题:使用docker-compose构建nginx容器组 >_

        本文链接:https://www.haomeiwen.com/subject/ialhaqtx.html