迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

将用户添加到 Docker 容器

作者:迹忆客 最近更新:2023/04/17 浏览次数:

在开发应用程序时,我们通常有不同的用户与系统交互以执行不同的功能。 这些用户具有使他们能够根据其角色执行功能的权限。

请注意 ,有些超级用户可以执行系统中的所有功能,因为他们已被分配了系统的所有角色。 同样,我们可以为Docker容器创建用户,并为用户设置权限,保证他们只能在容器中执行特定的任务。

这些增强了容器的安全性,因为我们可以将对容器关键部分的访问限制为超级用户,并且还可以跟踪在特定事件发生时哪个用户正在与系统交互。

在本文中,我们将学习如何通过实现返回产品数组的 Express 应用程序将用户添加到 Docker 容器。


创建一个快速应用程序

打开 WebStorm IDEA 并选择文件 > 新建 > 项目。 选择 Node.js 并将位置部分的项目名称从无标题更改为产品 API。

您应该已经安装了节点运行时环境,以便可以从计算机自动添加节点解释器和包管理器部分。 按标有“创建”的按钮生成项目。

要安装 Express,请在 WebStorm 上使用键盘快捷键 Alt+F12 打开一个新的终端窗口,然后使用以下 npm 命令安装依赖项。

~/WebstormProjects/product-api$ npm install express

在当前文件夹中创建一个名为 index.js 的文件,并将以下代码复制并粘贴到该文件中。

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.json(
        [
            {
                name: 'Iphone 13 Pro',
                price: 1500.67
            },
            {
                name: 'Samsung Galaxy A53',
                price: 1300.56
            },
        ])
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

在此文件中,我们创建了一个 Express 应用程序,当我们向浏览器/在浏览器上发出 GET 请求时,它会返回一组产品。 请注意,此应用程序公开了端口 3000,这有助于我们为该应用程序提供服务。


创建应用程序的 Docker 映像

在 product-api 文件夹中创建一个名为 Dockerfile 的文件,并将以下代码复制并粘贴到该文件中。

FROM node:latest
RUN groupadd admin &&\
    useradd mary -g admin -s /bin/sh
WORKDIR /com/product
ADD package*.json ./
RUN npm install
ADD . .
USER mary
CMD node index.js

在这个文件中,我们使用 node:latest 作为基础图像创建了我们应用程序的图像。 FROM 指令之后的 RUN 指令创建一个名为 admin 的组,并向该组添加一个名为 mary 的用户。

我们使用反斜杠 \ 将命令继续到下一行。 注意,创建组和用户的命令都是Linux命令,包括 groupadduseradd

此文件中的另一条重要指令是 USER 指令,它设置将用于剩余阶段的默认用户。

我们也可以设置默认组,尽管这是可选的。 Docker 文档详细提供了其余指令的用法。


构建镜像

要构建镜像,请使用键盘快捷键 Alt+F12 打开一个新的终端窗口,然后使用以下命令创建一个名为 product-api 的图像。

~/WebstormProjects/product-api$ docker build --tag product-api:latest .

此命令从第一个文件开始依次执行名为 Dockerfile 的文件,如下面的终端窗口所示。 点。 命令末尾的 显示 Dockerfile 文件的位置,在本例中它位于当前文件夹中。

 => [1/6] FROM docker.io/library/node:latest@sha256:bb3b9f1867edba6d8892758889e43b2f0205ebbd381969a2c45577d38281  95.4s
 => => resolve docker.io/library/node:latest@sha256:bb3b9f1867edba6d8892758889e43b2f0205ebbd381969a2c45577d38281a  0.1s
 => [2/6] RUN groupadd admin &&    useradd mary -g admin -s /bin/sh                                                6.3s
 => [3/6] WORKDIR /com/product                                                                                     0.9s
 => [4/6] ADD package*.json ./                                                                                     0.7s
 => [5/6] RUN npm install                                                                                          7.1s
 => [6/6] ADD . .

运行容器

构建镜像后,使用以下命令从此镜像运行名为 product-service 的容器。

~/WebstormProjects/product-api$ docker run --name product-service -d -p 3030:3000 product-api:latest

此命令运行一个容器,该容器将端口 3000 映射到本地主机上的端口 3030,因为该容器正在本地使用。


验证用户是否已创建

要验证用户是否已创建,请使用以下命令,该命令允许我们使用 exec 指令从正在运行的容器中执行命令。

~/WebstormProjects/product-api$ docker exec -it product-service /bin/sh

由于我们将 WORKDIR 指定为 /com/product,因此命令在此目录中运行。 如果我们不指定目录,该命令将在默认目录中运行。

该命令执行交互式 Bash shell,我们可以使用 Bash 会话在运行的容器上执行命令。 在正在运行的容器上执行以下命令验证用户组,如下。

$ id mary

输出:

uid=1001(mary) gid=1001(admin) groups=1001(admin)

我们已经看到容器有一个名为 mary 的用户,来自名为 admin 的组,允许我们根据需要更改权限。


从容器中获取数据

打开任何浏览器并向 localhost:3030 (http://localhost:3030/) 发出请求以验证我们的应用程序是否按预期工作。 确保请求返回我们在 index.js 文件中创建的产品 API,如下所示。

[
    {
    "name": "Iphone 13 Pro",
    "price": 1500.67
    },
    {
    "name": "Samsung Galaxy A53",
    "price": 1300.56
    }
]

总结

在本文中,我们学习了如何使用 Dockerfile 将用户添加到容器中。 我们还了解了如何将用户添加到组中,因为用户属于组织中的特定组。

请注意 ,有不同的方法可以实现相同的结果,因此请随意使用满足要求的任何方法。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

发布时间:2025/03/26 浏览次数:202 分类:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便