Docker Community Forums

Share and learn in the Docker community.

Best practices for getting code into a container (git clone vs. copy vs. data container)

I’m still new to Docker and learning it.

What if we don’t need to change anything in server configuration or anything related to docker,
but we only have changed the source code.

Should we still create new docker image with source and deploy that image
or just deploy new source code tag which contains source code changes only?

If we deploy docker image every time, doesn’t it increase size of docker container on production?

1 Like

So does that mean your code lives in a docker hub as an image definition?

Hello @dgdosen ,

If you’re currently using docker hub than yes, that is the case. Please do remember that docker hub offers only one private repository for free. If you have multiple private projects, you’ll have to pay for docker hub. There are also alternatives to docker hub. None of which i’ve tried though.

Hey @kulinchoksi

Containers are ephemeral. Meaning that the lifetime of a container is short. You should be able to just destroy a container, and run a new one that will behave exactly the same easily with minimal configuration.

I think you should read up on how docker uses “copy on write” to save space. In a nutshell it means that if you run multiple containers from the same image they all share the same files. Files are only copied if a change is made to a file. So running 100 containers or 1 doesn’t increase space used on your file system. It’s a really interesting concept.

Hey @zmijewski and @winmintun,

I also use docker containers as jenkins slaves for building and testing code.

I’m curious though about what you mean when you say that for a web application you have a separate nginx and web application container? You mean that you use NGINX as a reverse proxy to your web application container? Because both should run a web server making the NGINX container optional depending on requirements.

Some clarification would be nice :slight_smile:

Use data volumes.
https://docs.docker.com/engine/tutorials/dockervolumes/

What if we want go pull code from a private repo and build this code at go build time .
To goal this,i have do some tries.
1.Do RUN ADD /root/.ssh/id_rsa /root/.ssh/id_rsa
2.Use shell script in Docker file,the script do-ssh.sh is:

eval "$(ssh-agent)" && ssh-agent -s
chmod 0600 /root/.ssh/id_rsa
ssh-add /root/.ssh/id_rsa
git clone git@103.6.128.106:fw/product.git -b new

then, RUN do-ssh.sh
id_rsa` was add into agent,but it still do not work

I have aready read this issue:https://github.com/docker/docker/issues/6396
It seem that there have’t any way to do pull from a private repo successfully when docker build
Must i compile my code at docker run time?

This is such a great question that I wrote I little post about it. In short I download the repo via wget and copy the data in the Docker image.

Here are the detail about how I do it: http://pascalandy.com/blog/best-practices-for-getting-code-into-a-container-git-clone-vs-copy-vs-data-container/

Cheers!
Pascal | Twitter

@devmtl:
May I ask why you post a dead link?

Thanks for having let me know! Few weeks ago, I updated my blog URL from blog.pascalandy.com/ to pascalandy.com/blog/

I update the link above :slight_smile:

Cheers!

devmtl,

I’m looking at your code at firepress.org…

I’m more than confused by what I’m looking at. I don’t recognize any of that code. What language is that? It doesn’t appear to be linux shell / bash commands… Is that a docker-compose file? What type of file is that, and how do you run it? Is this Java? I’m way confused…

The system talks about “Part two”. I’m sort of old fashioned. When I see part two, I immediately think there must be a part one somewhere. What is Part one, where is it, and why wasn’t it included?

No offense intended, but I’m totally lost. This may be an awesome technique, but without appropriate context the example is more than confusing.

Many thanks for any clarification you can offer,
Zip.

You are looking at a basic Dockerfile :slight_smile:

I used a lot of echo to debug stuff so yes, it’s cleaner in my prod Dockerifle.

Part II in this case means, to add another theme, add the instruction there. Hope it helps :slight_smile:

Hi, Thanks for sharing the thoughts.
In my case, I have a container running my application. The application consumes configuration file. The configuration file must be generic and may be updated based on the requirements. So, we have the configuration in the git repository and any modifications to that is being pushed to code / git repository. My goal is to make the container consume whatever the latest configuration available in the git (may be after a restart of container). 'm thinking to create a Dockerfile with the following contents

FROM ubuntu
ADD .ssh /root/.ssh // For the private and public key
RUN mkdir /usr/… // For copying the known_hosts file
ADD known_hosts /usr/… // Copy the known_hosts file.
RUN mkdir /opt/myservice/code_to_be_synced
RUN cd /opt/myservice/code_to_be_synced
RUN git clone username@gitrepo // This is to clone the code base
ADD entry.sh /etc/
ENTRYPOINT /etc/entry.sh

entry.sh will have the lines of code to pull the latest changes from git repository (already cloned) and replace the existing configuration file with the latest available in Git repository.

Let me know if this is the suggested way.

Thanks,
Dinesh

Hi @dineshbhat
the entrypoint method seems to be a very simple approach. May I know if you were able to get it working?
According to this [1], it should be possible

[1] https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact
Best

Hi

I need basic thing how can i clone public git repo into docker container, but still not found any helpful information. Seriously very poor documentation from docker.

This is not remotely what docker themselves should handle, non the less, install git and do git clone either inside the container, or, as more prefered you run git clone outside the container, and use the copy method provided with docker to copy the project inside the container.

Creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. Nothing in your project is tracked yet. (See Git Internals) for more information about exactly what files are contained in the .git directory you just created.)

If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit.

Cloning an Existing Repository

Clone a repository with git clone <url> . Example, if you want to clone the Git linkable library called libgit2 , Hope it helps. Go2top FIFA 18.

The focus should be to use container as one thing only and not complicate stuff. Containers are best left performing microservices operations. The ideal way would be to employ a continuous deployment pipeline through jenkins or any CI/CD. The CI/CD system creates a new image every time a code is pushed and deploys the container to your dev environment. I understand that setting up such pipeline can be time consuming, so I guess the best way is to mount the project directory to container where tomcat can see it.

Try using data volumes.It should help dqfansurvey