4

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.

I looked at this SO post, and a few other sites but am stuck on the git clone syntax.

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.

I have this bash script called gitclone.sh

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

and in my Dockerfile I have this line:

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.

During the Docker build phase I get the following error:

fatal: repository '/app' does not exist

The directory is there, but the $1 is empty to the command doesn't work.

Question How do I make the Dockerfile work? 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.

The Dockerfile will build, but the container stops after running the docker run command.

jwknz
  • 6,598
  • 16
  • 72
  • 115
  • I don't really understand what you're trying to do, the `Dockerfile` tells Docker how to build the image, and `docker run` starts a new container from the already build image. The `RUN` command is executed during the building of the image. Do you pretend to have the environment variable passed to the run command during the building of the image, or am I missing something? – Manuel Schmidt Jun 04 '17 at 11:12
  • What I want is for docker run to take a git URL and clone it at docker run, but want to pass it in using the -e parameter – jwknz Jun 04 '17 at 11:14
  • OK. Then, you don't want to clone the repo during the building of the image but during the start of a container from it. You can define an entrypoint.sh script that is executing when a container is started. I'll try to document this a bit in an answer. – Manuel Schmidt Jun 04 '17 at 11:17
  • `$1` is _shell_ syntax. Dockerfiles are completely different. – Thorbjørn Ravn Andersen Jun 04 '17 at 14:45

1 Answers1

2

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.

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.

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. Name it, e.g. 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:

[...]

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.

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.

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.

Manuel Schmidt
  • 2,178
  • 2
  • 15
  • 19
  • I have been having a play with it for a while and I keep getting an `exit` status after I run the command. `Exited (128) 2 seconds ago` and the container won't start after it – jwknz Jun 04 '17 at 20:38
  • 1
    Also how does the `ENTRYPOINT ` know what parameter to use? What if I have 2 shell scripts? – jwknz Jun 04 '17 at 20:41
  • Do you get an error building the image or starting a container? – Manuel Schmidt Jun 04 '17 at 20:45
  • The building works fine and running doesn't return an error, but the container exits when the script is completed – jwknz Jun 04 '17 at 20:47
  • A docker container always exists when the main process finishes. What should you container do after cloning the the repo? Does it clone the repo? – Manuel Schmidt Jun 04 '17 at 20:48
  • Yeah, but how do I get around that? Like how do I let a user import their own git repo at the docker run stage? Or is that impossible? – jwknz Jun 04 '17 at 20:49
  • The entrypoint scripts receives as parameters all what follow the docker image name in the run command (at the end of the command), but you can also use environment variables like you did with -e – Manuel Schmidt Jun 04 '17 at 20:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145823/discussion-between-manuel-schmidt-and-jeff-kranenburg). – Manuel Schmidt Jun 04 '17 at 20:52