如何在启动时在 Alpine Docker 容器上 运行 shell 脚本?

How to run a shell script on a Alpine Docker container on startup?

我想在 Alpine Docker 容器启动时执行 shell 脚本。

您的 docker 文件如下所示

FROM alpine:latest

##Do your stuff

#####

##Last line
CMD ["entrypoint.sh"]
##OR
ENTRYPOINT ["entrypoint.sh"]

您应该创建一个 shell 文件以复制到新容器中,然后将容器入口点设置为您创建的 shell 脚本,synthax 类似于

FROM <docker/alpine-image>
#.... somecode
USER root
COPY /localmachinelocation/entrypoint.sh /containerlocation/entrypoint.sh
RUN chmod 544 /var/www/webapp/entrypoint.sh
#.... somecode
CMD /containerlocation/entrypoint.sh

您可以指定 运行 脚本所需的用户。 Chmod 确保脚本将被脚本的所有者运行启用,您可以出于安全考虑更改它。

假设您的脚本在 /home/user/script.sh 中,您可以这样做

FROM <docker/alpine-image>
USER root
COPY /home/user/script.sh /root/entrypoint.sh
RUN chmod 544 /var/www/webapp/entrypoint.sh
CMD /root/entrypoint.sh