# USER

```
USER <user>[:<group>]
```

或

```
USER <UID>[:<GID>]
```

当运行一个镜像时，**USER指令**可以用来设置一个**用户名（UID）**&#x548C;可选的**用户组（或GID**）。任何跟随在该指令之后的**RUN，CMD**和**ENTRYPOINT指令**都会按照设置的**用户**和**用户组权限**运行。

**NOTE:**

当为用户指定一个组时，用户将只有指定组的成员关系。已经配置的任何其他组成员关系都将被忽略。

**WARNING:**

当用户没有一个主组时，那么镜像（或者下一条指令）将通过root组身份运行。

在**windows**上，如果不存在一个内建的账户，那么必须先创建一个用户。这可以通过作为**Dockerfile**的一部分被调用的**net user**命令来完成。

```
FROM microsoft/windowsservercore
# Create Windows user in the container
RUN net user /add patrick
# Set it for subsequent commands
USER patrick
```

demo:

```
FROM centos:7
RUN useradd -u 9000 Yang; groupadd -g 9100 ChangZhou #创建用户和用户组，否则系统中不存在该信息
USER 9000:9100
RUN mkdir /tmp/my_dir && cd /tmp/my_dir/ && touch file
CMD ["/bin/echo", "process finished"]
```

检查文件信息：

```bash
[Yang@b21fa1fb48f9 tmp]$ ll
total 8
-rwx------ 1 root root       836 Nov 13 01:55 ks-script-DrRL8A
drwxr-xr-x 2 Yang ChangZhou 4096 Mar  7 03:45 my_dir
-rw------- 1 root root         0 Nov 13 01:53 yum.log
[Yang@b21fa1fb48f9 tmp]$
```

上面的结果显示，目录根据我们设置的**用户名**和**组名**被创建了。在**Dockerfile**中引用**用户**和**用户组**时，记得得在基础镜像的系统中先创建该**用户**和**用户组**。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://camelgemonion.gitbook.io/docker/dockerfile-zhi-ling/user.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
