docker_practice/image/dockerfile/entrypoint.md

125 lines
5.1 KiB
Go
Raw Normal View History

### ENTRYPOINT
`ENTRYPOINT` `RUN` `exec` `shell`
`ENTRYPOINT` `CMD` `ENTRYPOINT` `CMD` `docker run` `--entrypoint`
`ENTRYPOINT` `CMD` `CMD` `ENTRYPOINT`
```bash
<ENTRYPOINT> "<CMD>"
```
`CMD` `ENTRYPOINT` `<ENTRYPOINT> "<CMD>"`
#### 使
IP `CMD`
```docker
FROM ubuntu:18.04
RUN apt-get update \
&& apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
2018-12-10 18:20:10 +08:00
CMD [ "curl", "-s", "https://ip.cn" ]
```
使 `docker build -t myip .` IP
```bash
$ docker run myip
IP61.148.226.66
```
使 `CMD` `curl` HTTP `-i` `-i` `docker run myip`
```bash
$ docker run myip -i
docker: Error response from daemon: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec: \\\"-i\\\": executable file not found in $PATH\"\n".
```
2018-12-10 18:20:10 +08:00
`executable file not found` `command` `CMD` `-i` `CMD` `curl -s https://ip.cn` `-i`
`-i`
```bash
2018-12-10 18:20:10 +08:00
$ docker run myip curl -s https://ip.cn -i
```
使 `ENTRYPOINT` `ENTRYPOINT`
```docker
FROM ubuntu:18.04
RUN apt-get update \
&& apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
2018-12-10 18:20:10 +08:00
ENTRYPOINT [ "curl", "-s", "https://ip.cn" ]
```
使 `docker run myip -i`
```bash
$ docker run myip
IP61.148.226.66
$ docker run myip -i
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 22 Nov 2016 05:12:40 GMT
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding
X-Powered-By: PHP/5.6.24-1~dotdeb+7.1
X-Cache: MISS from cache-2
X-Cache-Lookup: MISS from cache-2:80
X-Cache: MISS from proxy-2_6
Transfer-Encoding: chunked
Via: 1.1 cache-2:80, 1.1 proxy-2_6:8006
Connection: keep-alive
IP61.148.226.66
```
`ENTRYPOINT` `CMD` `ENTRYPOINT` `-i` `CMD` `curl`
####
`mysql` mysql
使 `root` `root` 使 `root` 便
`CMD` `CMD` `ENTRYPOINT` `<CMD>` `redis`
```docker
FROM alpine:3.4
...
RUN addgroup -S redis && adduser -S -G redis redis
...
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 6379
CMD [ "redis-server" ]
```
redis redis `ENTRYPOINT` `docker-entrypoint.sh`
```bash
#!/bin/sh
...
# allow the container to be started with `--user`
if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then
chown -R redis .
exec su-exec redis "$0" "$@"
fi
exec "$@"
```
`CMD` `redis-server` `redis` 使 `root`
```bash
$ docker run -it redis id
uid=0(root) gid=0(root) groups=0(root)
```