How do I bind a domain name to gitlab in the docker container?

  docker, question

At present, it can be accessed through ip, but it is not elegant (Virgo)

nginx.conf

user  www www;
 
 worker_processes  2;
 error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;  # log location and log > level
 pid /usr/local/webserver/nginx/nginx.pid;
 
 events {
 use epoll;
 worker_connections  65535;
 }
 
 
 http {
 include mime.types;
 default_type application/octet-stream;
 log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" $http_x_forwarded_for';
 
 charset utf-8;
 server_names_hash_bucket_size 128;
 client_header_buffer_size 32k;
 large_client_header_buffers 4 32k;
 client_max_body_size 8m;
 
 sendfile on;
 tcp_nopush on;
 keepalive_timeout 60;
 tcp_nodelay on;
 fastcgi_connect_timeout 300;
 fastcgi_send_timeout 300;
 fastcgi_read_timeout 300;
 fastcgi_buffer_size 64k;
 fastcgi_buffers 4 64k;
 fastcgi_busy_buffers_size 128k;
 fastcgi_temp_file_write_size 128k;
 gzip on;
 gzip_min_length 1k;
 gzip_buffers 4 16k;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_types text/plain application/x-javascript text/css application/xml;
 gzip_vary on;
 
 server {
 listen       80;
 server_name   git.example.com ;
 location / {
 client_max_body_size 50m;
 proxy_redirect off;
 proxy_set_header  Host $host;
 proxy_set_header  X-Real-IP $remote_addr;
 proxy_set_header  X-Forwarded-Proto https;
 proxy_set_header  X-Forwarded-For $remote_addr;
 proxy_set_header  X-Forwarded-Host $remote_addr;
 proxy_connect_timeout      180s;
 proxy_send_timeout         180s;
 proxy_read_timeout         180s;
 proxy_pass     http://localhost:3000;
 }
 }
 
 }

Docker start command

sudo docker run --detach \
 --hostname  git.example.cn \
 --publish 8443:443 --publish 3000:80 --publish 2222:2222 \
 --name gitlab \
 --restart always \
 --volume /srv/gitlab/config:/etc/gitlab \
 --volume /srv/gitlab/logs:/var/log/gitlab \
 --volume /srv/gitlab/data:/var/opt/gitlab \
 twang2218/gitlab-ce-zh:latest

I solved the writing process myself.

1. Start the container

sudo docker run --detach \
 --hostname  git.example.com \
 --publish 8443:443 --publish 3000:80 --publish 2222:2222 \
 --name gitlab \
 --restart always \
 --volume /srv/gitlab/config:/etc/gitlab \
 --volume /srv/gitlab/logs:/var/log/gitlab \
 --volume /srv/gitlab/data:/var/opt/gitlab \
 twang2218/gitlab-ce-zh:latest

2. turn off gitlab’s own nginx

docker exec -it gitlab vim /etc/gitlab/gitlab.rb
 Nginx['enable'] = false # remove comment # save exit
 docker exec -it gitlab gitlab-ctl reconfigure
 Sudo chmod -R 777 /srv/gitlab # folder owner permissions

3. Add to nginx Configuration http

log_format gitlab_access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
 proxy_cache_path proxy_cache keys_zone=gitlab:10m max_size=1g levels=1:2;
 proxy_cache gitlab;
 
 map $http_upgrade $connection_upgrade {
 default upgrade;
 ''      close;
 }
 
 
 upstream gitlab-workhorse {
 server unix:/srv/gitlab/data/gitlab-workhorse/socket;
 }
 
 server {
 listen *:3000;
 
 
 server_name  git.iamsjy.cn ;
 server_tokens off;
 client_max_body_size 0;
 
 add_header Strict-Transport-Security "max-age=31536000";
 
 access_log  /srv/gitlab/logs/nginx/gitlab_access.log gitlab_access;
 error_log   /srv/gitlab/logs/nginx/gitlab_error.log;
 
 if ($http_host = "") {
 set $http_host_with_default "git.iamsjy.cn:3000";
 }
 
 if ($http_host !  = "") {
 set $http_host_with_default $http_host;
 }
 
 proxy_read_timeout      3600;
 proxy_connect_timeout   300;
 proxy_redirect          off;
 proxy_http_version 1.1;
 
 proxy_set_header Host $http_host_with_default;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection $connection_upgrade;
 proxy_set_header X-Forwarded-Proto http;
 
 location ~ (\.git/gitlab-lfs/objects|\.git/info/lfs/objects/batch$) {
 proxy_cache off;
 proxy_pass http://gitlab-workhorse;
 proxy_request_buffering off;
 }
 
 location / {
 proxy_cache off;
 proxy_pass  http://gitlab-workhorse;
 }
 
 location /assets {
 proxy_cache gitlab;
 proxy_pass  http://gitlab-workhorse;
 }

Finally restart nginx and you’re done.

clipboard.png

Add that folder permissions must be given every time the container is restarted.
Sudo chmod -R 777 /srv/gitlab # folder owner permissions