Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COPY only Git tracked files in Dockerfile

Tags:

git

docker

I wonder how to achieve the following in Dockerfile (not using .dockerignore):

COPY `git ls-tree -r HEAD --name-only` ./

That is, copy only files that are tracked in the Git repository. Of course, the above does not work because Docker does not execute the shell command in this way, but maybe something similar is possible?

This is a fairly common problem. For example, in the AWS Elastic Beanstalk platform, it is resolved using git archive -v -o myapp.zip --format=zip HEAD (https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/applications-sourcebundle.html). Similarly, Heroku builds its "slugs" based on Git push (again, only tracked in Git files).

Just to sum up, I'd like to package the application as a Docker image and I don't want to include all untracked and Git-ignored files. I'd like to avoid having to duplicate the .gitignore file to a .dockerignore file.

like image 395
januszm Avatar asked Aug 05 '18 09:08

januszm


1 Answers

You could archive the app using git archive as you suggested:

git archive -v -o myapp.tar.gz --format=tar.gz HEAD

And then just build your docker image from the archive file:

docker import myapp.tar.gz myapp:latest
like image 137
moebius Avatar answered Sep 20 '22 00:09

moebius