Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloning a git repo in Dockerfile and working off it

Am trying to build a setup where my Dockerfile has instructions to clone a couple of git repos (amongst other stuff). After the first run, the cloned git repos should be made available to the host machine for editing. All further edits to the local cloned git repos should be made available for future docker builds.

So, how do I expose the git repos that were cloned in the Dockerfile for editing on the host machine.

like image 636
Samar Avatar asked Mar 30 '18 22:03

Samar


People also ask

How do I run a project from GitHub after cloning?

Open a console terminal, execute the init command and select dev as environment. Show activity on this post. After Clone Checkout Branches from repository in which your running code present then pull those branches and run code again.

How do I clone a repository in Docker desktop?

Using SSH keys inside docker container (the SSH keys are passed at build time − we want run time) Clone private git repo with dockerfile (same problem) Inject host's SSH keys into Docker Machine with Docker Compose (running as root )


1 Answers

You can do it by three ways.

Here is the Dockerfile.

FROM node:alpine
RUN apk add --no-cache git
RUN apk add --no-cache openssh
WORKDIR /data
RUN git clone https://github.com/jahio/hello-world-node-express.git /data/app
WORKDIR /data/app
EXPOSE 3000

Build:

docker build -t node-test .

Update:

Damn, I am crazy about docker :D Another solution easiest and good

Create an empty directory in host and container and mount that one

/home/adiii/Desktop/container_Data:/to_host

Copy the cloned repo to to_host at the entry point with -u flat so only will new file will be paste and host data will be persistent.

and entrypoint.sh

#!/bin/ash
cp -r -u /data/app /to_host && /bin/ash

dockerfile update section.

ADD entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh
RUN WORKDIR /
RUN mkdir -p to_host

# so we will put the code in to_host after container bootup boom im crazy about docker so no need to make it complex..simple easy :D

ENTRYPOINT [ "/usr/bin/entrypoint.sh" ]

1: using docker volume

Create volume named code

docker volume create code

Now run that container with mounting this volume.

docker run -p 3000:3000 -v myvol:/data/app --rm -it node-test ash

Now terminate the container or stopping it will data still preserved in volume.

You can find if OS is Linux.

/var/lib/docker/volumes/code/_data

you will see three

app.js  node_modules  package.json

2: using bash see comments in the script

  #!/bin/bash
image_name=node-test
container_name=git_code


# for first time use first_time
if [ $1 == "first_time" ] ; then
# remove if exist
docker rm -f $container_name
#run contianer for first time to copy code
docker run --name $container_name -dit $image_name ash
fi
# check if running
if docker inspect -f '{{.State.Running}}' $container_name ; then
# copy code from container to /home/adiii/desktop
docker cp $container_name:/data/app /home/adil/Desktop/app
fi

# for normal runing using run 
if [ $1 == "run" ]; then
# remove old container if running
docker rm -f $container_name
docker run --name $container_name -v /home/adil/Desktop/app:/data/app -dit $image_name
fi

Now run the command in the container

docker exec -it git_code ash

enter image description here

3: By mounting the empty directory of the host with code directory of the container at the runtime. So when you run next time with mount directory it will contain your update code which you made any change from host OS. But make sure the permission of that directory after container run and terminated data will be there but the behaviour of this method is not constant.

docker run -p 3000:3000 -v /home/adiii/code/app:/data/app --rm -it node-test ash

Here /home/adiii/code/app is an empty directory of host and after termination of containers its still have cloned code but I said its behavior varies.

like image 182
Adiii Avatar answered Oct 02 '22 17:10

Adiii