<p>You can try this, it works for me:</p>

<ol>
<li><p>Create a user named "git"</p>

<pre><code>$ sudo adduser git
$ su git
$ cd ~
$ mkdir .ssh &amp;&amp; chmod 700 .ssh
$ touch .ssh/authorized_keys &amp;&amp; chmod 600 .ssh/authorized_keys
</code></pre></li>
<li><p>If you don't have ssh keys for "nou" user then, from "nou" user run</p>

<pre><code>$ ssh-keygen
</code></pre>

<p>and accept all defaults.</p></li>
<li><p>Add "nou" user's ssh public key to git user ~/.ssh/authorized_keys</p>

<pre><code>$ sudo cat nou_user_home/.ssh/id_rsa.pub &gt;&gt; git_user_home/.ssh/authorized_keys
</code></pre></li>
</ol>

<p>After this try below command from "nou" user:</p>

<pre><code>docker build git@localhost:/home/nou/code/lib.git
</code></pre>

<p>If <code>Dockerfile</code> is not at the root of cloned repository then:</p>

<pre><code>docker build -f folder_containing_dockerfile/Dockerfile 
git@localhost:/home/nou/code/lib.git
</code></pre>

<p>If this all works fine then you can try with your <code>docker-compose</code> file.</p>

CC BY-SA 3.0
2017-10-03T18:34:18.850Z
2017-10-14T06:41:26.560Z
2017-10-14T06:41:26.560Z
MNA
<p>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:</p>

<ul>
<li>Build straight from Github: <code>docker build https://github.com/docker/rootfs.git</code></li>
<li>Run a Git daemon to use the <code>git://</code> protocol: <code>git daemon --base-path=. --export-all</code> (in <code>/home/nou/code</code>)</li>
<li>Running a Git daemon from within Docker, mounting your bare repository as a volume and building inside it via a mounted Docker socket</li>
<li>Converting your bare repository to a normal one</li>
</ul>

<p><a href="https://docs.docker.com/engine/reference/commandline/build/#build-with-url" rel="nofollow noreferrer">https://docs.docker.com/engine/reference/commandline/build/#build-with-url</a></p>

CC BY-SA 3.0
2017-10-03T14:08:12.727Z
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
CC BY-SA 3.0
2017-10-03T20:59:44.170Z
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
CC BY-SA 3.0
2017-10-03T22:28:25.750Z
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
CC BY-SA 3.0
2017-10-03T11:07:56.203Z
2017-10-03T22:26:17.483Z
2017-10-03T22:26:17.483Z
user2640621
<p>I have a git repository placed in local file system at <code>/home/nou/code/lib.git</code>
Also I have <code>/home/nou/docker-compose.yaml</code> that is used to deploy a service based on the code from the repo:</p>

<pre><code>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/
</code></pre>

<p>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):</p>

<pre><code>nou@ubuntu:~/$ docker-compose build
Building lib
ERROR: error fetching: fatal: unable to connect to .:
.: No address associated with hostname

: exit status 128
</code></pre>

<p>How can I use local git repo to build services using docker-compose?</p>

CC BY-SA 3.0
2017-10-03T07:46:24.130Z
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
CC BY-SA 3.0
2017-10-03T13:35:05.757Z
@Tensibai but with that suggestion how does composer know it should be using git? :)
Dan Cornilescu
CC BY-SA 3.0
2017-10-03T14:21:43.193Z
@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
CC BY-SA 3.0
2017-10-03T15:43:35.947Z
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
CC BY-SA 3.0
2017-10-03T21:20:09.937Z
@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
CC BY-SA 3.0
2017-10-03T23:19:36.033Z
My understanding is that the intention is to pull that git repo... If it's not the case - pls disregard my comments.
Dan Cornilescu
<docker><git><docker-compose>
How to "docker-compose build" a service from code located in local git repository?
FLCL
# How to "docker-compose build" a service from code located in local git repository?

(Asked by: username_0 on 2017-10-02)

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?

**Tags:** docker, git, docker-compose

> **Comments:**
> **username_1**: 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  
> **username_2**: @username_1 but with that suggestion how does composer know it should be using git? :)  
> **username_1**: @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  
> **username_2**: 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.  
> **username_1**: @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...  
> **username_2**: My understanding is that the intention is to pull that git repo... If it's not the case - pls disregard my comments.  

---

## 2 answers

### Answer by username_4 on 2017-10-03. Score: 6 | Accepted answer

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>

> **Comments:**
> **username_0**: 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)  
> **username_0**: 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`...  
> **username_4**: 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.  

---

### Answer by username_3 on 2017-10-03. Score: 4

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.


---

