Dockerfiles

A Dockerfile is the recipe to build a Docker image.

Dockerfile是构建Docker映像的秘诀。

This is the workflow: first you create a Dockefile, then you built a Docker image from it using docker build, and finally you run a container from the image.

这是工作流程:首先创建一个Dockefile,那么你用从它内置了码头工人形象docker build ,最后您从图像的容器。

A Dockerfile is a text file with instructions on how to build an image.

Dockerfile是一个文本文件,其中包含有关如何构建映像的说明。

Those instructions are part of a configuration language, which includes keywords like FROM, LABEL, RUN, COPY, ENTRYPOINT, CMD, EXPOSE, ENV and more.

这些指令是配置语言的一部分,其中包括诸如FROMLABELRUNCOPYENTRYPOINTCMDEXPOSEENV EXPOSE

Let’s create our first Dockerfile:

让我们创建第一个Dockerfile:

Let’s say you have a folder with a simple Node.js app composed by an app.js, a package.json file that lists a couple dependencies you need to install before running the app, and package-lock.json.

假设您有一个包含由app.js组成的简单Node.js应用程序的文件夹,一个package.json文件(其中列出了在运行该应用程序之前需要安装的几个依赖项)和package-lock.json

Inside it, create a plain text file named Dockerfile, with no extension, with this content:

在其中创建一个纯文本文件,名为Dockerfile ,不带扩展名,其内容如下:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json app.js ./
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

NOTE: use double quotes in the CMD line. Single quotes will result in an error.

注意:在CMD行中使用双引号 。 单引号将导致错误。

In the first line we say which image we want to start from. This will be our base image. In this case it will take the official Node.js image, based on Alpine Linux, using Node 14. When creating a container from the Dockerfile, Docker will get that image from Docker Hub.

在第一行中,我们说出要从哪个图像开始。 这将是我们的基本形象。 在这种情况下,它将使用Node 14获取基于Alpine Linux的官方Node.js映像。从Dockerfile创建容器时,Docker将从Docker Hub获取该映像。

Next we set the working directory to /usr/src/app, which means all our commands will be run in that folder until we change it again. That’s a folder we know already exists in the Node image.

接下来,将工作目录设置为/usr/src/app ,这意味着我们所有的命令都将在该文件夹中运行,直到再次更改它为止。 我们知道该文件夹已经存在于Node映像中。

We copy the package.json, package-lock.json (using the * wildcard) and app.js files that are present in the current folder, into the working directory.

我们将当前文件夹中存在的package.jsonpackage-lock.json (使用*通配符)和app.js文件复制到工作目录中。

We run npm install to install the packages listed in the package.json file.

我们运行npm install来安装package.json文件中列出的软件包。

Then we expose port 3000 to the outside, since that’s what our app runs on. A container is 100% isolated from the network unless you expose one of its ports using the EXPOSE command. We’ll later see how we can map ports on our computer to ports in a Docker container.

然后,将端口3000暴露在外部,因为这就是我们的应用程序运行的方式。 除非您使用EXPOSE命令公开容器的其中一个端口,否则容器与网络100%隔离。 稍后我们将看到如何将计算机上的端口映射到Docker容器中的端口。

Finally we run node app.js to start the app.

最后,我们运行node app.js来启动应用程序。

This is a Dockerfile, and we’ll soon see how to actually create a container from it.

这是一个Dockerfile,我们将很快看到如何从中实际创建一个容器。

翻译自: https://flaviocopes.com/docker-dockerfiles/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值