How to "docker-compose build" a service from code located in local git repository?
Solution 1
What you are specifically after (building from a bare repository on a local filesystem) isn't functionality offered by Docker, and by extension, docker-compose. Docker supports building from a few different URLs, but not bare repositories on local filesystems. You can try a few workarounds:
- Build straight from Github:
docker build https://github.com/docker/rootfs.git
- Run a Git daemon to use the
git://
protocol:git daemon --base-path=. --export-all
(in/home/nou/code
) - Running a Git daemon from within Docker, mounting your bare repository as a volume and building inside it via a mounted Docker socket
- Converting your bare repository to a normal one
https://docs.docker.com/engine/reference/commandline/build/#build-with-url
Solution 2
You can try this, it works for me:
-
Create a user named "git"
$ sudo adduser git $ su git $ cd ~ $ mkdir .ssh && chmod 700 .ssh $ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
-
If you don't have ssh keys for "nou" user then, from "nou" user run
$ ssh-keygen
and accept all defaults.
-
Add "nou" user's ssh public key to git user ~/.ssh/authorized_keys
$ sudo cat nou_user_home/.ssh/id_rsa.pub >> git_user_home/.ssh/authorized_keys
After this try below command from "nou" user:
docker build git@localhost:/home/nou/code/lib.git
If Dockerfile
is not at the root of cloned repository then:
docker build -f folder_containing_dockerfile/Dockerfile
git@localhost:/home/nou/code/lib.git
If this all works fine then you can try with your docker-compose
file.
Related videos on Youtube
luac
Updated on September 18, 2022Comments
-
luac over 1 year
I have a git repository placed in local file system at
/home/nou/code/lib.git
Also I have/home/nou/docker-compose.yaml
that is used to deploy a service based on the code from the repo:version: '2' services: lib: container_name: lib build: git://./code/lib.git/ #build: git://./code/lib.git #build: git:///home/nou/code/lib.git/ #build: git://file:///home/nou/code/lib.git/
As you can see, am trying to build lib service from the local git repo, but the only result I observe is such error (or similar):
nou@ubuntu:~/$ docker-compose build Building lib ERROR: error fetching: fatal: unable to connect to .: .: No address associated with hostname : exit status 128
How can I use local git repo to build services using docker-compose?
-
luac over 6 yearsAwesome answer, thank you. Git daemon works for me.(My current workaround is to clone into temporary directory and remove it after docker-compose goes down)
-
luac over 6 yearsSad to say, but docker can't clone daemon-hosted
git://localhost/lib.git
due to dumb gitbad pack header
error. At the same time I can clone it usinggit clone
... -
user2640621 over 6 yearsI've updated the command for
git daemon
to properly export your repository, you should be able todocker build git://localhost/lib.git
after starting the daemon with the updated command.