软盘映像使用_如何使用Dockerfile构建映像

软盘映像使用

Building an image with Dockerfile

Building the app, installing the dependencies and services, automating the deployment, and more — it all starts with the Dockerfile. Let’s review the syntax, from basic to elaborate, and some best practices when building your Docker images.

构建应用程序,安装依赖项和服务,自动执行部署等等—所有这些都始于Dockerfile。 让我们回顾一下从基本到详尽的语法,以及构建Docker映像时的一些最佳实践。

In this guide, we’ll write a Dockerfile instructing Docker to select a minimal Linux (base image) for the application we’ll deliver, and ship with it a set of tools of our choice and a certain configuration, effectively building our own Linux distribution which is just right for running our app.

在本指南中,我们将编写一个Dockerfile来指示Docker为我们将交付的应用程序选择一个最小的Linux(基本映像),并随其附带一组我们选择的工具和某种配置,以有效地构建我们自己的Linux发行版正好适合运行我们的应用。

为什么选择Docker (Why Docker)

docker_logo

With Docker you can “Build, ship, and run any app, anywhere”. That is, you can pack your application with all of the binaries and runtime libraries, back-end tools, OS tweaks, and even specific services your application needs for running — and make it readily available for instant delivery and automatic deployment.

使用Docker,您可以“在任何地方构建,交付和运行任何应用程序”。 也就是说,您可以将应用程序与所有二进制文件和运行时库,后端工具,操作系统调整甚至您的应用程序运行所需的特定服务一起打包在一起,并使其易于即时交付和自动部署。

The software containers technology that Docker implements is what makes this possible. And although I won’t cover here much of the detail behind it, you can read more about Docker, what software containers are, and how they work in Understanding Docker, Containers and Safer Software Delivery.

Docker实施的软件容器技术使之成为可能。 而且,尽管在这里我不会在其中涵盖很多细节,但是您可以在了解Docker,容器和更安全的软件中阅读有关Docker的更多信息,什么是软件容器以及它们如何工作。

安装Docker (Installing Docker)

Before starting, you’ll need to have Docker installed, whether it’s on your local machine or on a remote server.

在开始之前,您需要安装Docker,无论它是在本地计算机上还是在远程服务器上。

Fortunately, the latest version of Docker (1.12 as of this writing) made the installation process really smooth, and you have easy-to-follow guides for Windows, MacOS and Linux.

幸运的是,最新版本的Docker(在撰写本文时为1.12)使安装过程非常顺利,并且您具有易于使用的WindowsMacOSLinux指南。

Dockerfile (The Dockerfile)

In order to build an image in Docker, you first need to set the instructions for this build on a plain text file named Dockerfile and a context (more on this later). This file has a syntax similar to that of Apache configuration files — one instruction per line with its respective arguments, and all instructions processed in sequence, one after another. Comments are preceded by the # character and a whitespace. Finally, once you have a Dockerfile, the command docker build will build the image, as we’ll see in more detail later.

为了在Docker中构建映像,您首先需要在名为Dockerfile的纯文本文件和上下文上设置此构建的说明(稍​​后会对此进行更多介绍)。 该文件的语法类似于Apache配置文件的语法-每行一条指令及其各自的参数,所有指令依次处理。 注释前面带有#字符和空格。 最后,有了Dockerfile后,命令docker docker build将构建映像,我们将在后面详细介绍。

Before we start writing the Dockerfile, we’ll set the working space. We’ll create a directory called my_image in our home directory, use it as our working directory, and place the Dockerfile in there:

在开始编写Dockerfile之前,我们将设置工作空间。 我们将在主目录中创建一个名为my_image目录,将其用作我们的工作目录,并将Dockerfile放置在该Dockerfile中:

mkdir ~/my_build
cd ~/my_build
touch Dockerfile

Now we’re ready to start building the image.

现在我们准备开始构建图像。

选择基本图像 (Selecting the base image)

Most of the time when creating an image, you’ll use a starting point — that is, another image. This can be an official Ubuntu, MySQL, WordPress, or any other image available from the Docker Hub. You can also use an image you created yourself previously.

在大多数情况下,创建图像时,您将使用一个起点-即另一个图像。 这可以是官方的UbuntuMySQLWordPressDocker Hub中可用的任何其他映像。 您也可以使用以前创建的图像。

Note: You can create your own base image with your own core tools and directory structure, using Docker’s reserved, minimal image, called scratch. It’s a process I won’t cover here, but you can refer to the Docker site’s guide on creating a base image.

注意 :您可以使用自己的核心工具和目录结构创建自己的基础映像,并使用Docker保留的最小映像(称为scratch 。 这是我不会在此处介绍的过程,但是您可以参考Docker网站的有关创建基础映像的指南。

For example, if you want to start off with a minimal Debian distribution, you’ll add the following content to the Dockerfile:

例如,如果您想以最小的Debian发行版开始,则将以下内容添加到Dockerfile

# set the base image
FROM debian

FROM must be the first instruction you use when writing a Dockerfile. Notice that you can also use a specific version of your base image, by appending : and the version_name at the end of the image name. For example:

FROM必须是编写Dockerfile时使用的第一条指令。 请注意,您还可以通过在图像名称的末尾附加:version_name来使用基本图像的特定版本。 例如:

# set the base image
FROM debian:sid

In the code above, we’re using the “sid” Debian (unstable distribution). This will be relevant also when you want a specific version of a Ruby or Python interpreter, MySQL version, or what have you, when you use an official base image for any of these tools. For now, we’ll stick to the default (stable) debian image for this guide.

在上面的代码中,我们使用的是“ sid” Debian(不稳定的发行版)。 当您要使用特定版本的Ruby或Python解释器,MySQL版本或您拥有的版本时,或者将正式基础映像用于这些工具中的任何一个时,这也将相关。 现在,我们将继续使用本指南的默认(稳定) debian映像。

指定维护者并添加元数据 (Specifying a maintainer and adding metadata)

Optionally, you can specify who’s the MAINTAINER, replacing Lucero del Alba by your name or the person or team responsible for the build:

(可选)您可以指定谁是MAINTAINER ,用您的姓名或负责构建的人员或团队代替Lucero del Alba

# author
MAINTAINER Lucero del Alba

It isn’t necessary, but we may also add some metadata using the LABEL instruction, and this information will become available later when using the docker inspect command to examine the image:

不必要,但是我们也可以使用LABEL指令添加一些元数据,并且稍后使用docker inspect命令检查图像时,此信息将变得可用:

# extra metadata
LABEL version="1.0"
LABEL description="First image with Dockerfile."

For more on this feature, refer to Docker object labels.

有关此功能的更多信息,请参阅Docker对象标签

制作自己的发行版 (Making your own distro)

At this point, we’re going to select some tools and libraries to be included in our image, so that our container has everything it needs for what we intend it to do. At the end of this tutorial, we’ll be doing something that’s very close to actually building a Linux distribution.

在这一点上,我们将选择一些工具和库来包含在我们的映像中,以使我们的容器具有实现其预期功能所需的一切。 在本教程的最后,我们将做的事情与实际构建Linux发行版非常接近。

Some containers, such as one running a PostgreSQL database, are meant to run in the background. But often we need a console to perform some operations on the container, so we’re likely to need some extra tools, because the base image will bundle just a minimal set of GNU tools.

有些容器(例如运行PostgreSQL数据库的容器)应在后台运行。 但是通常我们需要一个控制台来对容器执行一些操作,因此我们可能需要一些额外的工具,因为基本映像只会捆绑最少的一组GNU工具。

处理缓存问题 (Dealing with cache issues)

It’s almost guaranteed that you’ll experience cache issues when trying to install additional packages on your image. This is because the base image comes with cached metadata, and the live repositories you’re pulling data from are often changing.

几乎可以保证在尝试在映像上安装其他程序包时会遇到缓存问题。 这是因为基本映像带有缓存的元数据,并且您从中提取数据的实时存储库经常在变化。

In Debian-based distributions, you can handle this by adding the following commands before installing new packages:

在基于Debian的发行版中,您可以通过在安装新软件包之前添加以下命令来解决此问题:

# update sources list
RUN apt-get clean
RUN apt-get update
安装基本工具 (Installing basic tools)

Code editors, locales, tools such as git or tmux — this is the time to install everything you’re going to need later, so that they’re bundled in the image.

代码编辑器,语言环境以及gittmux工具-这是时候安装以后需要使用的所有内容,以便它们捆绑在映像中了。

We’ll install one per line:

我们将在每行安装一个:

# install basic apps, one per line for better caching
RUN apt-get install -qy git
RUN apt-get install -qy locales
RUN apt-get install -qy nano
RUN apt-get install -qy tmux
RUN apt-get install -qy wget

We could install all of them in a single line, but if we later want to add or remove a package, we need to re-run the whole process. So the best practice here is to install one package per line so you can benefit from Docker’s caching.

我们可以将它们全部安装在一行中,但是如果以后要添加或删除软件包,则需要重新运行整个过程。 因此,这里的最佳实践是每行安装一个软件包,以便您可以受益于Docker的缓存。

Also, keep it tight. You don’t want to install tools “just in case”, as this may increase the build time and the image size.

另外, 请保持紧紧 。 您不希望“以防万一”安装工具,因为这可能会增加构建时间和图像大小。

为您的应用安装运行时库 (Installing runtime libraries for your app)

We’ll be shipping our app in this image as well. Do you need a specific version of PHP, Ruby or Python, together with certain modules? Now’s the time to deliver all of the programs and runtimes our app is going to need.

我们也将以该图像运送我们的应用程序。 您是否需要特定版本PHP,Ruby或Python,以及某些模块? 现在是交付应用程序需要的所有程序和运行时的时候了。

Be as specific as you like, as this container is intended to run only your app:

尽量具体,只要你喜欢,因为这个容器是为了运行你的应用程序:

# install app runtimes and modules
RUN apt-get install -qy python3
RUN apt-get install -qy python3-psycopg2
RUN apt-get install -qy python3-pystache
RUN apt-get install -qy python3-yaml

For this example, we’ll install Python 3 with the packages Psycopg 2 (to connect to PostgreSQL databases), the Mustache for Python module, and the YAML module. (You’ll naturally install the specific dependencies you need when doing your own Dockerfile.)

在此示例中,我们将安装Python 3,其中包含Psycopg 2软件包(用于连接到PostgreSQL数据库),Mustache for Python模块和YAML模块。 (您自然会在执行自己的Dockerfile时安装所需的特定依赖项。)

编译和下载软件包 (Compiling and downloading packages)

It’s also possible that your distribution won’t have a package for a certain module or program that you need. But you don’t need to manually install it in your running container! Instead, you can use the RUN instruction (one per line) to batch the process of downloading, compiling and setting whichever library your application will need.

您的发行版也可能没有针对您所需的某个模块或程序的软件包。 但是您无需在运行中的容器中手动安装它! 相反,您可以使用RUN指令(每行一条)来批处理下载,编译和设置应用程序所需的任何库的过程。

You can even write a script on a separate file, add this file to the build and run it, as we’ll see later in the “Shipping Your Own App” section.

您甚至可以在一个单独的文件上编写脚本,将该文件添加到构建中并运行它,我们将在稍后的“运输自己的应用程序”部分中看到。

打扫干净 (Cleaning up)

To keep your image tidy and as small as possible, it’s also a good idea to do a cleanup at the end of the installation sequence:

为了使映像整洁并尽可能地小,在安装序列结束时进行清理也是一个好主意:

# cleanup
RUN apt-get -qy autoremove

Again, notice we’re using apt-get because we chose Debian, but use the appropriate command for the distribution of your base image.

再次注意,因为我们选择了Debian,所以我们使用apt-get ,但是使用适当的命令分发基本映像。

运送您自己的应用 (Shipping your own app)

The whole point of building this environment is so that you can deliver your application smoothly and ready to run. To add files, directories, and even the content of remote URLs to the image, we’ll use the ADD instruction.

构建此环境的重点是使您可以顺利交付应用程序并准备运行。 要将文件,目录甚至远程URL的内容添加到图像,我们将使用ADD指令。

However, before adding files, we need to put them in the appropriate context. To make things easier, we’ll just locate everything in the aforementioned my_build directory, alongside the Dockerfile itself.

但是,在添加文件之前,我们需要将它们放在适当的上下文中 。 为了使事情变得简单,我们将所有内容都放置在上述my_build目录中,以及Dockerfile本身。

Let’s say that, with the app and everything we want to put into the image, we have the following files in ~/my_build (where app.py and lib.py are inside the sub-directory app/):

假设使用该应用程序以及我们想要放入图像中的所有内容,我们在~/my_build拥有以下文件(其中app.pylib.py位于子目录app/ ):

.bashrc
.profile
app/app.py
app/lib.py
Dockerfile

We’ll add .bashrc and .profile scripts to the /root directory in the container so that they execute whenever we launch a shell on the container, and we’ll copy the contents of app/ to the /app/ directory in the container.

我们将.bashrc.profile脚本添加到容器中的/root目录中,以便它们在我们在容器上启动shell时执行,然后将app/的内容复制到容器中的/app/目录中。

We add the following instructions:

我们添加以下说明:

# add scripts to the container
ADD .bashrc /root/.bashrc
ADD .profile /root/.profile

# add the application to the container
ADD app /app

设定环境 (Setting your environment)

Finally, we’ll set some environment variables that we’ll need at a system and application level.

最后,我们将设置系统和应用程序级别所需的一些环境变量。

Many of you will do just fine with the default Debian charset, but since we’re aiming at an international audience, let’s see how to have a UTF-8 terminal. We previously installed the locales package, so all we have to do now is generate the charsets and set the appropriate Linux environment:

你们中的许多人都会使用默认的Debian字符集做的很好,但是由于我们的目标读者是国际用户,因此让我们看看如何使用UTF-8终端。 我们之前安装了locales软件包,因此现在要做的就是生成字符集并设置适当的Linux环境:

# locales to UTF-8
RUN locale-gen C.UTF-8 && /usr/sbin/update-locale LANG=C.UTF-8
ENV LC_ALL C.UTF-8

You may also need to set some environment variables for your application, for exchanging passwords and paths. The Dockerfile provides the ENV instruction for doing precisely this:

您可能还需要为应用程序设置一些环境变量,以交换密码和路径。 Dockerfile提供了ENV指令来精确地做到这一点:

# app environment
ENV PYTHONIOENCODING UTF-8
ENV PYTHONPATH /app/

Notice that you can also pass environment variables from the command line when launching the container, which may be convenient for sharing some sensitive information such as passwords.

请注意,您还可以在启动容器时从命令行传递环境变量,这对于共享某些敏感信息(例如密码)可能很方便。

完整的Dockerfile (The Complete Dockerfile)

Naturally, you’ll have to adapt the Dockerfile to your needs, but hopefully you get the idea of the possibilities.

自然,您必须根据需要调整Dockerfile,但希望您对可能的想法有所了解。

Here’s the full file:

这是完整的文件:

# author
MAINTAINER Lucero del Alba

# extra metadata
LABEL version="1.0"
LABEL description="First image with Dockerfile."

# set the base image
FROM debian

# update sources list
RUN apt-get clean
RUN apt-get update

# install basic apps, one per line for better caching
RUN apt-get install -qy git
RUN apt-get install -qy locales
RUN apt-get install -qy nano
RUN apt-get install -qy tmux
RUN apt-get install -qy wget

# install app runtimes and modules
RUN apt-get install -qy python3
RUN apt-get install -qy python3-psycopg2
RUN apt-get install -qy python3-pystache
RUN apt-get install -qy python3-yaml

# cleanup
RUN apt-get -qy autoremove

# add scripts to the container
ADD .bashrc /root/.bashrc
ADD .profile /root/.profile

# add the application to the container
ADD app /app

# locales to UTF-8
RUN locale-gen C.UTF-8 && /usr/sbin/update-locale LANG=C.UTF-8
ENV LC_ALL C.UTF-8

# app environment
ENV PYTHONIOENCODING UTF-8
ENV PYTHONPATH /app/

建立形象 (Building the image)

From inside the my_build directory, we’ll use the docker build command, passing the -t flag to “tag” the new image with a name, which in this case will be my_image. The . indicates that the Dockerfile is in the current directory, along with so-called “context” — that is, the rest of the files that may be in that location:

my_build目录内部,我们将使用my_build docker build命令,传递-t标志以使用名称“标记”新映像,在本例中为my_image 。 的. 表示Dockerfile和所谓的“上下文”位于当前目录中,也就是说,可能在该位置的其余文件:

cd ~/my_build
docker build -t my_image .

That will generate a long output where every “step” is an instruction in our Dockerfile. This is a truncated output:

这将产生很长的输出,其中每个“步骤”都是Dockerfile中的一条指令。 这是一个截断的输出:

Sending build context to Docker daemon  5.12 kB
Step 1 : FROM debian
 ---> 7b0a06c805e8
Step 2 : MAINTAINER Lucero del Alba
 ---> Running in d37e46e5455d
 ---> 2d76561de558
Removing intermediate container d37e46e5455d
Step 3 : LABEL version "1.0"
 ---> Running in 904dde1b4cd7
 ---> a74b7a492aaa
Removing intermediate container 904dde1b4cd7
Step 4 : LABEL description "First image with Dockerfile."
 ---> Running in 9aaef0353256
 ---> 027d8c10e966
Removing intermediate container 9aaef0353256
Step 5 : RUN apt-get clean
 ---> Running in bc9ed85dda16
 ---> a7407036e74a
Removing intermediate container bc9ed85dda16
Step 6 : RUN apt-get update
 ---> Running in 265e757a7563
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Ign http://deb.debian.org jessie InRelease
Get:2 http://deb.debian.org jessie-updates InRelease [145 kB]
Get:3 http://deb.debian.org jessie Release.gpg [2373 B]
Get:4 http://deb.debian.org jessie Release [148 kB]
Get:5 http://security.debian.org jessie/updates/main amd64 Packages [402 kB]
Get:6 http://deb.debian.org jessie-updates/main amd64 Packages [17.6 kB]
Get:7 http://deb.debian.org jessie/main amd64 Packages [9064 kB]
Fetched 9843 kB in 10s (944 kB/s)
Reading package lists...
 ---> 93fa0a42fcdc
Removing intermediate container 265e757a7563
Step 7 : RUN apt-get install -qy git
 ---> Running in c9b93cecd953
(...)

列表图片 (Listing images)

We can list our images with the docker images command:

我们可以使用docker images命令列出docker images

docker images

This will output our newly created my_image alongside other base images we have downloaded:

这将输出我们新创建的my_image以及我们下载的其他基本图像:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my_image            latest              e71dc183df2b        8 seconds ago       305.6 MB
debian              latest              7b0a06c805e8        2 weeks ago         123 MB
debian              sid                 c1857cb435d7        3 weeks ago         97.77 MB

… and there it is, our image is ready to ship and run!

…在那里,我们的形象已准备就绪,可以运行

启动一个容器 (Launching a container)

Finally, to launch an interactive terminal of our newly created image, we’ll use the docker run command:

最后,要启动我们新创建的图像的交互式终端,我们将使用docker run命令:

docker run -ti my_image /bin/bash

接下来做什么 (What To Do Next)

I haven’t covered all of the possibilities of the Dockerfile. Particularly, I haven’t reviewed how to EXPOSE ports so that you can run services and even link containers between themselves; how to HEALTHCHECK containers to verify they’re still working; or even how to specify a VOLUME to store and recover data from the host machine … among other useful features.

我还没有介绍Dockerfile的所有可能性。 特别是,我没有讨论如何EXPOSE端口,以便您可以运行服务,甚至在它们之间链接容器。 如何HEALTHCHECK集装箱,以验证他们还在工作; 甚至其他如何指定VOLUME以从主机存储和恢复数据的方法……等等。

We’ll get to cover those on future articles. For now, you may like to check out the following resources.

我们将在以后的文章中介绍这些内容。 目前,您可能想查看以下资源。

From the Docker website:

从Docker网站:

From SitePoint:

从SitePoint:

翻译自: https://www.sitepoint.com/how-to-build-an-image-with-the-dockerfile/

软盘映像使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值