docker / compose 的安装 和 体验
文档
视频
简介
课程内容
1.Docker Compose 容器编排
2.Docker Swarm #集群 热扩容 需要在阿里上买服务器,至少冲100+以上的人民币
文档:
集群方式的部署,买4台阿里云服务器,建议买2核4g 用的时候付费,不用的时候销毁.一天也就几元钱
3.Docker Stack #部署 和 Compose比较像
4.Docker Secret #权限认证
5.Docker Config #配置
6.k8s
容器单独没有什么意义,容器编排才有意义.
docker-compose使用的三个步骤
docker-compose.yml示例:
version: '2.0'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
通过编写docker-compose.yml
文件,哪怕有100个服务,
也可以用docker-compose up
一键启动
Compose : 重要的概念
- 服务sevices,容器.应用 (web,redis,mysql....),很多单容器
- 把很多单容器编排在一起,就是一个项目project.一组关联的容器.博客,web,mysql
安装docker-compose
# 1. 国内下载地址:(推荐)
执行:curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# 1. 官网的地址地址(不推荐,下载慢)
[root@localhost ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 423 100 423 0 0 313 0 0:00:01 0:00:01 --:--:-- 313
100 16.7M 100 16.7M 0 0 6907k 0 0:00:02 0:00:02 --:--:-- 19.3M
# 2.赋权:
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
# 3.看版本: 就是一个二进制文件.用它去运行命令
[root@localhost ~]# docker-compose version
Docker Compose version v2.17.2
[root@localhost /usr/local/bin]# docker -v
Docker version 23.0.4, build f480fb1
# 4.卸载:
[root@localhost bin]# rm /usr/local/bin/docker-compose
rm:是否删除普通文件 "/usr/local/bin/docker-compose"?y
[root@localhost bin]# docker-compose version
-bash: /usr/local/bin/docker-compose: 没有那个文件或目录
更多docker-compose版本见下面链接
https://github.com/docker/compose/releases
体验部署python应用
用docker-compose布置一个python应用
python应用.计数器.reids!
- 应用app.py
- Dockerfile 应用打包为镜像
- Docker-compose yaml文件(定义整个服务需要的环境变量web redis),完整的线上服务.
- 启动compose项目(docker-compase up)
实操
1.Create a directory for the project:
$ cd /home
$ mkdir composertest
$ cd composertest
2.Create a file called app.py in your project directory and paste this in:
$ vim app.py
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(host ="0.0.0.0",debug=True)
3.Create another file called requirements.txt in your project directory and paste this in:
vim requirements.txt
flask
redis
4.Create a Dockerfile
$ vim Dockerfile
FROM python:3.7-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt #安装txt里的python依赖项(flask redis)
CMD ["python", "app.py"] #将容器的默认命令设置为python app.py
5.在项目中创建docker-compose.yml文件
version: '3.8'
services:
web:
build: . #使用Dockerfile当前目录中构建镜像
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine" #从docker hub中下载运行
[root@localhost compasetest]# tree
.
├── app.py
├── docker-compose.yml
├── Dockerfile
└── requirements.txt
6.启动
$ docker-compose up` #窗口运行
$ docker-compose up -d #后台运行
启动效果:
[root@localhost composertest]# docker-compose up
Creating network "composertest_default" with the default driver
Creating composertest_web_1 ... done
Creating composertest_redis_1 ... done
Attaching to composertest_web_1, composertest_redis_1
redis_1 | 1:C 20 Aug 2020 23:34:26.784 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 20 Aug 2020 23:34:26.784 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 20 Aug 2020 23:34:26.784 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 20 Aug 2020 23:34:26.792 * Running mode=standalone, port=6379.
redis_1 | 1:M 20 Aug 2020 23:34:26.792 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 20 Aug 2020 23:34:26.792 # Server initialized
redis_1 | 1:M 20 Aug 2020 23:34:26.793 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 20 Aug 2020 23:34:26.793 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 20 Aug 2020 23:34:26.797 * Ready to accept connections
web_1 | * Serving Flask app "app" (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: on
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger PIN: 318-153-711
7.看新的窗口,查看启动的容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1012c89675b redis:alpine "docker-entrypoint.s…" 43 seconds ago Up 38 seconds 6379/tcp composertest_redis_1
5111b3adadaf composertest_web "python app.py" 43 seconds ago Up 38 seconds 0.0.0.0:5000->5000/tcp composertest_web_1
8.访问一下
[root@localhost composertest]# curl localhost:5000
Hello World! I have been seen 1 times.
[root@localhost composertest]# curl localhost:5000
Hello World! I have been seen 5 times.
[root@localhost composertest]# curl localhost:5000
Hello World! I have been seen 6 times.
[root@localhost composertest]# curl localhost:5000
Hello World! I have been seen 7 times.
[root@localhost ~]# docker service ls #它不是一个集群,所以不能这样查看
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
浏览器访问:
Hello World! I have been seen 4 times.
9.停止服务
$ docker-compose down
或者
ctrl+c
小结:
网络规则
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
55246ac3d411 bridge bridge local
8bf32609e619 composertest_default bridge local
868b78ed9d45 host host local
f81eb59f871d none null local
查看容器的网络
[root@localhost ~]# docker network -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
[root@localhost ~]# docker network inspect 8bf32609e619
[
{
"Name": "composertest_default",
"Id": "8bf32609e619553398f22ef75ae557eda7292647d17d4b864487559754428655",
"Created": "2020-08-21T07:34:22.293824382+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"5111b3adadafa06462ec653b0cf34750c483db1ff17d00c0b261f265b46b88c7": {
"Name": "composertest_web_1",
"EndpointID": "43857e6a28f7a6061dab21fa3e66a103158182b42ffebfbfd5d8083cd953b574",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
},
"e1012c89675bca1e7040af20176839427089e98a2aafe930ab1fa0eb4258ac65": {
"Name": "composertest_redis_1",
"EndpointID": "e3805e9343649e7ba830a705c26df97eb9f64c5407a7d43e87acd45fb4c581f1",
"MacAddress": "02:42:ac:14:00:03",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "composertest",
"com.docker.compose.version": "1.25.5"
}
}
]
可以看到在同一个网络下,可以通过服务名redis ping相互通网络
下面我们来测试一下:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1012c89675b redis:alpine "docker-entrypoint.s…" 39 minutes ago Up 38 minutes 6379/tcp composertest_redis_1
5111b3adadaf composertest_web "python app.py" 39 minutes ago Up 38 minutes 0.0.0.0:5000->5000/tcp composertest_web_1
[root@localhost ~]# docker exec -it 5111 /bin/sh
/code # ping redis # web里ping redis 可以ping通
PING redis (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.076 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.107 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.128 ms
64 bytes from 172.20.0.3: seq=3 ttl=64 time=0.115 ms
^C
--- redis ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.076/0.106/0.128 ms
/code # exit
[root@localhost ~]# docker exec -it e101 /bin/sh
/data # ping web # redis 里ping web 可以ping通
PING web (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.109 ms
64 bytes from 172.20.0.2: seq=1 ttl=64 time=0.111 ms
64 bytes from 172.20.0.2: seq=2 ttl=64 time=0.113 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.109/0.111/0.113 ms
/data #
docker / compose 的安装 和 体验的更多相关文章
- Docker Compose 介绍安装
Compose介绍 Compose是一个定义和管理多容器的工具,也是一种容器编排工具,前身是Pig,使用Python语言编写.使用Compose配置文件描述多个容器应用的架构,biubiu使用什么镜像 ...
- 26. docker compose 的安装 和 基本使用
1. 安装 docker compose https://docs.docker.com/compose/install/ 选择linux 即可 sudo curl -L "https:/ ...
- 手把手教你 Docker Compose的安装和使用
一.Docker Compose是什么? Docker Compose是一个工具,用于定义和运行多容器应用程序的工具: Docker Compose通过yml文件定义多容器的docker应用: Doc ...
- Docker Compose的安装及命令补全
安装Compose Compose的安装有多种方式,例如通过shell安装.通过pip安装.以及将compose作为容器安装等等.本文讲解通过shell安装的方式.其他安装方式如有兴趣,可以查看Doc ...
- centos7 Docker Compose 的安装
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose ...
- centos docker compose安装
docker compose离线安装 通过联网机器下载docker-compose离线安装包(参见Downloads部分) https://github.com/docker/compose/rele ...
- 【Docker Compose】简介与安装
1.简介 Compose 是一个用户定义和运行多个容器的 Docker 应用程序.在 Compose 中你可以使用 YAML 文件来配置你的应用服务.然后,只需要一个简单的命令,就可以创建并启动你配置 ...
- Docker Compose + Traefik v2 快速安装, 自动申请SSL证书 http转https 初次尝试
前言 昨晚闲得无聊睡不着觉,拿起服务器尝试部署了一下Docker + Traefik v2.1.6 ,以下是一些配置的总结,初次接触,大佬勿喷. 我的系统环境是 Ubuntu 18.04.3 LTS ...
- 手把手教你 Docker Compose安装DOClever
一.什么是Docker Compose以及Docker Compose的安装和使用 查看我的另外一篇博客:Docker Compose的安装和使用 二.DOClever是什么 DOClever是一个可 ...
- 5种常见的Docker Compose错误
在构建一个容器化应用程序时,开发人员需要一种方法来引导他们正在使用的容器去测试其代码.虽然有几种方法可以做到这一点,但 Docker Compose 是最流行的选择之一.它让你可以轻松指定开发期间要引 ...
随机推荐
- archlinux 安装后xfce没有声音,声音无法调节
参照 http://ivo-wang.github.io/2018/02/17/fix/ sudo pacman -S alsa-utils pavucontrol sudo pacman -S pi ...
- 如在 Java 中分割 Excel 工作表
前言 在Excel中创建的大多数商业报告不是单页的文档,而是包含了多个上下文相关的信息,这些信息被存储在多个工作表中.例如我们的一些地区销售报告.按部门分类的员工记录.每家店铺的库存清单等. 然而,随 ...
- Windows Terminal的资料
Windows Terminal是微软Windows平台难得好用的工具. 由于工作内容的原因,需要打开多个CMD窗口.多个git bash窗口,并且在多个窗口间切换,因此命令行窗口支持多TAB的特性, ...
- 直播预告丨OpenHarmony标准系统多媒体子系统之音频解读
今晚19点,OpenHarmony开源开发者成长计划知识赋能第五期"掌握OpenHarmony多媒体的框架原理"的第四节直播课,即将开播! 深开鸿资深技术专家苑春鸽老师,将在Ope ...
- DOM 节点遍历:掌握遍历 XML文档结构和内容的技巧
遍历是指通过或遍历节点树 遍历节点树 通常,您想要循环一个 XML 文档,例如:当您想要提取每个元素的值时. 这被称为"遍历节点树". 下面的示例循环遍历所有 <book&g ...
- C++ 多线程编程和同步机制:详解和实例演示
C++中的多线程编程和同步机制使得程序员可以利用计算机的多核心来提高程序的运行效率和性能.本文将介绍多线程编程和同步机制的基本概念和使用方法. 多线程编程基础 在C++中,使用<thread&g ...
- openGauss数据与PostgreSQL的差异对比
openGauss 数据与 PostgreSQL 的差异对比 前言 openGauss 数据库已经发布 2.0.1 版本了,中启乘数科技是一家专业的专注于极致性能的数据库服务提供商,所以也关注 ope ...
- github 小技巧
前言 简单记一下github 小技巧,因为经常忘. 正文 就是如何快速搜索到自己想找的项目. 如果自己知道项目名,那么直接输入就可以搜索到. 如果不是,那么一般要通过高级搜索. https://git ...
- 重新整理.net core 计1400篇[三] (.net core 如何源代码调试和查看源码 )
前言 本来这里是写源代码的,因为vs没有那么容易调试查看到源代码,所以先把调试源代码和查看源代码弄完. 正文 需要修改一些vs配置,这个vs也是有要求的,要vs2017. 那么你还需要加载远程符号. ...
- 论文记载:A Survey on Traffic Signal Control Methods
ABSTRACT 交通信号控制是一个重要且具有挑战性的现实问题,其目标是通过协调车辆在道路交叉口的移动来最小化车辆的行驶时间.目前使用的交通信号控制系统仍然严重依赖过于简单的信息和基于规则的方法,尽管 ...