Docker to Git
There are a few different strategies for combining Docker and Git:
1. Dockerfile in Git repository
The simplest approach is to store the Dockerfile directly in the Git repository, along with the rest of the application code:
ProjectFolder/
.git/
src/
Dockerfile
pom.xml
Then in the Dockerfile, you can copy the entire project directory into the container using:
ADD . /project/
Or you can specify a particular build output to copy:
ADD /output/project.jar /project/
The advantage is that everything is in one place and you can test changes locally before committing. You'll need a .dockerignore
file to exclude large files from the Docker build.
2. Clone Git repository in Dockerfile
Another option is to clone the Git repository during the Docker build process:
RUN git clone [email protected]:eugenp/tutorials.git
This fetches the whole repository into the container.
However, there are downsides:
You'll need to store your Git credentials in the Docker image
The
RUN
command will be cached, so changes won't be picked upYou have to install Git in the container
3. Volume mapping
You can mount the Git repository directory as a volume into the container:
VOLUME /build/ /project/
Mapping /build/
on the host to /project/
in the container.
This keeps the repository outside the container, reducing its size. But the container has write access to the volume, so care is needed.
4. Git submodules
If the Dockerfile and code are in separate repositories, use Git submodules to include the code repository as a submodule of the Dockerfile repository.
You can then copy the submodule directory into the container. Submodules must be updated manually.
In summary, storing the Dockerfile in the same Git repository as the code is the simplest approach, while volume mapping offers the cleanest separation. Using Git submodules can work for multi-repo setups.