Operations 6 min read

Master GitLab CI: From Code to Docker Deployment in Minutes

This guide walks you through setting up a GitLab CI environment, configuring a Docker‑enabled runner, writing Dockerfile and .gitlab-ci.yml files, and automating code compilation, container image creation, and deployment of a Go application, complete with command examples and troubleshooting tips.

New Oriental Technology
New Oriental Technology
New Oriental Technology
Master GitLab CI: From Code to Docker Deployment in Minutes

1. Preparation

Set up a GitLab instance (using the hosted service for convenience) and a target server that has Docker and GitLab Runner installed (e.g., a CentOS 7 64‑bit machine). Use a sample Go project ( testgolang) from the official GitLab repository as the source code.

Create a Dockerfile to build the Go binary and package it into an image, and a .gitlab-ci.yml file that defines the CI/CD pipeline.

# Dockerfile example
FROM golang:latest
MAINTAINER William "[email protected]"
WORKDIR $GOPATH/src/chinaase.com/testgolang
COPY . $GOPATH/src/chinaase.com/testgolang
RUN go build .
EXPOSE 8001
ENTRYPOINT ["./testgolang"]
# .gitlab-ci.yml example
stages:
  - deploy

docker-deploy:
  stage: deploy
  script:
    - docker build -t testgolang .
    - if [ $(docker ps -aq --filter name=testgolang) ]; then docker rm -f testgolang; fi
    - docker run -d -p 8001:8001 --name testgolang testgolang
  tags:
    - mbp13
  only:
    - golang

2. Environment Configuration

Register a runner on the target server and link it to the GitLab project. The runner is identified by the tag mbp13, which is used in the pipeline definition to select the execution node.

Typical registration steps involve obtaining the registration token from GitLab and executing:

sudo gitlab-runner register
# Follow prompts to provide URL, token, description, tags (e.g., mbp13), and executor (docker)

Additional commands to ensure the runner can use Docker:

sudo groupadd docker
sudo gpasswd -a gitlab-runner docker
newgrp docker
sudo docker ps   # verify Docker works without sudo

3. Commit Updates and Automatic Deployment

Push changes to the golang branch. GitLab CI automatically detects the push, reads .gitlab-ci.yml, and starts the defined job.

The pipeline builds the Docker image, removes any existing container with the same name, and runs a new container exposing port 8001.

Job results are visible under the CI/CD → Jobs section in GitLab.

4. Access the Deployed Service

After the pipeline finishes, the Go application is reachable at http://127.0.0.1:8001/hello.

5. Supplement: Deploying GitLab Itself with Docker

For a self‑hosted GitLab instance, run the following commands:

docker pull beginor/gitlab-ce:11.3.0-ce.0

docker run --detach \
  --publish 8443:443 \
  --publish 8880:80 \
  --publish 8822:22 \
  --name my-gitlab \
  --restart unless-stopped \
  --volume /Users/ys/05Docker_workspace/gitlab/etc:/etc/gitlab \
  --volume /Users/ys/05Docker_workspace/gitlab/log:/var/log/gitlab \
  --volume /Users/ys/05Docker_workspace/gitlab/data:/var/opt/gitlab \
  --privileged=true \
  beginor/gitlab-ce:11.3.0-ce.0

6. Prerequisite Knowledge

Basic Docker commands

Git command usage

GitLab operation basics

Writing a .gitlab-ci.yml file

Programming language fundamentals (Go used in examples)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerCI/CDAutomationGoGitLab CI
New Oriental Technology
Written by

New Oriental Technology

Practical internet development experience, tech sharing, knowledge consolidation, and forward-thinking insights.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.