ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

docker服务编排--docker-compose

2020-05-08 20:56:14  阅读:240  来源: 互联网

标签:compose demo server nginx 编排 html docker


根据哔哩哔哩视频【docker入门2】实战~如何组织一个多容器项目docker-compose进行学习

利用网站https://labs.play-with-docker.com/进行测试

第一步,安装nginx镜像

# 拉取nginx镜像
docker pull nginx
# 运行nginx容器
docker run -d -p80:80 --name mynginx nginx 
#容器ID
87702c4c192061036d731df8f533a82f20b972284b650a850adec3aba9580342
# 查看运行中的容器
docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
87702c4c1920        nginx               "nginx -g 'daemon of…"   24 seconds ago      Up 23 seconds       0.0.0.0:80->80/tcp   mynginx 
# 在容器中进行交互操作
docker exec -it mynginx /bin/bash
# 查看容器IP地址
cat /etc/hosts

127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	87702c4c1920

第二步:安装最小化Linux镜像alpine

# 拉取alpine镜像
docker pull alpine
# 安装curl
apk add curl
# curl nginx容器
curl 172.17.0.2

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

另外一种方式 --link方式

#后台运行alpine,并将mynginx容器绑定域名myng
docker run -dit --link mynginx:myng alpine   
825d4b3d221238bda2f29411fcae6d72fafb97d5002986660aa8c3b7cc501d99
# 安装curl
apk add curl

fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/4) Installing ca-certificates (20191127-r1)
(2/4) Installing nghttp2-libs (1.40.0-r0)
(3/4) Installing libcurl (7.67.0-r0)
(4/4) Installing curl (7.67.0-r0)
Executing busybox-1.31.1-r9.trigger
Executing ca-certificates-20191127-r1.trigger
OK: 7 MiB in 18 packages
# curl nginx镜像
curl myng

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

# 原理,增加了172.17.0.2      myng d2d78de2ad2f
cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      myng d2d78de2ad2f
172.17.0.3      9688a42fec94

docker-compose方式

/Users/doubledumbao/docker-demo/compose-demo目录下,新建html目录,添加index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello world nginx</title>
</head>
<body>
    <h1>hello world nginx!!!</h1>
</body>
</html>

/Users/doubledumbao/docker-demo/compose-demo/html目录下,添加test.php

<!DOCTYPE html>
<html>
<body>

<?php
echo "Hello World PHP!!!";
?>

</body>
</html>

/Users/doubledumbao/docker-demo/compose-demo/html目录下,添加mysql.php

<?php
$servername = "mysql";
$username = "root";
$password = "123456";
 
// 创建连接
$conn = new mysqli($servername, $username, $password);
 
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "connected success !!!!!";
?>

/Users/doubledumbao/docker-demo/compose-demo目录下,新建conf目录

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        # }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
           fastcgi_pass   php:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
           include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

/Users/doubledumbao/docker-demo/compose-demo目录下,添加docker-compose.yml

version: '3'
services: 
    nginx:
        image: nginx:alpine
        ports: 
        - 80:80
        volumes: 
        - /Users/doubledumbao/docker-demo/compose-demo/html:/usr/share/nginx/html
        - /Users/doubledumbao/docker-demo/compose-demo/conf/nginx.conf:/etc/nginx/nginx.conf
    php:
        image: devilbox/php-fpm:5.2-work-0.89
        volumes: 
        - /Users/doubledumbao/docker-demo/compose-demo/html:/var/www/html
    mysql:
        image: mysql:5.6
        environment: 
        - MYSQL_ROOT_PASSWORD=123456

启动docker-compose

docker-compose up

打开浏览器,查看效果

# 查看nginx配置效果
http://localhost
# 查看php配置效果
http://localhost/test.php
# 查看mysql配置效果
http://localhost/mysql.php

标签:compose,demo,server,nginx,编排,html,docker
来源: https://www.cnblogs.com/zhaoran8775/p/12852915.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有