如何将Docker插件用于Visual Studio代码

介绍 (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.

VS Code with left panel open showing Docker plugin

安装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

The Docker Explorer panel

安装后,您应该在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 typed into the command palette showing the commands available.

在命令面板中还添加了一些命令,您可以通过打开命令面板并键入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的内容

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以具有启动脚本。

package.json
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,我们将遵循以下步骤。

  1. Create a Dockerfile (or docker-compose.yaml)

    创建一个Dockerfile(或docker-compose.yaml)
  2. Add docker instructions to the file (FROM, WORKDIR, ADD, EXPOSE, CMD)

    将docker指令添加到文件中(FROM,WORKDIR,ADD,EXPOSE,CMD)
  3. Run docker build... on the terminal to build the image

    在终端上运行docker build...以构建映像

  4. 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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值