简述
1. 什么是docker,用docker的好处:
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.
platform develop deploy run application
2. 为什么要使用docker:
containerization
flexible:很复杂的app都可以做成containerized
lightweight:共享利用主机内核
interchangeable
portable
scalable:increase and automat 儿 distribute container replicas
Stackable
比如说我在新的服务器部署app:只要docker run下 就能获得和其他服务器一样的app
3 docker的组成:
Images: An image is an executable package(包含code runtime libraries 环境变量 配置文件)
Containers
就是把images加载进内存获得process 或者 an image with state
docker ps:you can see a list of your running containers
4. Containers and virtual machines
containers:natively share the kernel it runs a discrete process no more memory
只需加载我需要的资源
虚拟机: runs a full-blown“guest” operating system 通过 hypervisor 与内核交互,虚拟机需要更多的**额外资源**, 有些applications是不需要的
docker常用指令:
docker --version
docker info
docker run hello-world :测试docker 安装是否ok
docker image ls:查看有哪些image
docker container ls (--all):查看container运行的
docker container --help:查看有哪些信息
docker build -t friendhello . //build 成 image 并取个名字 在dockerfile所在文件路径
docker run -p 4000:80 friendhello
docker-machine ip
docker service ls
docker service ps 【service name]
dockee node ls
Compose file + docker file
无缝的CI/CD过程:
1. 没有系统依赖
2. 升级分布式应用的任意部分
3. resource density can be optimized.
Containers简述
building an app the Docker way
Dockeerfile:
1. 定义了运行容器时你所有的需要
2. networking interface
3. disk drivers is virtualized,对于系统中的其他他是孤立的,所有需要map ports to the outside world
output from app.py is accessible over HTTP thanks to the EXPOSE command.
如果你配置了代理服务器
ENV http_proxy host:port
ENV https_proxy host:port
DNS settings
/etc/docker/daemon 不然可能会有pip的问题
Share image 的过程
docker login
docker tag image username/repository:part2
docker push username/repository:part2
docker stack ls # List stacks or apps
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker service ls # List running services associated with an app
docker service ps <service> # List tasks associated with an app
docker inspect <task or container> # Inspect task or container
docker container ls -q # List container IDs
docker stack rm <appname> # Tear down an application
docker swarm leave --force # Take down a single node swarm from the manager
Services
docker?
container 是一个容器,是一个平台?
镜像文件?
容器是镜像文件运行的实例,--CPU就开始给他分配内存(image包含程序运行的所需依赖)
比虚拟机好,原生许可?
1. 可以部署开发环境
2. 部署测试环境
image 文件:是一个package包含了运行软件所需的代码,库,环境,配置文件等...
容器和虚拟机对比
虚拟机:外来客
容器:随处可用?原生的-相当于就是一个QQ程序
就像python的.py程序打包成.exe可执行程序,就不用安装python环境,
不过他还有repository的功能,可以像GitHub一样拉取最新的信息,
所以可以把你的app部署到docker上
可以把app部署到多个containers
部署到集群cluster
Stack services by adding a backend database
安装docker仓库,
With Docker, you can just grab a portable Python runtime as an image(省去在新机器上安装编译环境等)
portable-images defined by something called Dockefile
dockfile:
1. 定义你的容器都有哪些东西(比如说Python环境啊,Java环境啊,MySQL环境等image),你只要和它联系上就行(比如通过端口号),然后把你要执行的application一并定义在这个文件上就可以啦
eg.1
# Use an official Python runtime as a parent image
FROM python:2.7-slim #我去仓库拿个镜像
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app #把你的app放到container中
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt #比如安装一些依赖的python库,根据requirements.txt (如re库,os库等等)
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
#当运行container 就调用.py程序
//代理的配置
创建 container
2. 常用指令 :
WORKDIR
ADD(把本地要执行的目录添加进容器中)
RUN (安装依赖包) ---requirements.txt
EXPOSE (暴露端口)
ENV : 添加环境变量
CMD :当container运行时,要执行的命令(比如运行一个Python程序)
代理的设置
ENV http_proxy host:port
ENV https_proxy host:port
eg. build the app
dockerfile app.py requirements.txt
//把这三个放到一个Docker image 里面构建
: docker build -t friendlyhello . //friendlyhello为image的名字
1. Dockerfile名字固定不可更改(docker 只认这个名字)
2. 开始下载,还是要下载,然后放到local Docker image registry
3. 不过,服务器迁移的时候,就能变得很方便了
在哪里创建镜像:
需在本地创立镜像仓库(image-registry) 把镜像注册到这里
//docker images
docker run -p 4000:80 friendlyhello
docker run -d -p 4000:80 friendlyhello //后台运行
machine 的 4000端口映射到container 的80端口,只给出主机上的4000端口,无法访问到80端口
localhost:4000 正确的URL,
curl http://localhost:4000
http://localhost:80--无法发访问 ---看到的是container的信息
docker 不同系统的停止运行的区别
docker container stop 《container 的ID 或者 name》
docker 运行后,你可以获得哪些相关信息
//docker container ls
如何上传分享你的image,无论何时何地都能运行,就像GitHub一样,申请个账号
push to registries
registry 》repositories 》image 三者之间的关系
使用docker公共的registry,省事,很多东西都配置好了,你也可以建立自己私有的registry
创立账户 (和GitHub有点像)
仓库: 本地仓库 --》 注册它
docker login
// username/repository:tag
eg.1
get-started:part2->把image放入 “get-started”仓库,并给它贴上标签“part2”
指定tag的作用:dockerhub默认tag是latest,所有拉取时默认拉取最后一个版本
eg.2
docker tag image username/repository:tag
//这个是在本地仓库
eg.3 // 放到远程仓库
docker push username/repository:tag
------------------------
和GitHub很像很像 ,DockerHub??
本地和远程的区别
docker run -p 4000:80 username/repository:tag
eg.1:
docker run -p 4000:80 john/get-started:part2
//本地找不到,就上dockerhub上去pull,以此来实现anywhere,未指定tag 默认latest
----到此你就创建了一个friendhello的image,container就是他的实例,并下载所需的东西
Services
负载均衡?分布式应用?
把不同的服务放在不同的服务器?
we scale our application and enable load-balancing
A service only runs one image
what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on.
Services are really just “containers in production.” A service only runs one image,
定义了image,定义了服务,固定了,能快速无缝更改么?
docker-compose.yml 文件-方便省事,定义了container 的行为
包含多个镜像,替代“username/repo:tag”
这个文件可以做出什么指示:
1. 从registry pull image
2. 把image实例化,并按指定分配系统相应的系统资源(limits)
3. 如果失败立即重启container
4. 指定4000端口映射到web的80端口
5. 调用webnet服务 load-balanced?
6. 定义webnet服务
命令:
docker swarm init //集群开启
docker stack deploy -c docker-compose.yml getstartedlab //保存或更新getstartedlab
docker service ls
docker service ps getstartedlab_web //查看task id
Scale the app:
docker stack deploy -c docker-compose.yml getstartedlab
docker container ls -q
docker stack rm getstartedlab
docker swarm leave --force
开启服务
docker stack deploy
Swarms
理解什么是集群,
搭建了集群,现在使用docker命令就是针对cluster by a swarm manager----nodes
A swarm is a group of machines that are running Docker and joined into a cluster
swarm manager --in a swarm that can execute your commands
nodes(physical or virtual)
several strategies to run containers
workers(提供空间的劳力,但互相不知道)
搭建集群:
docker swarm init -》 进入swarm mode and make current machine to be a swarm manage
docker swarm join ->其他机器通过这个命令加入集群
docker-machine create --driver virtualbox myvm1
成为王者:
docker-machine ssh myvm1 "docker swarm init --advertise-addr <myvm1 ip>"
//docker-machine ssh to talk to the VMs
..收服手下
其他一些输出指令
在王者的范围里放下一颗地雷///deploy your app on the swarm cluster
另一种给手下发送指令的方法:
docker-machine env
docker-machine ssh 和 docker-machine env 的对比
Unsetting docker-machine shell variable settings:
eval $(docker-machine env -u)
stacks
A stack is a group of interrelated services that share dependencies, and can be orchestrated and scaled together.
it is easy to add services to out docker-compose.yml file
1. Add a new service and redeploy
2. Persist the data
redis:
image: redis
ports:
- "6379:6379"
volumes:
- "/home/docker/data:/data"
deploy:
placement:
constraints: [node.role == manager]
command: redis-server --appendonly yes
networks:
- webnet
docker-machine ssh myvm1 "mkdir ./data"
Docker CLI(command line interface)
docker-client docker-daemon Registry
docker pull
docker run
docker push
Docker object
images containers networks
volume plugins other objects
Manage application data
1. Volumes :/var/lib/docker/volumes ,managed by Docker
创建:
1. docker volume create...
2. 创建容器时或者服务时
移除:
1. docker volume prune
命名机制: 不指定就随机,保证唯一性
volume drivers
2. Bind mounts: in host files ,can change the host filesystem
使用场景:
3. tmpfs mount : in memory
storing non-persistent state or sensitive information
tag :--mount
使用场景:危险数据
Use volumes
1. 易备份和迁移
2. 使用docker CLI 或者API 进行管理
3. 多平台,Linux和windows container
4. 易于多个容器之间安全共享
5. 能存储到云端,或者远程主机,对内容进行加密,安全
6. pre-populated
Run your app in production(如何部署app)
简述
如何手动开启docker ,当有问题时如何debug
- 配置和开启docker服务
开启:
1. dockerd daemon
默认配置
2. 手动开启 docker daemon:
dockerd (可能需要sudo权限) ---运行在前台,便于debug
3. 手动关闭: Ctrl-C
配置:
1. 很多配置的flags作为参数传入CLI
$ dockerd -D --tls=true --tlscert=/var/docker/server.pem --tlskey=/var/docker/serverkey.pem -H tcp://192.168.59.3:2376
-D: 开启debug模式
开启TLS模式--服务器验证certificate和key
-H 指定网络监听连接接口
还可以配置自动开启容器\限定容器的资源\配置存储驱动和容器安全
2. 修改daemon.json文件(持久生效)
{
"debug": true,
"tls": true,
"tlscert": "/var/docker/server.pem",
"tlskey": "/var/docker/serverkey.pem",
"hosts": ["tcp://192.168.59.3:2376"]
}
冲突解决
Script和daemon.json
3. 脚本指令和daemon.json配置文件冲突(上述1和2两种方式)
删除其中一个即可
4. 其他常见冲突:
4.1 改变默认的daemon address(监听通过socket方式)
1. -H 标签一直有效(系统使用systemd服务)
2. 修改daemon.json 会有冲突
创建新文件: /etc/systemd/system/docker.service.d/docker.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd
需要在json文件或者通过-H标签指定host 否则失败 //在Windows和Mac暂不支持修改json文件指定host
4.2 HTTP or HTTPS proxy
4.3 sudo systemctl daemon-reload
在开启docker前,使用此命令,如果docker成功开启,监听端口就为json文件中写入hosts key 而不是socket
- Troubleshoot the daemon
1. 开启debug模式 -D //hosts’ startup scripts will create
2. force a full stack trace // 发送SIGUSR signal to be logged
$ sudo kill -SIGUSR1 $(pidof dockerd)
//并不会停止daemon,daemon会显示stack trace ,并dump到这个log
//stack trace 决定了所有goroutines(并发?)和线程的状态,下述第五条讲解
修改daemon.json文件 // usually located in /etc/docker/
{
"debug": true
"log-level":(debug, info, warn, error, fatal)
}
3. $ sudo kill -SIGHUP $(pidof dockerd)
//hub signal --$ sudo kill -SIGHUP $(pidof dockerd)
//kill 信号 杀死或者重启等
- Out Of Memory Exceptions
1. 已经为docker分配了内存空间,但是你想使用更多的内存
2. 这样会有 Out of memory Exception(OOME)--这样就会导致container或者daemon被内核的OOM杀死
3. 如何解决:
分配充分的内存空间,以及了解内存不足时的风险
- Read the logs (如何分析日志)
日志的默认位置根据系统的不同而不同
Operating system | Location 2 |
---|---|
RHEL, Oracle Linux | /var/log/messages |
Debian | /var/log/daemon.log |
Ubuntu 16.04+, CentOS | Use the command journalctl -u docker.service |
Ubuntu 14.10- | /var/log/upstart/docker.log |
macOS | ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/console-ring |
Windows | AppData\Local |
- View stack traces(堆栈跟踪)
5.1 如何找到log文件
journalctl -u docker.service
/var/log/messages, /var/log/daemon.log, or /var/log/docker.log on older Linux systems
Get-EventLog -LogName Application -Source Docker -After (Get-Date).AddMinutes(-5) | Sort-Object Time on Docker EE for Windows Server
eg.1 saves these stack traces and dumps
可以在log中找到这些东西的存放位置
- check whether Docker is running(检查docker服务是否正在运行)
1. docker info //操作系统和docker服务进行通信
系统自带的服务来查看
2. sudo systemctl is-active docker or sudo status docker or sudo service docker status
Collect Docker metrics with Prometheus
简介
Prometheus是一个开源监视警告的工具包,
可以配置docker使他作为一个prometheus的target
set up Prometheus to run as a Docker container
这样可以通过PrometheUS监控你的docker instance
在docker中配置PrometheUS
把docker服务作为prometheus 的target,需要指定metrics-address
通过修改 daemon.json
{
"metrics-addr" : "127.0.0.1:9323",
"experimental" : true
}
网友评论