docker_practice/image/dockerfile/user.md

27 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### USER
`USER <用户名>[:<用户组>]`
`USER` `WORKDIR` `WORKDIR` `USER` `RUN`, `CMD` `ENTRYPOINT`
`WORKDIR` `USER`
```Dockerfile
RUN groupadd -r redis && useradd -r -g redis redis
USER redis
RUN [ "redis-server" ]
```
`root` 使 `su` `sudo` TTY 使 [`gosu`](https://github.com/tianon/gosu)。
```Dockerfile
# 建立 redis 用户,并使用 gosu 换另一个用户执行命令
RUN groupadd -r redis && useradd -r -g redis redis
# 下载 gosu
RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.7/gosu-amd64" \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true
# 设置 CMD并以另外的用户执行
CMD [ "exec", "gosu", "redis", "redis-server" ]
```