其他分享
首页 > 其他分享> > Docker-07-docker compose

Docker-07-docker compose

作者:互联网

一、docker运行wordpress案例

[root@docker01 ~]# docker run -d --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wordpress mysql
05e9c5733c1784dc86582e633796e154118bbbefeadb4394bfff82d8a06054c4

官网地址:https://hub.docker.com/_/wordpress/

[root@docker01 ~]# docker run -d -e WORDPRESS_DB_HOST=mysql:3306 --link mysql -p 8080:80 wordpress
97443a6db7650fe3ebd2c016008455238f95b692597012e05532d58363e32ef3

二、什么是Docker Compose

2.1 docker-compose版本

官网介绍:https://docs.docker.com/compose/compose-file/

version2只支持单机

version3可以支持多机(建议使用)

2.2 docker-compose.yml文件

docker-compose的三大概念:

2.2.1 Services

例(从docker hub拉取镜像):

services:
  db:
    image: postgres:9.4
    volumes:
      - "db-data:/var/lib/postgresql/data"
    networks:
      - back-tier

等价于docker run命令:

docker run -d --network back-tier -v db-data:/var/lib/postgresql/data postgres:9.4

例(本地构建dockerfile):

services:
worker:
build: ./worker
links:
- db
- redis
networks:
- back-tier

2.2.2 Volumes

创建一个volume,或者将volume挂载到容器:

volumes:
  mysql-data:

等价于:

docker volume create mysql-data

2.2.3 Networks

创建一个网络,或者为容器指定一个网络

networks:
  my-bridge:
    driver: bridge

等价于:

docker network create my-bridge

三、docker-compose的安装和使用

3.1安装docker-compose

MAC和windows在安装docker时已经安装好docker-compose了。

在linux上安装docker-compose

官网地址:https://docs.docker.com/compose/install/

3.1.1 下载可执行文件安装

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
chmox +X /usr/local/bin/docker-compose
docker-compose --version

3.1.2 通过pip安装

yum -y install epel-release
yum -y install python-pip
pip install docker-compose
docker-compose --version

3.2 docker-compose的使用

3.2.1 docker-compose命令参数

  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

3.2.2 使用docker-compose

案例:通过docker-compose安装wordpress

[root@docker02 wordpress]# cat docker-compose.yml 
version: '3'

services:
  wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_PASSWORD: root
    networks:
      - my-bridge

  mysql:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - my-bridge

volumes:
  mysql-data:

networks:
  my-bridge:
    driver: bridge
docker-compose up
[root@docker02 wordpress]# docker-compose up -d
Starting wordpress_wordpress_1 ... done
Starting wordpress_mysql_1     ... done
[root@docker02 wordpress]# docker-compose ps
        Name                       Command               State          Ports        
-------------------------------------------------------------------------------------
wordpress_mysql_1       docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp 
wordpress_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp
docker-compose down

注意:后面跟的是在docker-compose.yml中定义的service名

[root@docker02 wordpress]# docker-compose exec mysql bash

 四、水平拓展负载均衡实验

docker-compose命令中有一个参数:scale,可以通过这个参数来调节容器的运行数量!

准备flask-redis案例(参考文章:https://www.cnblogs.com/liuguangjiji/p/10629410.html

4.1 启动多个web和一个redis

(1)python程序文件app.py

from flask import Flask
from redis import Redis
import os
import socket

app = Flask(__name__)
redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)


@app.route('/')
def hello():
    redis.incr('hits')
    return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname())


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

(2)Dockerfile

FROM python:2.7
LABEL maintainer="this is test message"
COPY . /app/
WORKDIR /app
RUN pip install flask redis
EXPOSE 5000
CMD ["python","app.py"]

(3)docker-compose.yml

version: '3'

services:
  redis:
    image: redis

  web:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      REDIS_HOST: redis
docker-compose up -d
[root@docker01 chapter6]# docker-compose ps
      Name                    Command               State    Ports  
--------------------------------------------------------------------
chapter6_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
chapter6_web_1     python app.py                    Up      5000/tcp
docker-compose up --scale web=3 -d
[root@docker01 chapter6]# docker-compose ps
      Name                    Command               State    Ports  
--------------------------------------------------------------------
chapter6_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
chapter6_web_1     python app.py                    Up      5000/tcp
chapter6_web_2     python app.py                    Up      5000/tcp
chapter6_web_3     python app.py                    Up      5000/tcp

4.2 在前方增加HAProxy

(1)app.py

from flask import Flask
from redis import Redis
import os
import socket

app = Flask(__name__)
redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)


@app.route('/')
def hello():
    redis.incr('hits')
    return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname())


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80, debug=True)

(2)Dockerfile

FROM python:2.7
LABEL maintaner="this is test message"
COPY . /app
WORKDIR /app
RUN pip install flask redis
EXPOSE 80
CMD [ "python", "app.py" ]

(3)docker-compose.yml

version: "3"

services:

  redis:
    image: redis

  web:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      REDIS_HOST: redis

  lb:
    image: dockercloud/haproxy
    links:
      - web
    ports:
      - 8080:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock 
docker-compose up -d
#查看docker-compose启动的容器,共有三个
[root@docker01 chapter6]# docker-compose ps
      Name                    Command               State                    Ports                 
---------------------------------------------------------------------------------------------------
chapter6_lb_1      /sbin/tini -- dockercloud- ...   Up      1936/tcp, 443/tcp, 0.0.0.0:8080->80/tcp
chapter6_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp                               
chapter6_web_1     python app.py                    Up      5000/tcp   


#访问自身8080端口
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 1 times and my hostname is 2e7fe9be9d40.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 2 times and my hostname is 2e7fe9be9d40.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 3 times and my hostname is 2e7fe9be9d40.
[root@docker01 chapter6]# docker-compose up --scale web=3 -d
chapter6_redis_1 is up-to-date
Starting chapter6_web_1 ... done
Creating chapter6_web_2 ... done
Creating chapter6_web_3 ... done
chapter6_lb_1 is up-to-date
[root@docker01 chapter6]# docker-compose ps
      Name                    Command               State                    Ports                 
---------------------------------------------------------------------------------------------------
chapter6_lb_1      /sbin/tini -- dockercloud- ...   Up      1936/tcp, 443/tcp, 0.0.0.0:8080->80/tcp
chapter6_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp                               
chapter6_web_1     python app.py                    Up      5000/tcp                               
chapter6_web_2     python app.py                    Up      5000/tcp                               
chapter6_web_3     python app.py                    Up      5000/tcp      
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 4 times and my hostname is 2e7fe9be9d40.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 5 times and my hostname is b087d3960be1.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 6 times and my hostname is 986d2584749b.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 7 times and my hostname is 2e7fe9be9d40.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 8 times and my hostname is b087d3960be1.
[root@docker01 chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 9 times and my hostname is 986d2584749b.

已实现扩容和负载均衡,实验过程到此结束~~


 

标签:compose,app,chapter6,redis,docker,root,Docker
来源: https://www.cnblogs.com/liuguangjiji/p/10631634.html