楼主: kychan
3617 25

【Sébastien Goasguen】Docker Cookbook [推广有奖]

21
吕涛 发表于 2016-8-27 07:28:54

  1. 2.5 Optimizing Your Dockerfile by Following Best Practices
  2. Problem
  3. You want to follow best practices to write your Dockerfiles and optimize your Docker
  4. images.
  5. Solution
  6. The Docker documentation has published best practices to write Dockerfiles. This
  7. recipe highlights a few of them to put you on your way to building good images:
  8. 1. Run a single process per container. Although you can run multiple processes per
  9. container (e.g., Recipe 1.15), building images that will run only one process or at
  10. least one functional service per container will help you build decoupled applications
  11. that can scale. Take advantage of container linking (see Recipe 3.3) or other
  12. container-networking techniques (see Chapter 3) to have the containers communicate
  13. with each other.
  14. 2. Do not assume that your containers will live on; they are ephemeral and will be
  15. stopped and restarted. You should treat them as immutable entities, which means
  16. that you should not modify them but instead restart them from your base image.
  17. Therefore, manage runtime configuration and data outside the containers and
  18. hence the image. Use Docker volumes (see Recipe 1.18 and Recipe 1.19) for this.
  19. 3. Use a .dockerignore file. When building images, Docker will copy the content of
  20. the working directory where the Dockerfile exists (i.e., the build context) inside
  21. the image. Using .dockerignore, you can exclude files and directories from being
  22. copied during the build process. If you do not use a .dockerignore file, make sure
  23. to build your image in a directory that has only the minimum required. Check
  24. the syntax for the .dockerignore file.
  25. 4. Use official images from Docker Hub instead of writing your own from scratch.
  26. These images are maintained and blessed by the projects authoring the software.
  27. You can also use ONBUILD images (see Recipe 2.10) to simplify even further your
  28. images.
  29. 5. Finally, minimize the number of layers of your images and take advantage of the
  30. image cache. Docker uses union filesystems to store images. This means that
  31. each image is made of a base image plus a collection of diffs that adds the
  32. required changes. Each diff represents an additional layer of an image. This has a
  33. direct impact on how your write your Dockerfile and use the various directives.
  34. The following section illustrates this point further.
复制代码

22
吕涛 发表于 2016-8-27 07:30:40

  1. 2.7 Migrating from Vagrant to Docker with the Docker
  2. Provider
  3. Problem
  4. You have been using Vagrant for your testing and development work and would like
  5. to reuse some of your Vagrantfiles to work with Docker.
  6. Solution
  7. Use the Vagrant Docker provider. You can keep writing Vagrant files to bring up new
  8. containers and develop your Dockerfiles.
  9. Here is an example Vagrantfile that uses the Docker provider:
  10. # -*- mode: ruby -*-
  11. # vi: set ft=ruby :
  12. VAGRANTFILE_API_VERSION = "2"
  13. Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  14. config.vm.provider "docker" do |d|
  15. d.build_dir = "."
  16. end
  17. config.vm.network "forwarded_port", guest: 5000, host: 5000
  18. end
  19. The build_dir option looks for a Dockerfile in the same directory as the Vagrantfile.
  20. Vagrant then issues a docker build in turn, and starts the container:
复制代码

23
吕涛 发表于 2016-8-27 07:32:04
  1. 2.8 Using Packer to Create a Docker Image
  2. Problem
  3. You have developed several configuration management recipes using Chef, Puppet,
  4. Ansible, or SaltStack. You would like to reuse those recipes to build Docker images.
  5. Solution
  6. Use Packer from HashiCorp. Packer is a tool to create identical machine images for
  7. multiple platforms from a single template definition. For example, from a template it
  8. can automatically create images for Amazon EC2 (an Amazon Machine Image, or
  9. AMI), VMware, VirtualBox, and DigitalOcean. One of those target platforms is
  10. Docker.
  11. This means that if you define a Packer template, you can automatically generate a
  12. Docker image. You can also post-process it to tag the image and push it to Docker
  13. Hub (see Recipe 2.9).
复制代码

24
Nicolle 学生认证  发表于 2017-4-14 23:12:35
提示: 作者被禁止或删除 内容自动屏蔽

25
Nicolle 学生认证  发表于 2017-4-14 23:14:51
提示: 作者被禁止或删除 内容自动屏蔽

26
Nicolle 学生认证  发表于 2017-4-14 23:18:18
提示: 作者被禁止或删除 内容自动屏蔽

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-25 20:04