Docker 进阶篇(三)Docker 容器编排 Docker

Docker 进阶篇(三)Docker 容器编排 Docker-compose

Docker-compose 说明

Compose 简介

Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。

YAML文件基础解读

示例:

version: '3'
services:
  mysql:
    image: mysql:5.7.22
    container_name: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
    restart: always
    privileged: true
    ports:
      - "3306:3306"
    networks:
      - "net"
    volumes:
      - "/mysl_data/:/var/lib/mysql"
  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - "80:80"
    networks:
      - "net"
    volumes:
      - "/nginx_data/:/usr/share/nginx/html/"
networks:
  net:
    driver: bridge

version:指定 docker-compose.yml 文件的写法格式的版本,写在yaml文件第一行
services:多个容器的集合,所有容器的定义都要写到services下

version: '3'
services:
  container 1 的名称:
   	容器定义内容......
  container 2 的名称:
    容器定义内容......

image: 所使用的镜像

version: '3'
services:
  mysql:
    image: mysql:5.7.22

container_name:容器名称

version: '3'
services:
  mysql:
    image: mysql:5.7.22
    container_name: mysql

environment:容器环境变量

    environment:
      - 变量名称1=值
      - 变量名称2=值

restart: always,与docker run 后面的 restart: always 一样,使容器随Docker启动而启动

privileged: true 开启特权模式,使用该参数,container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。

ports 端口映射

    ports:
      - "宿主机端口:容器端口"

networks: 定义容器网络的名称,可在与services 平级的networks中来定义容器network的类型

    networks:
      - "net"
networks:
  net:
    driver: bridge

volumes: 数据卷挂载

    volumes:
      - "宿主机目录:容器目录"
Docker-compose 安装 及 使用 安装Docker-compose
[root@node_1 ~]#  curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@node_1 ~]#  chmod +x /usr/local/bin/docker-compose
运行测试

docker-compose以下都需要在docker-compose.yml所在目录下执行,如果编写的yaml文件不叫docker-compose.yml,那么在执行的时候需要加-f参数指定yaml文件

[root@node_1 ~]# mkdir compose
[root@node_1 ~]# cd compose
[root@node_1 compose]# mkdir /mysl_data/
[root@node_1 compose]# mkdir  /nginx_data
[root@node_1 compose]# vi docker-compose.yml
version: '3'
services:
  mysql:
    image: mysql:5.7.22
    container_name: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
    restart: always
    privileged: true
    ports:
      - "3306:3306"
    networks:
      - "net"
    volumes:
      - "/mysl_data/:/var/lib/mysql"
  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - "80:80"
    networks:
      - "net"
    volumes:
      - "/nginx_data/:/usr/share/nginx/html/"
networks:
  net:
    driver: bridge

[root@node_1 compose]# docker-compose up -d
Creating network "compose_net" with driver "bridge"
Pulling mysql (mysql:5.7.22)...
5.7.22: Pulling from library/mysql
be8881be8156: Extracting [===============================================   ]                                                                                                                                                              21.56MB/22.49MBownload complete
be8881be8156: Extracting [================================================  ]                                                                                                                                                              21.79MB/22.49MBownload complete
be8881be8156: Extracting [================================================= ]                                                                                                                                                              22.25MB/22.49MBownload complete
be8881be8156: Pull complete
c3995dabd1d7: Pull complete
9931fdda3586: Pull complete
bb1b6b6eff6a: Pull complete
a65f125fa718: Pull complete
2d9f8dd09be2: Pull complete
37b912cb2afe: Pull complete
79592d21cb7f: Pull complete
00bfe968d82d: Pull complete
79cf546d4770: Pull complete
2b3c2e6bacee: Pull complete
Creating mysql ... done
Creating nginx ... done
[root@node_1 compose]# docker-compose  ps
Name               Command               State           Ports
-----------------------------------------------------------------------
mysql   docker-entrypoint.sh mysqld      Up      0.0.0.0:3306-3306/tcp
nginx   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:80-80/tcp

[root@node_1 compose]# echo  "It's OK"  /nginx_data/index.html

[root@node_1 compose]# curl 127.0.0.1:80
It's OK

[root@node_1 compose]# mysql -uroot -proot -h 127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql

常用基础命令

docker-compose ps ##查看当前目录下yaml文件定义的容器状态
docker-compose stop ##停止当前目录下yaml文件定义的容器
docker-compose start ##启动当前目录下yaml文件定义的容器
docker-compose rm ##删除当前目录下yaml文件定义的容器

最新回复(0)
/jishu3acDZ5P_2B0Gcrk3QxaRj6EfZWxPKSze92kEGhWX_2FY2tw_3D4858797
8 简首页