介绍 (Introduction)
In this article we’ll use the Docker Plugin for Visual Studio Code. Docker allows us to package our applications into images and run them as containers on any platform that has Docker installed. We’ll use this plugin with two developer stacks: Node.js and Go.
在本文中,我们将使用适用于Visual Studio Code的Docker插件 。 Docker允许我们将应用程序打包成映像,然后在安装了Docker的任何平台上将它们作为容器运行。 我们将这个插件与两个开发人员堆栈一起使用:Node.js和Go。
安装 (Installation)
You need to have docker installed on your work station. Instructions on how to install and run docker are available here, and it should be specific to the particular operation system you are running.
您需要在工作站上安装docker。 此处提供了有关如何安装和运行docker的说明,并且应特定于您正在运行的特定操作系统。
You also need to have Visual Studio Code installed.
您还需要安装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.
安装Visual Studio Code后,将其打开,单击最左侧窗格的扩展部分,然后搜索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
安装后,您应该在Visual Studio Code实例中注意到一些新内容。 在最左侧的窗格中,有一个带有Docker徽标的新Docker部分,单击该按钮可打开包含三个部分的Docker Explorer。 图片 , 容器 , 注册表
There are also a few commands added to the command palette, which you can view by opening the command palette and typing in docker
在命令面板中还添加了一些命令,您可以通过打开命令面板并键入docker
来查看 。
Node.js (Node.js)
We’ll use a Node.js application to demonstrate the capabilities the Docker plugin adds to VSCode.
我们将使用Node.js应用程序来演示Docker插件添加到VSCode的功能。
Let’s create an Express server.
让我们创建一个Express服务器。
- mkdir docker-node mkdir docker节点
- cd docker-node cd docker节点
- npm init -y npm初始化-y
- npm install --save express npm install-保存快递
- touch index.js 触摸index.js
We should have a directory tree like this:
我们应该有一个像这样的目录树:
.
├── index.js
├── node_modules
├── package-lock.json
└── package.json
1 directory, 3 files
This is the content of index.js
这是index.js
的内容
const express = require('express')
const app = express()
app.listen(3000)
app.get('/', (req, res) => {
res.send('hello world')
})
Update package.json
to have a start script.
更新package.json
以具有启动脚本。
"scripts": {
"start": "node index.js"
},
Now, we can simply run this app with npm start
, and go to port 3000
and see the app working.
现在,我们可以简单地使用npm start
运行该应用npm start
,并转到端口3000
并查看该应用程序的运行情况。
Traditionally, to add Docker, we would follow these steps.
传统上,要添加Docker,我们将遵循以下步骤。
- Create a Dockerfile (or docker-compose.yaml) 创建一个Dockerfile(或docker-compose.yaml)
- Add docker instructions to the file (FROM, WORKDIR, ADD, EXPOSE, CMD) 将docker指令添加到文件中(FROM,WORKDIR,ADD,EXPOSE,CMD)
Run
docker build...
on the terminal to build the image在终端上运行
docker build...
以构建映像Run
docker run...
on the terminal to run the container在终端上运行
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