Setting up GitLab with Docker: Daily Backup, Restore, Migration, and Upgrade
In this article, I will guide you through the steps of setting up GitLab with Docker, performing daily backups, restoring from backups, migrating to a new server, and upgrading to a newer version.
Docker Setup for GitLab
- Edit the
docker-compose.yml
file with the necessary configurations:
version: '3.4'
x-logging: &logging
driver: "json-file"
options:
max-size: "10m"
max-file: "1"
services:
gitlab:
image: gitlab/gitlab-ce:14.0.1-ce.0
container_name: gitlab
restart: always
hostname: git.yoursite.com
environment:
TZ: Asia/Shanghai
GITLAB_OMNIBUS_CONFIG: |
external_url "https://git.yoursite.com"
gitlab_rails['gitlab_shell_ssh_port'] = 23022
gitlab_rails['backup_keep_time'] = 2592000
nginx['client_max_body_size'] = 0
nginx['enable'] = true
nginx['listen_port'] = 80
nginx['listen_https'] = false
ports:
- '80:80'
- '23022:22'
volumes:
- '/data/gitlab/config:/etc/gitlab'
- '/data/gitlab/logs:/var/log/gitlab'
- '/data/gitlab/data:/var/opt/gitlab'
- '/data/gitlab/backups:/var/opt/gitlab/backups'
- '/data/gitlab/cron.d:/etc/cron.d'
logging: *logging
-
Run
docker-compose up -d
to start the GitLab service. -
Set the initial administrator password:
Enter the GitLab container and execute the command:gitlab-rake "gitlab:password:reset"
After a few seconds, follow the prompts to input the username (root) and the initial password.
The important paths to note are:
Local location | Container location | Usage |
---|---|---|
/srv/gitlab/data | /var/opt/gitlab | For storing application data |
/srv/gitlab/logs | /var/log/gitlab | For storing logs |
/srv/gitlab/config | /etc/gitlab | For storing the GitLab configuration files |
Creating a Backup
To create a backup, execute the following command:
gitlab-rake gitlab:backup:create
This will create a backup file in the directory specified in the gitlab_rails['backup_path']
configuration in gitlab.rb
.
Restoring from Backup
To restore from a backup, follow these steps:
- Copy the .tar backup file to the
backup_path
directory on the new server. - Refer to the official documentation: https://docs.gitlab.com/ee/raketasks/restore_gitlab.html
docker exec gitlab update-permissions
docker restart gitlab
docker exec gitlab gitlab-ctl stop puma
docker exec gitlab gitlab-ctl stop sidekiq
chmod +r 1674844794_2023_01_28_13.12.15_gitlab_backup.tar
docker exec -it gitlab /bin/bash
gitlab-backup restore BACKUP=1674844794_2023_01_28_13.12.15
Note: If you encounter the ERROR: must be the owner of the extension
error during the restore process, you can ignore it. Refer to: https://docs.gitlab.com/ee/raketasks/backup_restore.html#restoring-database-backup-using-omnibus-packages-outputs-warnings
Restoring Secret Data
Restoring secret data is essential to avoid 500 errors on the admin's runner interface. Follow these steps:
- Copy
/etc/gitlab/gitlab-secrets.json
to the configuration directory on the new server. - Inside the Docker container, run:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
sudo gitlab-rake gitlab:check SANITIZE=true
sudo gitlab-rake gitlab:doctor:secrets
The admin's runner interface should now work without errors, but all runners may appear offline. To resolve this, restart each runner server using gitlab-runner restart
.
Refer to the official documentation: https://docs.gitlab.com/ee/raketasks/restore_gitlab.html
Note: When migrating to a new server, you may not need to copy the original server's gitlab.rb
configuration, as the settings, such as various paths, might differ between the two servers.
Upgrading GitLab
To upgrade GitLab, follow these steps:
- Modify the Docker Compose file by updating the image version.
- Execute
docker-compose down; docker-compose up -d
to automatically upgrade GitLab. - During the initial upgrade startup, monitor the progress using
docker logs -f gitlab
.
Keep in mind that the upgrade should not span a significant version gap. Refer to the official Upgrade Paths: https://docs.gitlab.com/ee/update/index.html#upgrade-paths
Daily Backup
Edit the /etc/crontab
file and add the following rule to create daily backups:
0 2 * * 6 root docker exec gitlab gitlab-rake gitlab:backup:create
By following these steps, you can efficiently manage GitLab with Docker, ensuring data backup, seamless restore, smooth migration, and timely upgrades.