介绍
在本文中,我们将使用 Visual Studio Code 的 Docker 插件。Docker 允许我们将我们的应用程序包装成图像,并在安装了 Docker 的任何平台上运行作为容器。
安装
有关如何安装和运行 docker 的说明是 在这里可用,它应该是具体的操作系统你正在运行。
您还需要安装 Visual Studio Code。
Once you have Visual Studio Code installed, open it click on the extensions section on the left most pane, and search for Docker.
Once installed, you should notice a few new things in your Visual Studio Code instance. On the left most pane, there's a new Docker section with the Docker logo, which when clicked opens the Docker Explorer with three sections. Images, Containers, Registries
There are also a few commands added to the command palette, which you can view by opening the command palette and typing in docker
.
Node.js 的
我们将使用 Node.js 应用程序来展示 Docker 插件向 VSCode 添加的功能。
创建一个快递服务器。
1mkdir docker-node
2cd docker-node
3npm init -y
4npm install --save express
5touch index.js
我们应该有一个这样的目录树:
1.
2├── index.js
3├── node_modules
4├── package-lock.json
5└── package.json
6
71 directory, 3 files
这是index.js的内容。
1[label index.js]
2const express = require('express')
3const app = express()
4
5app.listen(3000)
6
7app.get('/', (req, res) => {
8 res.send('hello world')
9})
更新「package.json」以建立起始脚本。
1[label package.json]
2"scripts": {
3 "start": "node index.js"
4 },
现在,我们可以简单地使用npm start
运行这个应用程序,然后去端口3000
并查看应用程序的工作。
传统上,要添加Docker,我们会遵循这些步骤。
- 创建 Dockerfile (或 docker-compose.yaml)
- 将 docker 指令添加到文件中(FROM, WORKDIR, ADD, EXPOSE, CMD)
- 在终端上运行
docker build...
以构建图像 - 在终端上运行
docker run...
以运行容器
With the plugin however, all we need to do is the following.
Open the command palette, and type in docker
, then select Docker: Add Docker files to Workspace
. It should be the first option. Press Enter
You will be asked to choose the platform/stack, select Node.js and press
Enter
.
You will then be asked to choose a port. Write
3000
since it's the port our app will listen to.
The following files are added to your workspace:
.dockerignore
, docker-compose.debug.yml
, docker-compose.yml
, and Dockerfile
.
.dockerignore 告诉 docker 在将文件添加到构建图像时,忽略列出的文件。
docker-compose.debug.yml
将允许您使用insect
运行docker-compose
,并附加调试器。
1version: '2.1'
2
3services:
4 docker-node:
5 image: docker-node
6 build: .
7 environment:
8 NODE_ENV: development
9 ports:
10 - 3000:3000
11 - 9229:9229
12 command: node --inspect=0.0.0.0:9229 index.js
然而,如果您在开发过程中进行调试,则可能需要附加一个卷,以便您对本地机器所做的更改在容器中继续存在。
「docker-compose.yml」文件是用于运行 docker 服务的标准 docker-compose 文件. 当您添加其他资源/服务,如数据库连接和负载平衡器时,您将编辑此文件。
1version: '2.1'
2
3services:
4 docker-node:
5 image: docker-node
6 build: .
7 environment:
8 NODE_ENV: production
9 ports:
10 - 3000:3000
这里最重要的Dockerfile
,因为它必须建造,包含我们如果没有安装插件,就必须手动编写的说明。
1FROM node:8.9-alpine
2ENV NODE_ENV production
3WORKDIR /usr/src/app
4COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
5RUN npm install --production --silent && mv node_modules ../
6COPY . .
7EXPOSE 3000
8CMD npm start
Next, to build the Image, open the VS Code command palette, and type in docker
then select Docker: Build Image
and press Enter
.
You'll be prompted to select the Dockerfile, choose it and press
Enter
.
Next you'll be prompted to select the tag. Leave the default
docker-node
selected and press Enter
.
The Integrated Terminal will open, and the build logs will show.
Finally, we need to run the container. Once again, open the command palette and type in docker run
, the select Docker: Run
A list of all containers in your system will show up, select
docker-node:latest
, the one we tagged, and press Enter
.
The terminal will show the logs for the run command.
Notice it added the
-p 3000:3000
exposing the port to our host machine so that we can run the application by visiting localhost:3000
.
We can also run the container by going to the left pane, selecting the Docker section, then under Images, choose the docker-node
image, right click and click on run.
The same logs will run on the terminal.
You'll also notice that the images section above has a list of the images in your system. Once the docker-node container is running, We can check the running containers in the same section, and even stop them.
Above,
Attach Shell
is selected, which is equivalent to the docker command below.
1docker exec -it <container> sh
这是下面的终端日志输出。
你可以看到我们在容器中,我们可以列出容器内部的文件。
停止容器,然后尝试使用 docker-compose 运行应用程序. 打开命令板,查找 docker-compose 并查看输出。
去吧
如果你不熟悉Golang,你可以跳到下一个主题。
杜克尔也是 与 Go 构建
让我们创建一个Go App。
1mkdir docker-go
2cd docker-go
3touch main.go
您的目录树将有一个文件。
1.
2└── main.go
3
40 directories, 1 file
以下是main.go
文件的内容。
1[label main.go]
2package main
3
4import (
5 "log"
6 "net/http"
7)
8
9func helloHandler(w http.ResponseWriter, r *http.Request) {
10 w.Write([]byte("Hello World"))
11}
12
13func main() {
14 http.HandleFunc("/", helloHandler)
15
16 if err := http.ListenAndServe(":9000", nil); err != nil {
17 log.Fatalln("ListenAndServer Error", err)
18 }
19}
您可以使用 App:
1go run main.go
但是,让我们使用VSCode Docker插件来构建图像并运行容器。
创建Dockerfile,打开命令板,键入Docker,然后选择Docker: Add Dockerfile to Workspace
。
You will be prompted to select a platform, choose Go
and press Enter
.
You'll then be prompted to select a port, write in port 9000
, since it's the port we chose on our app, and press Enter
.
The following 4 files will be created. .dockerignore
, docker-compose.debug.yml
, docker-compose.yml
, and Dockerfile
.
.dockerignore
文件告诉Docker在将文件添加到图像时忽略某些文件。
docker-compose.debug.yml
和 docker-compose.yml
被 Docker Compose 用来运行应用程序. 它们并不那么不同,因为调试文件需要额外的输入,因为调试 Go 更为复杂。
然而,这里的Dockerfile
是最有趣的部分,而构建阶段
的最后两行则被评论并添加了RUN go install -v./...
1# RUN go-wrapper download # "go get -d -v ./..."
2# RUN go-wrapper install # "go install -v ./..."
3RUN go install -v ./...
这里是最后的docker文件。
1#build stage
2FROM golang:alpine AS builder
3WORKDIR /go/src/app
4COPY . .
5RUN apk add --no-cache git
6# RUN go-wrapper download # "go get -d -v ./..."
7# RUN go-wrapper install # "go install -v ./..."
8RUN go install -v ./...
9
10#final stage
11FROM alpine:latest
12RUN apk --no-cache add ca-certificates
13COPY --from=builder /go/bin/app /app
14ENTRYPOINT ./app
15LABEL Name=docker-go Version=0.0.1
16EXPOSE 9000
这种类型的 Dockerfile 模式被称为 多阶段构建,其主要优势是对 Docker 图像的优化,主要用于编译语言,在大多数情况下不需要编译工具来运行编译的应用程序。
简而言之,我们使用Docker构建的一部分来编译应用程序,然后将编译的二进制复制到更轻的Docker图像,并从那里运行。
接下来我们需要构建图像。打开命令板,然后键入docker-build
,选择Docker: Build Image
,然后按Enter
。
您将被要求选择Dockerfile,留下默认选项并按Enter
。
Finally, you'll be ask to pick an image tag. Leave the default docker-go:latest
and press Enter
.
You'll see the build logs in the integrated terminal.
Lastly, we need to run the container. Open the command palette and type in docker run
. Select Docker: Run
and press Enter
.
You'll be prompted to select the image. Select
docker-go:latest
.
You'll see the logs in the Integrated Terminal.
Like before, you can also run the container by selecting the Docker section in the left pane, and under containers, select
docker-go
, right click on click Run.
You'll then see the same
docker run
logs.
Since our our running Docker container only had the binary, we can attach the shell, in the containers section.
We can the type in
ls
in the attached shell in the Integrated Terminal, and we'll see a binary file called app
, which corresponds to the Dockerfile.
其他特征
最后,我们将看看与VSCode Docker插件一起提供的其他有用的功能。
Docker 检查图像:这允许您检查构建的图像并查看 JSON 文件中的详细信息。
Select the image you want and open the context menu, and select inspect image.
A JSON file will be opened with the details.
Show container logs: This is also found in the context menu for running containers. We'll use the running Node.js container
The logs will be shown in the Integrated Terminal.
注册表:您可以登录到您的 Docker 注册表并查看您创建和推出的图像。
System Prune: This option allows you to run docker system prune
, which clears unused images your system. It's available via the button with the windows and a cross in the Docker explorer.
Intellisense: If you have to write the Docker files (Dockerfile, docker-compose.yml) yourself, you'll get useful intellisense when typing.
It will even give you available image tags. This is triggered by typing in the image name, then a full colon, and
CMD
+ Space
.
Dockerfile Linting: When you have an error in your Dockerfiles, a squiggly line will appear in VS Code and when you hover over it, you'll be shown what the error is.
The problems tab below VS Code will also show it.
结论
用于 VS 代码的 Docker 插件可以帮助您快速设置和创建您的 Dockerfiles,构建并运行它们,而无需自己键入许多命令。