简体   繁体   English

Docker将变量传递到运行git clone的shell脚本中

[英]Docker passing in a variable into an shell script running git clone

I am creating a Dockerfile and as part of this file, I am wanting to give the end user the option to pass in their git repo. 我正在创建一个Dockerfile,并且作为该文件的一部分,我希望为最终用户提供传递其git repo的选项。

I looked at this SO post , and a few other sites but am stuck on the git clone syntax. 我看了这篇SO帖子以及其他一些站点,但仍停留在git clone语法上。

The git clone command returns an error as the variable that is part of it does not contain a value at the docker build stage. git clone命令返回错误,因为该变量的一部分在docker build阶段不包含任何值。

I have this bash script called gitclone.sh 我有一个名为gitclone.sh的bash脚本

#!/bin/bash
git clone $1 /app

and in my Dockerfile I have this line: 在我的Dockerfile中我有这行:

RUN ./gitclone.sh $LOCATION

The shell script has the right permissions to run and it works when running it from within the container since I am passing an actual repository to it. shell脚本具有正确的运行权限,并且在从容器中运行它时可以运行,因为我正在向其传递实际的存储库。

During the Docker build phase I get the following error: 在Docker构建阶段,我收到以下错误:

fatal: repository '/app' does not exist 致命:存储库“ / app”不存在

The directory is there, but the $1 is empty to the command doesn't work. 目录在那里,但是命令的$1为空不起作用。

Question How do I make the Dockerfile work? 问题如何使Dockerfile工作? The desired output I want it: 我想要的期望输出:

docker run -e LOCATION=https://github.com/something.git -d jwknz/pg02

UPDATE Changing my Dockerfile to contain CMD ["../gitclone.sh","$LOCATION"] instead of RUN ./gitclone.sh $LOCATION doesn't fix it. 更新将我的Dockerfile更改为包含CMD ["../gitclone.sh","$LOCATION"]而不是RUN ./gitclone.sh $LOCATION无法修复它。

The Dockerfile will build, but the container stops after running the docker run command. Dockerfile将生成,但是容器在运行docker docker run命令后停止。

First off, you have to notice that the Dockerfile is used to build your image (with the docker build command) and docker run is used to start a container from the already build docker image. 首先,您必须注意到Dockerfile用于构建映像(使用Dockerfile docker build命令),而docker run用于从已构建的Docker映像启动容器。

You have to define an entrypoint script. 您必须定义一个入口点脚本。 This is a script that is executed every time a container is started from the image you've build with the Dockerfile . 这是一个脚本,每次从您使用Dockerfile构建的映像启动容器时都会执行该脚本。

Assuming you want to clone a git repo and run apache httpd afterwards you can create a script like this in the same folder where your Dockerfile is. 假设您要克隆git repo并随后运行apache httpd,则可以在Dockerfile所在的同一文件夹中创建这样的脚本。 Name it, eg gitCloneAndRunHttpd.sh 为其命名,例如gitCloneAndRunHttpd.sh

#!/bin/bash 

git clone $LOCATION /app 
/usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf 

Then you have to modify your Dockerfile telling docker to copy the script file to your docker image, make it executable, en tell it to run it at container creation: 然后,您必须修改Dockerfile告诉Dockerfile将脚本文件复制到Dockerfile映像,使其可执行,并告诉它在容器创建时运行它:

[...]

COPY ["gitCloneAndRunHttpd.sh", "/gitCloneAndRunHttpd.sh"]
RUN chmod +x /gitCloneAndRunHttpd.sh
ENTRYPOINT ["/gitCloneAndRunHttpd.sh"]

After this you build your docker image with the docker build command and then you can start creating containers from your image. 之后,您可以使用docker build命令构建docker映像,然后可以开始从映像创建容器。

Every time you start a container from it with docker run -e LOCATION=https://github.com/some-git-repo.url -d your/image , the repo https://github.com/some-git-repo.url will be cloned from within the container to the /app folder and apache httpd will be started. 每次您使用docker run -e LOCATION=https://github.com/some-git-repo.url -d your/image从其启动容器时,存储库https://github.com/some-git- repo.url将从容器内克隆到/ app文件夹,并启动apache httpd。

Note: If you are wondering what is going on in the SO post you mentioned, there the default entrypoint is used (/bin/sh), and /file.sh abc is the argument passed to that entrypoint , therefore the file.sh script is executed. 注意:如果您想知道所提到的SO帖子中发生了什么,请使用默认入口点(/ bin / sh),并且/file.sh abc是传递给该入口点的参数,因此是file.sh脚本被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM