本文是对官方文档的总结与备注。
官方文档:https://docs.docker.com/develop/
镜像选择
alpine:3.7
(首选)debian:stretch-slim
(次选)
常见系统镜像大小对比
1 | REPOSITORY TAG IMAGE ID CREATED SIZE |
根据官方文档的层次,分为
容器 (
Containers
) 使用Docker run
服务 (
Services
) 使用Docker Compose
Defines how containers behave in production服务栈 (
Stack
) 使用Swarm mode
Defining the interactions of all the services
必须知道
使用
Dockerfile
构建镜像使用
multistage builds
保持镜像最小使用
Volume
和bind mounts
管理数据使用
docker swarm
部署服务使用
docker stack
部署服务栈compose 文件
普遍的应用开发最佳实践
Docker development best practices
Docker 开发最佳实践
如何保持镜像最小
Start with an appropriate base image. 如果你需要 JDK,则直接使用官方的
openjdk
镜像,而不要基于ubuntu
安装openjdk
Use
multistage builds
. 使用多阶段构建,如果你的 Docker 版本不支持多阶段构建
,请请尽可能减少镜像层数。使用你自己的基础镜像
保持生产环境镜像尽可能小,但允许调试
使用有明确含义的镜像标签
prod
或者test
,尽量不使用latest
标签。
Where and how to persist application data
应用数据如何存储,存放在哪里
避免
将数据存放在镜像中使用
volumes
存放数据在开发环境使用
bind mounts
,在生产环境使用volume
Use swarm services when possible
在可能的情况下使用
Swarm mode
哪怕仅需要运行一个容器,
Swarm mode
能提供更多的功能通过
Swarm
服务,网络和数据卷能够连接和断开一些功能只在
服务
中可用,比如secrets
config
,上一部分已经提到使用
docker stack deploy
pull 镜像,而不是使用docker pull
Use CI/CD for testing and deployment
当程序源码改变(commit、tag)或创建了一个
Pull request
,使用 Docker Cloud 或者其他 CI/CD 自动构建镜像和创建镜像标签并自动测试镜像。Docker cloud 可以把测试通过的镜像部署到生产环境中。使用 Docker EE ,安全团队 sign 一个镜像,之后部署到生产环境中。
Differences in development and production environments
Development | Production |
---|---|
Use bind mounts to give your container access to your source code. | Use volumes to store container data. |
Use Docker for Mac or Docker for Windows. | Use Docker EE if possible, with userns mapping for greater isolation of Docker processes from host processes. |
Don’t worry about time drift. | Always run an NTP client on the Docker host and within each container process and sync them all to the same NTP server. If you use swarm services, also ensure that each Docker node syncs its clocks to the same time source as the containers. |
镜像管理
Docker Hub
Dcoekr Registry 私有仓库
https://www.khs1994.com/docker/registry.html