New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new Dockerfile.docker-ce for docker-ce(>=v17.06) to build docker image #5322

Merged
merged 2 commits into from Aug 13, 2018

Conversation

3 participants
@alimy
Copy link
Contributor

alimy commented Jun 30, 2018

Docker-CE can be given to a new build stage by adding AS name to the FROM instruction sine release version of v17.06. The Dockerfile's FROM instruction like below:

FROM

FROM <image> [AS <name>]

Or

FROM <image>[:<tag>] [AS <name>]

Or

FROM <image>[@<digest>] [AS <name>]
  • Optionally a name can be given to a new build stage by adding AS name to the FROM instruction. The name can be used in subsequent FROM and COPY --from=<name|index> instructions to refer to the image built in this stage.

Find Docker-ce official document here.

You can use this patch to build docker image if docker-ce that version >=v17.06 is installed:

>docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:20:16 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:58 2018
  OS/Arch:      linux/amd64
  Experimental: false
  • Build docker image
>cd $GOPATH/src/github.com/gogs/gogs
>docker build -t <your/image-tag> -f Dockerfile.docker-ce .
@Unknwon

This comment has been minimized.

Copy link
Member

Unknwon commented Jul 2, 2018

Hi, thanks for the PR!

What does this new Dockerfile make differences/benefits from the existing Dockerfile(s) in regards to build image size or process or ...?

@alimy

This comment has been minimized.

Copy link
Contributor

alimy commented Jul 2, 2018

Docker-ce version

$ docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:20:16 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:58 2018
  OS/Arch:      linux/amd64
  Experimental: false

Use default Dockerfile to build docker image

  • Before build enviroment
$ free -h
              total        used        free      shared  buff/cache   available
Mem:           991M        311M        240M         37M        440M        480M
Swap:            0B          0B          0B

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G   18G  7.7G  70% /
devtmpfs        473M     0  473M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M   38M  459M   8% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000
  • use Dockerfile build image
$ time docker build -t bitriory/gogs:buildWithoutAS .
Sending build context to Docker daemon  56.53MB
Step 1/15 : FROM alpine:3.5
 ---> 6c6084ed97e5
Step 2/15 : ADD https://github.com/tianon/gosu/releases/download/1.9/gosu-amd64 /usr/sbin/gosu
Downloading  1.805MB/1.805MB
 ---> 2a88fa2404c6
...
...  #so many other output
...
github.com/gogs/gogs/vendor/github.com/urfave/cli
github.com/gogs/gogs/cmd
github.com/gogs/gogs
# github.com/gogs/gogs
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: fork/exec /usr/bin/gcc: cannot allocate memory

make: *** [Makefile:37: build] Error 2
The command '/bin/sh -c ./docker/build-go.sh     && ./docker/build.sh     && ./docker/finalize.sh' returned a non-zero code: 2

real	3m24.264s
user	0m2.422s
sys	0m0.342s
  • After build enviroment (even if build failure because no many memory)
$ free -h
              total        used        free      shared  buff/cache   available
Mem:           991M        306M        350M         37M        334M        477M
Swap:            0B          0B          0B

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G   18G  7.1G  72% /
devtmpfs        473M     0  473M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M   38M  459M   8% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000

Use Dockerfile.docker-ce to build docker image

  • Before build enviroment
$ free -h
              total        used        free      shared  buff/cache   available
Mem:           991M        294M        362M         37M        335M        489M
Swap:            0B          0B          0B

$ df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G   18G  7.1G  72% /
devtmpfs        473M     0  473M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M   38M  459M   8% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000
  • use Dockerfile.docker-ce build image
$ time docker build -t bitriory/gogs:buildWithAS -f Dockerfile.docker-ce .
Sending build context to Docker daemon  56.53MB
Step 1/20 : FROM golang:1.10.3-alpine AS binarybuilder
 ---> 22e024490b47
...
...  #so many other output
...
Step 20/20 : CMD ["/bin/s6-svscan", "/app/gogs/docker/s6/"]
 ---> Running in a3501f0dd0fb
Removing intermediate container a3501f0dd0fb
 ---> bb06f1fb0d04
Successfully built bb06f1fb0d04
Successfully tagged bitriory/gogs:buildWithAS

real	1m34.903s
user	0m2.413s
sys	0m0.311s

  • After build Enviroment
$ free -h
              total        used        free      shared  buff/cache   available
Mem:           991M        322M        223M         37M        445M        466M
Swap:            0B          0B          0B

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G   19G  6.7G  74% /
devtmpfs        473M     0  473M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M   38M  459M   8% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000
  • The Docker image file info
$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
bitriory/gogs                   buildWithAS         bb06f1fb0d04        3 minutes ago       94MB
<none>                          <none>              ab178e40157f        3 minutes ago       711MB

The image id of bb06f1fb0d04 is build result and ab178e40157f is Intermediate generate image that build gogs binary defined by FROM instruction of AS.

  • Push image to docker.io and see the size of image
$ docker push bitriory/gogs:buildWithAS
The push refers to repository [docker.io/bitriory/gogs]

The image size in https://hub.docker.com/r/bitriory/gogs/tags/ is 39MB

And image size in https://hub.docker.com/r/gogs/gogs/tags/ is 65MB

@alimy

This comment has been minimized.

Copy link
Contributor

alimy commented Jul 2, 2018

Dockerfile Dockerfile.docker-ce
Memory(available ~480M) build failure (need more memory) build success
image size in hub.dokcer.com 65MB(gogs/gogs:latest) 39MB(bitriory/gogs:buildWithAS)
Build time >3m24.264s (build failure because need more memory) ~1m34.903s
Other No Intermediate image generated will generate an intermediate image(can remove manual)
@alimy

This comment has been minimized.

Copy link
Contributor

alimy commented Jul 2, 2018

The feature of AS name to the FROM instruction in Docker-ce is since release version >=v17.06. I don't know other Linux distribution's docker package is have this feature. In my Fedora Workstation 28, default docker is not have this feature.

$ docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-59.gitaf6b32b.fc28.x86_64
 Go version:      go1.10.2
 Git commit:      63758e0-unsupported
 Built:           Tue Jun 12 19:05:31 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-59.gitaf6b32b.fc28.x86_64
 Go version:      go1.10.2
 Git commit:      63758e0-unsupported
 Built:           Tue Jun 12 19:05:31 2018
 OS/Arch:         linux/amd64
 Experimental:    false

So just use Dockerfile.docker-ce for Docker-ce (>=17.06) to use this feature build gogs docker image.

@Unknwon

This comment has been minimized.

Copy link
Member

Unknwon commented Jul 11, 2018

Thanks @alimy !

If the Docker Hub already has docker-ce(>=v17.06) support (not sure, do you know?), I suggest make changes directly on original Dockerfile. What do you think?

@cardonator

This comment has been minimized.

Copy link

cardonator commented Jul 16, 2018

This is a great PR. I wonder if gogs can run in a scratch container?

I would also think making these changes on the main Dockerfile makes sense. 17.06 is pretty old by now, and most distributions can be upgraded.

@@ -1,5 +1,3 @@
.git
.git/**

This comment has been minimized.

@alimy

alimy Jul 18, 2018

Contributor

if ignore .git will have error when build gogs

fatal: not a git repository (or any of the parent directories): .git
go install "-v" -ldflags '-X "github.com/gogs/gogs/pkg/setting.BuildTime=2018-07-18 01:38:48 UTC" -X "github.com/gogs/gogs/pkg/setting.BuildGitHash="' -tags 'sqlite cert pam'

This comment has been minimized.

@Unknwon

Unknwon Jul 18, 2018

Member

As long as the new Dockerfile build steps do not increate final image size because of coping the whole Git data, I think it is fine to include.

@@ -1,7 +1,13 @@
FROM alpine:3.5
FROM golang:alpine AS binarybuilder

This comment has been minimized.

@alimy

alimy Jul 18, 2018

Contributor

golang:alpine is docker image of alpine:latest platform of official golang latest stable build version.
eg: current golang:alpine == (alpine-3.8, go-1.10.3)

@Unknwon Unknwon changed the base branch from develop to docker/binary-builder Aug 13, 2018

@Unknwon

This comment has been minimized.

Copy link
Member

Unknwon commented Aug 13, 2018

Thank you!

@Unknwon Unknwon merged commit cc95d25 into gogs:docker/binary-builder Aug 13, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment