Docker-07-docker compose

Posted liuguangjiji

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-07-docker compose相关的知识,希望对你有一定的参考价值。

一、docker运行wordpress案例

  • 第一步:创建一个mysql容器
[[email protected] ~]# docker run -d --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wordpress mysql
05e9c5733c1784dc86582e633796e154118bbbefeadb4394bfff82d8a06054c4
  • 第二步:启动wordpress容器,指定相应环境变量

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

[[email protected] ~]# docker run -d -e WORDPRESS_DB_HOST=mysql:3306 --link mysql -p 8080:80 wordpress
97443a6db7650fe3ebd2c016008455238f95b692597012e05532d58363e32ef3
  • 第三步:网页访问http://host_ip:8080

二、什么是Docker Compose

  • Docker Compose是一个基于docker的命令行工具;
  • 这个工具可以通过一个yml文件定义多容器的docker应用;
  • 通过一条命令就可以根据yml文件的定义去创建或管理多个容器;
  • docker-compose.yml是默认的yml配置文件名称。

2.1 docker-compose版本

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

version2只支持单机

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

2.2 docker-compose.yml文件

docker-compose的三大概念:

  • Services
  • Networks
  • Volumes

2.2.1 Services

  • 一个service代表一个container,这个container可以从dockerhub的image来创建,或者从本地的dockerfile build出来的image创建。
  • Service启动类似docker run,我们可以给其指定network和volume,所以可以给service指定network和volume的引用。

例(从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安装

  • 安装python-pip,并通过pip安装docker-compose
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

  • 编写docker-compose.yml
[[email protected] 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
docker-compose up
  • 后台启动docker-compose
[[email protected] wordpress]# docker-compose up -d
Starting wordpress_wordpress_1 ... done
Starting wordpress_mysql_1     ... done
  • 查看docker-compose生成的容器
[[email protected] 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
docker-compose down
  • 进入到docker-compose启动的容器

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

[[email protected] 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
docker-compose up -d
  • 第三步:查看docker-compose
[[email protected] 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
  • 第四步:调整web数量为3
docker-compose up --scale web=3 -d
  • 第五步:再次查看docker-compose
[[email protected] 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

技术图片

  • 第一步:修改app.py、Dockerfile以及docker-compose.yml文件

(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
docker-compose up -d
  • 第三步:查看docker-compose容器,并访问端口8080
#查看docker-compose启动的容器,共有三个
[[email protected] 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端口
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 1 times and my hostname is 2e7fe9be9d40.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 2 times and my hostname is 2e7fe9be9d40.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 3 times and my hostname is 2e7fe9be9d40.
  • 第四步:将web容器scale成3个节点
[[email protected] 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
[[email protected] 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      
  • 第五步:再次访问8080端口,发现已经可以负载到后端的其他容器了,实验成功!!
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 4 times and my hostname is 2e7fe9be9d40.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 5 times and my hostname is b087d3960be1.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 6 times and my hostname is 986d2584749b.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 7 times and my hostname is 2e7fe9be9d40.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 8 times and my hostname is b087d3960be1.
[[email protected] chapter6]# curl 127.0.0.1:8080
Hello Container World! I have been seen 9 times and my hostname is 986d2584749b.

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


 

以上是关于Docker-07-docker compose的主要内容,如果未能解决你的问题,请参考以下文章

Docker异常docker-compose up throw UnicodeDecodeError: 'ascii' codec can't decode byte 0x(代

homestead的laravel项目错误:Use of undefined constant JSON_INVALID_UTF8_SUBSTITUTE - assumed ‘JSON_INVAL(代

信息科技 | PHP Laravel工程师

Java分代垃圾回收机制:年轻代/年老代/持久代(转)

R留学生作业代码代写代编程代编程代编程

IPEX-1代/3代/4代/5代,PCB天线底座,公头,样式及封装尺寸图