- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 124434 个
- 通用积分
- 1.2984
- 学术水平
- 10 点
- 热心指数
- 9 点
- 信用等级
- 7 点
- 经验
- 2167 点
- 帖子
- 129
- 精华
- 1
- 在线时间
- 16 小时
- 注册时间
- 2016-8-11
- 最后登录
- 2017-10-4
|
21楼
吕涛
发表于 2016-8-27 07:28:54
|
- 2.5 Optimizing Your Dockerfile by Following Best Practices
- Problem
- You want to follow best practices to write your Dockerfiles and optimize your Docker
- images.
- Solution
- The Docker documentation has published best practices to write Dockerfiles. This
- recipe highlights a few of them to put you on your way to building good images:
- 1. Run a single process per container. Although you can run multiple processes per
- container (e.g., Recipe 1.15), building images that will run only one process or at
- least one functional service per container will help you build decoupled applications
- that can scale. Take advantage of container linking (see Recipe 3.3) or other
- container-networking techniques (see Chapter 3) to have the containers communicate
- with each other.
- 2. Do not assume that your containers will live on; they are ephemeral and will be
- stopped and restarted. You should treat them as immutable entities, which means
- that you should not modify them but instead restart them from your base image.
- Therefore, manage runtime configuration and data outside the containers and
- hence the image. Use Docker volumes (see Recipe 1.18 and Recipe 1.19) for this.
- 3. Use a .dockerignore file. When building images, Docker will copy the content of
- the working directory where the Dockerfile exists (i.e., the build context) inside
- the image. Using .dockerignore, you can exclude files and directories from being
- copied during the build process. If you do not use a .dockerignore file, make sure
- to build your image in a directory that has only the minimum required. Check
- the syntax for the .dockerignore file.
- 4. Use official images from Docker Hub instead of writing your own from scratch.
- These images are maintained and blessed by the projects authoring the software.
- You can also use ONBUILD images (see Recipe 2.10) to simplify even further your
- images.
- 5. Finally, minimize the number of layers of your images and take advantage of the
- image cache. Docker uses union filesystems to store images. This means that
- each image is made of a base image plus a collection of diffs that adds the
- required changes. Each diff represents an additional layer of an image. This has a
- direct impact on how your write your Dockerfile and use the various directives.
- The following section illustrates this point further.
复制代码
|
|