4

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?

  • git protocol wait a hostname, if you want to use local path, use the file protocol, your 4th attempt is nearly correct, just drop the git:// before file://. In brief: build: file:///home/nou/code/lib.git/ should do – Tensibai Oct 3 '17 at 7:46
  • @Tensibai but with that suggestion how does composer know it should be using git? :) – Dan Cornilescu Oct 3 '17 at 13:35
  • @DanCornilescu well it's an uri and not a path... Anyway if it is a repo, that a valid path for build context also. If there's another need, it should be stated – Tensibai Oct 3 '17 at 14:21
  • I believe git:// is essential in this config line for the repo to be pulled (using git) before the build is actually executed. In other words - not a plain URI, but a git URI. – Dan Cornilescu Oct 3 '17 at 15:43
  • @DanCornilescu can you extend on the need to pull a repo already here? There's something bothering me in the wish to pull into local something already there... – Tensibai Oct 3 '17 at 21:20
5

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

  • Awesome 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) – FLCL Oct 3 '17 at 14:08
  • Sad to say, but docker can't clone daemon-hosted git://localhost/lib.git due to dumb git bad pack header error. At the same time I can clone it using git clone... – FLCL Oct 3 '17 at 20:59
  • I've updated the command for git daemon to properly export your repository, you should be able to docker build git://localhost/lib.git after starting the daemon with the updated command. – user2640621 Oct 3 '17 at 22:28
3

You can try this, it works for me:

  1. 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
    
  2. If you don't have ssh keys for "nou" user then, from "nou" user run

    $ ssh-keygen
    

    and accept all defaults.

  3. 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.

0

The issue is caused as there is a dot instead of a hostname, i.e. build: git://./code/lib.git/

  • What hostname can be used to address local git repository? – FLCL Oct 2 '17 at 19:40
  • did you try localhost? What git repository do you use? – 030 Oct 2 '17 at 19:45
  • localhost doesn't help too, cos I don't share my repository via http. It's just a repo hosted locally at /home/nou/code/lib.git, like if you do git init --bare lib.git – FLCL Oct 2 '17 at 19:52
  • stackoverflow.com/a/4694142/2777965 – 030 Oct 2 '17 at 20:00
  • Oh man I know how to push, I just wanna make docker pull'n'build – FLCL Oct 3 '17 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.