docker_practice/image/dockerfile/entrypoint.md

125 lines
5.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.

# 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/*
CMD [ "curl", "-s", "http://myip.ipip.net" ]
```
使 `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".
```
`executable file not found` `command` `CMD` `-i` `CMD` `curl -s http://myip.ipip.net` `-i`
`-i`
```bash
$ docker run myip curl -s http://myip.ipip.net -i
```
使 `ENTRYPOINT` `ENTRYPOINT`
```docker
FROM ubuntu:18.04
RUN apt-get update \
&& apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
ENTRYPOINT [ "curl", "-s", "http://myip.ipip.net" ]
```
使 `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
find . \! -user redis -exec chown redis '{}' +
exec gosu redis "$0" "$@"
fi
exec "$@"
```
`CMD` `redis-server` `redis` 使 `root`
```bash
$ docker run -it redis id
uid=0(root) gid=0(root) groups=0(root)
```