ENTRYPOINT与CMD/实现切换用户执行
1、CMD
最终会被解析成:["cmd","arg1","arg2"]
可以在运行时被覆盖
2、ENTRYPOINT 最终解析成 ["cmd","arg1","arg2"]
联合CMD使用时,CMD后面的内容将作为参数传递给ENTRYPOINT后面的命令(可以在内部访问)
运行覆盖需要-entrypoint
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
脚本。
#!/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 "$@"
T