Deploying a Go service to the Integrated Docker Registry in Gitlab

Deploying a Go service to the Integrated Docker Registry in Gitlab

In this second part of the Gitlab/K8S CI articles, we deploy a simple Go server using the Gitlab CI and Registry we set up earlier, and run the image built pulling it from our own registry.

Go Service

In gitlab create a private project 'hi' in

Clone the project (in my case the username is sr)

git clone ssh://

Navigate to it:

cd hi

Add the file hi.go with the following:

package main

import (

func main() {
	fmt.Println("Started on 8090")
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hi %s\n", r.URL.Path)
	http.ListenAndServe(":8090", nil)

Add, commit and push the file to git. Browse your local gitlab and you should see the file hi.go listed.

Create following and name it Dockerfile

FROM golang:1.12.0-alpine3.9 as builder
RUN mkdir /app
ADD hi.go /app
RUN go build -o hi .

FROM alpine:latest as host
RUN apk add --no-cache ca-certificates
COPY --from=builder /app/hi /
CMD ["/hi"]

Gitlab ci file .gitlab-ci.yml

  DOCKER_DRIVER: overlay2

  - mkdir -p $GOPATH/src/$(dirname $REPO_NAME)
  - cd $GOPATH/src/$REPO_NAME  

  - test
  - build
  - release
  - deploy

  image: golang:latest
  stage: test
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)

  image: golang:latest
  stage: build
    - go build -race -ldflags "-extldflags '-static'" -o $CI_PROJECT_DIR/hi
      - hi

    image: docker:19.03.1
    stage: release
      - echo ${CONTAINER_IMAGE}
      - echo  $CI_BUILD_TOKEN | docker login -u gitlab-ci-token --password-stdin ${CI_REGISTRY}
      - docker build -t ${CONTAINER_IMAGE} -t ${CONTAINER_IMAGE_LATEST} .
      - docker push ${CONTAINER_IMAGE}
      - docker push ${CONTAINER_IMAGE_LATEST}

Register a runner and set it to run without tags (see the previous post ...)

Git commit these files and push.

The CI should kick in, build the go service and register it. The release phase should be something like the following:

Running with gitlab-runner 12.4.0 (1564076b)
  on Runner 8RydtMPh
Using Docker executor with image docker:19.03.1 ...
Pulling docker image docker:19.03.1 ...
Using docker image sha256:0cecfefe921f22fc898f7a0055358380c8870ab6f05b01999367911714fe9d00 for docker:19.03.1 ...
Running on runner-8RydtMPh-project-3-concurrent-0 via d00feb8672c0...
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /builds/sr/hi/.git/
Checking out 3538cfa1 as master...
Removing hi

Skipping Git submodules setup
Downloading artifacts for compile (124)...
Downloading artifacts from coordinator... ok        id=124 responseStatus=200 OK token=dWTn-Pwd
$ echo  $CI_BUILD_TOKEN | docker login -u gitlab-ci-token --password-stdin ${CI_REGISTRY}
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See

Login Succeeded
Sending build context to Docker daemon  9.755MB

Step 1/11 : FROM golang:1.12.0-alpine3.9 as builder
 ---> 2205a315f9c7
Step 2/11 : RUN mkdir /app
 ---> Using cache
 ---> 54da6752cdd5
Step 3/11 : ADD hi.go /app
 ---> d21d0d2af424
Step 4/11 : WORKDIR /app
 ---> Running in e2e87def04e3
Removing intermediate container e2e87def04e3
 ---> 4d78d2a12967
Step 5/11 : RUN go build -o hi .
 ---> Running in 60fc1b0dffe8
Removing intermediate container 60fc1b0dffe8
 ---> 5b2fa72dd518
Step 6/11 : FROM alpine:latest as host
 ---> 965ea09ff2eb
Step 7/11 : RUN apk add --no-cache ca-certificates
 ---> Using cache
 ---> ba261b495589
Step 8/11 : COPY --from=builder /app/hi /
 ---> 8d376b2c37cb
Step 9/11 : WORKDIR /
 ---> Running in 7f60e9fb9d88
Removing intermediate container 7f60e9fb9d88
 ---> 387b7422eab2
Step 10/11 : EXPOSE 8090
 ---> Running in 3aa6f0aa255b
Removing intermediate container 3aa6f0aa255b
 ---> 9f8c5dbf79b4
Step 11/11 : CMD ["/hi"]
 ---> Running in a46f92a45ab4
Removing intermediate container a46f92a45ab4
 ---> 4a72c58b9b54
Successfully built 4a72c58b9b54
Successfully tagged
Successfully tagged
$ docker push ${CONTAINER_IMAGE}
The push refers to repository []
f85f9ff79b0a: Preparing
3162ed9eaf42: Preparing
77cae8ab23bf: Preparing
3162ed9eaf42: Layer already exists
77cae8ab23bf: Layer already exists
f85f9ff79b0a: Pushed
master_3538cfa18843906473c0a075d39cce785d2f5eb4: digest: sha256:393b1ab11ab37aff4ddf9192fb8dd96561d15cab875aa67266efdef5234b49c4 size: 949
The push refers to repository []
f85f9ff79b0a: Preparing
3162ed9eaf42: Preparing
77cae8ab23bf: Preparing
f85f9ff79b0a: Layer already exists
3162ed9eaf42: Layer already exists
77cae8ab23bf: Layer already exists
latest: digest: sha256:393b1ab11ab37aff4ddf9192fb8dd96561d15cab875aa67266efdef5234b49c4 size: 949
Job succeeded

You should see the image in  Packages -> Container Registry.

Copy the latest image details and run it like so:

docker run --rm -it -p 8090:8090

It should output

Started on 8090

Navigate to the following link:


Should give you:

Hi /there

In the next post we look at integrating kubernetes to gitlab and deploying the image to it.

Related Article