Docker将一个variables传递给运行git clone的shell脚本
我正在创build一个Dockerfile,作为这个文件的一部分,我想让最终用户可以select通过他们的git仓库。
我看了这个SOpost ,还有一些其他的网站,但是我坚持使用git clone
语法。
git clone命令返回一个错误,因为作为其一部分的variables在docker build
阶段不包含值。
我有这个名为gitclone.sh的bash脚本
#!/bin/bash git clone $1 /app
并在我的Dockerfile我有这一行:
RUN ./gitclone.sh $LOCATION
shell脚本具有正确的权限来运行,并且在从容器内运行它时起作用,因为我将一个实际的存储库传递给它。
在Docker构build阶段,我得到以下错误:
致命的:仓库'/应用程序'不存在
该目录在那里,但$1
是空的命令不起作用。
问题我如何使Dockerfile工作? 我想要的所需输出:
docker run -e LOCATION=https://github.com/something.git -d jwknz/pg02
更新更改我的Dockerfile以包含CMD ["../gitclone.sh","$LOCATION"]
而不是RUN ./gitclone.sh $LOCATION
不解决它。
Dockerfile将生成,但容器在运行docker docker run
命令后停止。
首先,您必须注意Dockerfile
用于构build您的图像(使用Dockerfile
docker build
命令),并使用Dockerfile
docker run
从已经构build的docker镜像启动容器。
您必须定义入口点脚本。 这是一个脚本,每当从Dockerfile
构build的映像启动容器时执行该脚本。
假设你想克隆一个git repo,然后运行apache httpd,你可以在Dockerfile
所在的文件夹中创build一个像这样的脚本。 命名它,例如gitCloneAndRunHttpd.sh
#!/bin/bash git clone $LOCATION /app /usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf
然后你必须修改你的Dockerfile
告诉Dockerfile
把脚本文件复制到你的Dockerfile
镜像,使其可执行,告诉它在容器创build时运行它:
[...] COPY ["gitCloneAndRunHttpd.sh", "/gitCloneAndRunHttpd.sh"] RUN chmod +x /gitCloneAndRunHttpd.sh ENTRYPOINT ["/gitCloneAndRunHttpd.sh"]
在此之后,您使用docker build
命令构buildDocker镜像,然后从图像开始创build容器。
每次你用docker run -e LOCATION=https://github.com/some-git-repo.url -d your/image
一个容器时, docker run -e LOCATION=https://github.com/some-git-repo.url -d your/image
,repo https://github.com/some-git- repo.url将从容器内克隆到/ app文件夹,并且Apache httpd将被启动。
注意:如果您想知道在您提到的SO文章中发生了什么,那么使用默认入口点(/ bin / sh),而/file.sh abc
是传递给该入口点的参数,因此file.sh
脚本被执行。