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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With