Docker containers run on and made changes to images. Generally, we use existing images which is like a template to create a new container. A lot of Linux distribution and software provide serve Docker images. But in some cases, we may need to create or enhance the existing image. Dockerfile can be used to create new images by providing instructions about the new images.
Docker容器继续运行并更改了映像。 通常,我们使用类似于模板的现有图像来创建新容器。 许多Linux发行版和软件都提供服务Docker映像。 但是在某些情况下,我们可能需要创建或增强现有的图像。 通过提供有关新映像的说明,可以使用Dockerfile创建新映像。
Docker文件 (Dockerfile)
Dockerfile is a simple text file that contains configuration for the Docker Image which will be created. Dockerfile contains Dockerfile Commands to change the properties of the image. We can also call a Dockerfile as a recipe for an image.
Dockerfile是一个简单的文本文件,其中包含将要创建的Docker Image的配置。 Dockerfile包含用于更改映像属性的Dockerfile命令。 我们还可以将Dockerfile称为映像的配方。
Here is a simple and lean Dockerfile. This file simple derive Base Image from current and latest Ubuntu Docker image and then updates the existing packages. After update is completed apache2
package is install. Generally Dockerfile’s are more complicated then this example as we will see below.
这是一个简单而精简的Dockerfile。 该文件简单地从当前和最新的Ubuntu Docker映像派生基础映像,然后更新现有软件包。 更新完成后,将安装apache2
软件包。 通常,Dockerfile比这个示例要复杂得多,如下所示。
#This is a sample Image
FROM ubuntu
MAINTAINER[email protected]
RUN apt-get update
RUN apt-get install –y apache2
![Dockerfile](https://img-blog.csdnimg.cn/img_convert/685026ba009c1d253603d8ef8e02e09d.png)
构建Dockerfile并创建新映像(Build Dockerfile and Create New Image)
We will start with simple steps where we will build previously given Dockerfile example. This build will create an image with the -t
option of docker build
command poftut1
.
我们将从简单的步骤开始,在该步骤中,我们将构建先前给出的Dockerfile示例。 此构建将使用poftut1
docker build
命令poftut1
的-t
选项创建映像。
$ mkdir poftut1
$ cd poftut1/
$ vim Dockerfile
After the DockerFile is created with the example content we can build our image with docker build
command. We will require sudo because the current user does not have enough rights to create Docker image and we get root
privileges.
使用示例内容创建DockerFile之后,我们可以使用docker build
命令构建映像。 我们将需要sudo,因为当前用户没有足够的权限来创建Docker映像,并且我们获得了root
特权。
$ sudo docker build -t poftut1 .
![Build Dockerfile and Create New Image](https://img-blog.csdnimg.cn/img_convert/2087d1c92af80c5e388c28a7385c688b.png)
We can see from image given Dockerfile instructions are executed line by line.
从镜像中我们可以看到,Dockerfile指令是逐行执行的。
Step 1 is getting base
ubuntu
image步骤1正在获取基本的
ubuntu
映像- Step 2 is setting Maintainer information步骤2设置维护者信息
Step 3 is running
apt-get update
command步骤3正在运行
apt-get update
命令Step 4 is installing
apache2
withapt
package manager.步骤4是使用
apt
软件包管理器安装apache2
。
运行和使用Docker映像 (Run and Use Docker Image)
We can use this Docker image named poftut1
like a regular image for example ubuntu
. We will create a new container from poftut1
image with docker run
command.
我们可以像使用ubuntu
这样的常规映像一样使用名为poftut1
Docker映像。 我们将使用poftut1
docker run
命令从poftut1
映像创建一个新容器。
$ sudo docker run -t -i poftut1 bash
![Use Docker Image](https://img-blog.csdnimg.cn/img_convert/509c63213a0b46ea88cab5dbee447a91.png)
Dockerfile命令(Dockerfile Commands)
Upto now we have learned simple steps to build an image with Dockerfile
. there are a lot of different Dockerfile commands which can be used to run command, create file, create metadata, copy file etc.
到目前为止,我们已经学习了使用Dockerfile
构建映像的简单步骤。 有很多不同的Dockerfile命令可用于运行命令,创建文件,创建元数据,复制文件等。
添加–将文件从本地系统复制到映像中 (ADD – Copy File From Local System Into Image)
Add is very useful command which is used to copy files and folders from local system where docker runs into the image file. This can be used to copy special configuration, copy applications or data. We can also use URL’s for [SOURCE DIRECTORY] like https://www.poftut.com/data
添加是非常有用的命令,用于从docker运行到映像文件的本地系统复制文件和文件夹。 这可用于复制特殊配置,复制应用程序或数据。 我们也可以使用[SOURCE DIRECTORY]的URL,例如https://www.poftut.com/data