Gitlab – ce mirror

Before setting anything else, configure a new environment variable, $GITLAB_HOME, pointing to the directory where the configuration, log, and data files will reside.

Setting environment Variables

export GITLAB_HOME=/srv/gitlab
Copy the code

The GitLab container uses volumes mounted by the host to store persistent data:

The installation

Pull the mirror

docker pull gitlab/gitlab-ce:latest
Copy the code

You can fine-tune these directories to suit your needs. After setting the GITLAB_HOME variable, you can run the image

sudo docker run --detach \
  --hostname gitlab.example.com \
  --publish 443:443 --publish 80:80 --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab \
  --volume $GITLAB_HOME/logs:/var/log/gitlab \
  --volume $GITLAB_HOME/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest
Copy the code

This will start a GitLab container and publish the ports needed to access SSH, HTTP, and HTTPS. All GitLab data will be stored in a subdirectory of $GITLAB_HOME. The container will automatically restart after the system restarts.

The initialization process may take a long time. You can view log tracking for this process:

sudo docker logs -f gitlab
Copy the code

After starting a container, you can visit http://192.168.131.130. It may take a while before the Docker container starts responding to queries. When you first visit GitLab, you will be asked to set an administrator password. After the modification, you can use the root user name and password to log in

configuration

This container uses the official generic GitLab package, all configured in /etc/gitlab/gitlab.rb. Enter the Docker container.

docker exec -it gitlab /bin/bash
Copy the code

Once you open /etc/gitlab/gitlab.rb. Ensure that external_URL is set to point to a valid URL. After making the changes you want, you need to restart the container in order to reconfigure GitLab

Run GitLab on a public IP address

You can have Docker use your IP address and forward all traffic to the GitLab container by changing the — publish flag.

Sudo docker run --detach \ --hostname gitlab.example.com \ --publish 198.51.100.1:443:443 \ --publish 198.51.100.1:80:80 \ --publish 198.51.100.1:22:22 \ --name gitlab \ --restart always \ --volume $GITLAB_HOME/config:/etc/gitlab \ --volume  $GITLAB_HOME/logs:/var/log/gitlab \ --volume $GITLAB_HOME/data:/var/opt/gitlab \ gitlab/gitlab-ce:latestCopy the code

The GitLab instance can then be accessed via http://198.51.100.1/ and https://198.51.100.1/.

Expose GitLab on different ports

If you want to change the port, you need to add a separate — publish directive to the Docker run command.

1. To expose the web interface on port 8929 and SSH service on port 2289, use the following docker run command:

sudo docker run --detach \
  --hostname gitlab.example.com \
  --publish 8929:8929 --publish 2289:22 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab \
  --volume $GITLAB_HOME/logs:/var/log/gitlab \
  --volume $GITLAB_HOME/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest
Copy the code

2. Enter the container

sudo docker exec -it gitlab /bin/bash
Copy the code

3. Run the /etc/gitlab/gitlab.rb command to set external_url:

A certificate is required to create a runner when HTTPS is set. For convenience, HTTP is recommended. If you want to use HTTPS, look at the considerations when creating RUNNERS

# For HTTP
external_url "http://gitlab.example.com:8929"

or

# For HTTPS (notice the https)
external_url "https://gitlab.example.com:8929"
Copy the code

The port specified in this URL must match the port that Docker publishes to the host.

4. Set the gitlab_shell_ssh_port:

 gitlab_rails['gitlab_shell_ssh_port'] = 2289 
Copy the code

5. Restart GitLab:

 gitlab-ctl reconfigure 
Copy the code

Following the example above, you will be able to access GitLab from a Web browser using port 8929 and push docker-compose using SSH under port 2289.

Gitlab-Runner

Start the Gitlab-Runner container

docker pull gitlab/gitlab-runner:latest
Copy the code

To run Gitlab-Runner in a Docker container, you need to ensure that the configuration is not lost when the container is restarted. To do this, there are two options

  • Start the Runner container with a local system volume mount

    Use the local system as the configuration volume mounted to the Gitlab-Runner container. This volume is used for configuration files and other resources.

       docker run-d --name gitlab-runner --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest
    Copy the code
  • Use the Docker volume to start the Runner container

    • Create a docker volume

      docker volume create gitlab-runner-config
      Copy the code
    • Start the GitLab runner container with the volume you just created:

      docker run-d --name gitlab-runner --restart always \ -v /var/run/docker.sock:/var/run/docker.sock \ -v gitlab-runner-config:/etc/gitlab-runner \ gitlab/gitlab-runner:latest
      Copy the code

Install a trusted SSL server certificate

If your GitLab CI server uses a self-signed SSL certificate, you should ensure that the GitLab CI server certificate is trusted by the GitLab Runner container so that they can communicate with each other. The gitlab/gitlab-runner image is configured to look for trusted SSL certificates in /etc/gitlab-runner/certs/ca.crt.

Sign up for Gitlab-Runner using Docker

Here is the runner who creates the specified project. Assuming we have already created a project.

The following steps describe starting a Gitlab-Runner container to register runners. After registration is complete, the generated configuration is written to the selected configuration volume (/ SRV /gitlab-runner/config) and will be loaded by the running program using this configuration volume.

  1. Run register according to the mount type:

    1. Mount a local system volume

      docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
      Copy the code
    2. Docker volume mount

      docker run --rm -it -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest register
      Copy the code
  2. Enter your GitLab instance URL(also known as the Gitlab-CI Coordinator URL).

  3. Enter the token you get to register runner.

  4. Enter a description of runner. You can change this value later in the GitLab user interface.

  5. Enter the tags associated with runner, separated by commas. You can change this value later in the GitLab user interface.

  6. Provide runner Executor. For most use cases, enter Docker.

  7. If you enter Docker as your executor, you need to provide the default image (Ruby :latest) for projects that do not have.gitlab-ci.yml.

You can also specify parameters in advance

Gitlab - runner register \ - non -interactive \ - url "https://192.168.131.130:8929/" \ - registration - token "xNzRQx8v3z66EtPo-K_P" \ --executor "docker" \ --docker-image maven:latest \ --description "runner " \ --tag-list "run" \ --run-untagged \ --locked="false"Copy the code

Once registered, you can view the registered Runners in Setting ->CI/CD->Runners->Expand

To avoid errors, check Run untagged Jobs for the runner

Matters needing attention

cannot validate certificate

This is because we set external_url to HTTPS in /etc/gitlab/gitlab.rb. A certificate is required at request.

  1. Go into the GitLab container and look for the certificate. Certificate path: /etc/gitlab/ssl

  2. Copy the certificate to the GitLab container using Docker CP.

    1. You can store the certificate in a certain path. Gitlab-runner register uses — tlS-ca-file to specify the certificate path

    2. It is stored in /etc/gitlab-runner/certs/. There is no need to specify the certificate path after restart

    1. If -volume is used to map the configuration file of GitLab, it can be directly saved to the corresponding directory of the host computer.

Post Gitlab.zjy.com: 8929 / API/v4 / runn…: dial tcp 129.226.170.70:8929: connect: connection refused

Modify the hosts file in /etc

Echo "192.168.131.130 gitlab.zjy.com" >> hostsCopy the code