如何使用DockerFile为Docker容器构建映像?

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
Dockerfile
Docker文件

构建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
Build Dockerfile and Create New Image
构建Dockerfile并创建新映像

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 with apt 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
Use Docker Image
使用Docker映像

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值