How to build docker image from github repository

79,480

Solution 1

docker build url#ref:dir

Git URLs accept context configuration in their fragment section, separated by a colon :. The first part represents the reference that Git will check out, this can be either a branch, a tag, or a commit SHA. The second part represents a subdirectory inside the repository that will be used as a build context.

For example, run this command to use a directory called docker in the branch container:

docker build https://github.com/docker/rootfs.git#container:docker

https://docs.docker.com/engine/reference/commandline/build/

Solution 2

The thing you specified as repo URL is not a valid git repository. You will get error when you will try

git clone github.com/docker-library/redis/tree/99c172e82ed81af441e13dd48dda2729e19493bc/2.8.10

Valid URL for this repo is github.com/docker-library/redis. So you may want to try following:

docker build github.com/docker-library/redis

But this will not work too. To build from github, docker requires Dockerfile in repository root, howerer, this repo doesn't provide this one. So, I suggest, you only have to clone this repo and build image using local Dockerfile.

Solution 3

One can use the following example which sets up a Centos 7 container for testing ORC file format. Make sure to escape the # sign:

$ docker build https://github.com/apache/orc.git\#:docker/centos7 -t orc-centos7

Share:
79,480
seanlook
Author by

seanlook

Updated on July 09, 2022

Comments

  • seanlook
    seanlook almost 2 years

    In official docs we can see:

    # docker build github.com/creack/docker-firefox
    

    It just works fine to me. docker-firefox is a repository and has Dockerfile within root dir.
    Then I want to buid redis image and exact version 2.8.10 :

    # docker build github.com/docker-library/redis/tree/99c172e82ed81af441e13dd48dda2729e19493bc/2.8.10
    2014/11/05 16:20:32 Error trying to use git: exit status 128 (Initialized empty Git repository in /tmp/docker-build-git067001920/.git/
    error: The requested URL returned error: 403 while accessing https://github.com/docker-library/redis/tree/99c172e82ed81af441e13dd48dda2729e19493bc/2.8.10/info/refs
    
    fatal: HTTP request failed
    )
    

    I got error above. What's the right format with build docker image from github repos?

  • seanlook
    seanlook over 9 years
    In fact docker build https://raw.githubusercontent.com/docker-library/redis/maste‌​r/2.8.10/Dockerfile works, but not like the official example. Thanks for your answer.
  • david
    david over 4 years
    The "-t" flag and name is important in order the image gets a name set. Otherwise you'll end up with <none> images.
  • runzhi xiao
    runzhi xiao over 2 years
    I don't know why it fails on my computer. The error says "failed to solve with frontend dockerfile.v0: failed to read dockerfile: failed to load cache key: subdir not supported yet". I'm using Docker-CE-20.10