第141天学习打卡(Docker Docker Compose )
Posted doudoutj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第141天学习打卡(Docker Docker Compose )相关的知识,希望对你有一定的参考价值。
1.卸载docker
systemctl stop docker
yum -y remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
#1.卸载旧的版本
yum remove docker \\
docker-client \\
docker-client-latest \\
docker-common \\
docker-latest \\
docker-latest-logrotate \\
docker-logrotate \\
docker-engine
# 2.需要的安装包
yum install -y yum-utils
#3. 设置镜像仓库
yum-config-manager \\
--add-repo \\
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#4. 更新yum软件包索引
yum makecache fast
#5.安装docker CE
yum install docker-ce docker-ce-cli containerd.io
# 6.启动docker
systemctl start docker
#7.测试命令
docker version
docker run hello-world
docker images
#8.配置镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://g6yrjrwf.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
#9.再次重启docker
sudo systemctl restart docker
Docker Compose
简介
DockerFile build run 手动操作 ,单个容器!
Docker Compose 轻松高效的管理容器。定义运行多个容器。
官方介绍
官网地址:Overview of Docker Compose | Docker Documentation
定义、运行多个容器
YAML file配置文件
single command。命令有哪些?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
三步骤:
Using Compose is basically a three-step process:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere.- Dockerfile保证我们的项目在任何地方都可以运行
- Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment.- service 什么是服务
- docker-compose.yml 这个文件怎么写
- Run
docker compose up
and the Docker compose command starts and runs your entire app. You can alternatively rundocker-compose up
using the docker-compose binary.- 启动项目
作用:批量容器编排。
Compose是Docker官方的开源项目。需要安装
Dockerfile
让程序在任何地方运行。
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
Compose:重要的概念
- 服务service, 容器,应用。
- 项目project。一组关联的容器。
安装Compose
官网地址:Install Docker Compose | Docker Documentation
1.下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 这个快一点
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
[root@kuangshen home]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/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 379 100 379 0 0 695 0 --:--:-- --:--:-- --:--:-- 696
100 633 100 633 0 0 608 0 0:00:01 0:00:01 --:--:-- 2670
100 12.1M 100 12.1M 0 0 285k 0 0:00:43 0:00:43 --:--:-- 337k
[root@kuangshen home]# cd /usr/local/bin
[root@kuangshen bin]# ll
total 32500
-rw-r--r-- 1 root root 14899 May 16 15:16 6379.log
-rw-r--r-- 1 root root 192844 May 16 15:09 6380.log
-rw-r--r-- 1 root root 191883 May 16 15:16 6381.log
-rw-r--r-- 1 root root 139 May 14 21:01 appendonly.aof
-rwxr-xr-x 1 root root 388 Apr 26 2020 chardetect
-rwxr-xr-x 1 root root 396 Apr 26 2020 cloud-id
-rwxr-xr-x 1 root root 400 Apr 26 2020 cloud-init
-rwxr-xr-x 1 root root 2108 Apr 26 2020 cloud-init-per
-rwxr-xr-x 1 root root 12737304 May 29 19:30 docker-compose
#授权
[root@kuangshen bin]# sudo chmod +x docker-compose
[root@kuangshen bin]# docker-compose version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
体验
官网地址:Get started with Docker Compose | Docker Documentation
1.应用 app.py
2.Dockerfile 应用打包为镜像
3.Docker-compose yaml 文件(定义整个服务,需要的环境。web redis)完整的上线服务!
4.启动compose 项目(docker-compose up)
流程:
1.创建网络
2.执行Docker-compose yaml
3.启动服务
Creating composetest_redis_1 … done
Creating composetest_web_1 … done
[root@kuangshen home]# mkdir composetest
[root@kuangshen home]# cd composetest
[root@kuangshen composetest]# ll
total 0
[root@kuangshen composetest]# vim app.py
import time
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)
[root@kuangshen composetest]# vim requirements.txt
flask
redis
[root@kuangshen composetest]# vim Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
[root@kuangshen composetest]# vim docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
[root@kuangshen composetest]# docker-compose up
运行docker-compose build之后再次执行docker-compose up
服务启动成功 服务正常!
[root@kuangshen /]# 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.
默认的服务名 文件名_服务名 _num
多个服务器, 集群。 如果有A B服务器 _num 代表的是副本数量
集群状态,服务都不可能只有一个运行实例,真实的项目是弹性的。
网络规则:
运行10个服务,最终打包成为一个项目(项目中的内容都在同一个网络下。可以域名访问),
[root@kuangshen /]# docker network inspect composetest_default
如果 同一个网络下,我们可以直接通过域名访问。
以前都是单个docker run启动容器
docker-compose 通过docker-compose编写yaml配置文件,可以通过compose一键启动所有的服务或者停止。
docker-compose stop
docker-compose down --volumes
Docker小结
1.Docker 镜像, run=>容器
2.DockerFile 构建镜像 (服务打包)
3.docker-compose启动项目(编排、多个微服务/环境)
4.Docker网络!
以上是关于第141天学习打卡(Docker Docker Compose )的主要内容,如果未能解决你的问题,请参考以下文章
第139天学习打卡(Docker 发布自己的镜像 Docker网络)
第142天学习打卡(Docker yaml规则 开源项目 Docker Swarm)
第133天学习打卡(Docker 初识Docker Docker的基本组成 安装docker 阿里云镜像加速 Docker常用命令)