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?

FLCL
  • 143
  • 1
  • 5
  • 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 03 '17 at 07:46
  • @Tensibai but with that suggestion how does composer know it should be using git? :) – Dan Cornilescu Oct 03 '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 03 '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 03 '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 03 '17 at 21:20
  • My understanding is that the intention is to pull that git repo... If it's not the case - pls disregard my comments. – Dan Cornilescu Oct 03 '17 at 23:19

2 Answers2

6

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

user2640621
  • 1,395
  • 8
  • 20
  • 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 03 '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 03 '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 03 '17 at 22:28
4

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.

MNA
  • 141
  • 2