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

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.