安装

1
2
3
4
5
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
# docker-compose version 1.24.0, build 0aa59064

测试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# docker-compose.yaml
version: "3"
services:
  whoami:
    container_name: whoami
    hostname: whoami
    image: containous/whoami
    networks:
    - backend

  nginx:
    container_name: nginx
    hostname: nginx
    image: nginx:1.16.0-alpine
    ports:
    - "18080:80"
    networks:
    - backend
    volumes:
    - /shell/etc/nginx/conf.d:/etc/nginx/conf.d

networks:
  backend:
# 自动创建一个deploy_backend的bridge
1
vi /shell/etc/nginx/conf.d/demo.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
    listen 80;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 10;
    proxy_send_timeout 10;
    proxy_read_timeout 10;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_ignore_client_abort on;
    location /{
      proxy_pass http://backend;
    }
  }
  upstream backend{
    server   whoami:80  weight=1 max_fails=2 fail_timeout=10s;
    keepalive 300;
  }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
docker-compose --verbose -f docker-compose.yaml up
docker-compose --verbose -f docker-compose.yaml down
# docker network create backend
# docker network inspect backend
# ip a list type bridge
curl http://127.0.0.1:18080

Hostname: whoami
IP: 127.0.0.1
IP: 172.23.0.2
GET / HTTP/1.1
Host: 127.0.0.1
User-Agent: curl/7.29.0
Accept: */*
X-Forwarded-For: 172.23.0.1
X-Real-Ip: 172.23.0.1

参考

1
2
3
4
5
6
7
8
9
# 1.创建一个容器内部网络
docker network create --driver bridge my-net
# 2.启动php容器并且加入到该网络
docker run --name myphp -v /usr/local/www:/www --privileged=true -d --network my-net myphp:7.2-fpm
# 3.启动NGINX容器并且加入到该网络
docker run --name runoob-php-nginx -p 20080:80 -d -v /usr/local/www:/usrinxml:ro -v /usr/localinx/conf.d:/etcinx/conf.d:ro --network my-net nginx
# export KUBECONFIG=/etc/kubernetes/admin.conf
# echo export KUBECONFIG=/etc/kubernetes/kubelet.conf >> ~/.bashrc
# kubectl config view