直接使用gitlab-ce镜像
从docker hub上找到社区版的gitlab镜像,然后从readme指南找到示例docker-compose.yml,稍作修改便可直接使用。
创建config,logs,data文件夹;
创建docker-compose.yml文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| version: '3'
services: gitlab: container_name: gitlab image: gitlab/gitlab-ce hostname: gitlab #restart: always ports: - "8443:443" - "8989:8989" - "2222:22" environment: GITLAB_OMNIBUS_CONFIG: | external_url 'http://127.0.0.1:8989'
volumes: - ~/dbs/gitlab/config:/etc/gitlab - ~/dbs/gitlab/logs:/var/log/gitlab - ~/dbs/gitlab/data:/var/opt/gitlab
|
在开始时映射端口未加引号导致出现端口映射错误
1
| for gitlab Cannot create container for service gitlab: invalid port specification: "133342"
|
原因可参考docker错误解析yml配置,在端口加上引号即可解决。
使用外置redis和postgres
gitlab镜像自带了redis和postgres,这里可以改用外置的redis和postgres。
docker搭建postgres
先使用docker搭建postgres。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| version: '3.1'
services:
postgres: container_name: postgres hostname: postgres image: postgres #restart: always networks: - default - dbnetwork ports: - 5432:5432 environment: POSTGRES_USER: lyyljs POSTGRES_PASSWORD: 123456 volumes: - ~/dbs/postgres:/var/lib/postgresql/data
networks: dbnetwork: external: true
|
gitlab使用外置postgres
修改gitlab的docker-compose.yml,使其使用该容器postgres。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| version: '3'
services: gitlab: container_name: gitlab image: gitlab/gitlab-ce hostname: gitlab #restart: always networks: - default - dbnetwork ports: - "8443:443" - "8989:8989" - "10022:22" environment: GITLAB_OMNIBUS_CONFIG: | external_url 'http://127.0.0.1:8989' postgresql['enable'] = false gitlab_rails['db_adapter'] = 'postgresql' gitlab_rails['db_encoding'] = 'utf8' gitlab_rails['db_host'] = 'postgres' gitlab_rails['db_port'] = '5432' gitlab_rails['db_username'] = 'lyyljs' gitlab_rails['db_password'] = '123456' gitlab_rails['db_database'] = "gitlabhq"
volumes: - ~/dbs/gitlab/config:/etc/gitlab - ~/dbs/gitlab/logs:/var/log/gitlab - ~/dbs/gitlab/data:/var/opt/gitlab
networks: dbnetwork: external: true
|
可通过 docker exec -it gitlab /bin/bash 进入gitlab容器控制台,再通过 psql -h postgres -U lyyljs 测试连通,访问外置数据库。
docker搭建redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| version: '3.1'
services: redis: container_name: redis hostname: redis image: redis #restart: always networks: - default - dbnetwork ports: - 6379:6379 volumes: - ~/dbs/redis:/data networks: dbnetwork: external: true
|
- 启动后本地可使用 redis-cli 直接连接,并使用 get a 测试连通。
但此时有问题是redis默认是没有密码的,现在想使用自定义配置。新建redis.conf文件,从官方包下copy内容,添加需要密码,并取消只监听localhost。
从官网文档上根据docker命令修改image。
新建Dockerfile文件,内容如下:
1 2 3
| FROM redis COPY redis.conf /usr/local/etc/redis/redis.conf CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
|
然后修改docker-compose.yml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| version: '3.1'
services: redis: container_name: redis hostname: redis build: . #restart: always networks: - default - dbnetwork ports: - 6379:6379 volumes: - ~/dbs/redis:/data networks: dbnetwork: external: true
|
docker-compose up –build 启动后报错
1
| # Can't chdir to '/usr/local/var/db/redis/': No such file or directory
|
该错误有一个已关闭的git issue给出了解决方案,即手动创建这个不存在的文件夹。
重新修改 Dockerfile 文件
1 2 3 4
| FROM redis COPY redis.conf /usr/local/etc/redis/redis.conf RUN mkdir -p /usr/local/var/db/redis CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
|
docker-compose up –build,启动成功,使用 redis-cli -a 123456 连接,并执行 get a,测试成功。
gitlab 使用外置redis
修改docker-compose.yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| version: '3'
services: gitlab: container_name: gitlab image: gitlab/gitlab-ce hostname: gitlab #restart: always networks: - default - dbnetwork ports: - "8443:443" - "8989:8989" - "10022:22" environment: GITLAB_OMNIBUS_CONFIG: | external_url 'http://127.0.0.1:8989' postgresql['enable'] = false gitlab_rails['db_adapter'] = 'postgresql' gitlab_rails['db_encoding'] = 'utf8' gitlab_rails['db_host'] = 'postgres' gitlab_rails['db_port'] = '5432' gitlab_rails['db_username'] = 'lyyljs' gitlab_rails['db_password'] = '123456' gitlab_rails['db_database'] = "gitlabhq" redis['enable'] = false gitlab_rails['redis_host'] = 'redis' gitlab_rails['redis_port'] = 6379 gitlab_rails['redis_password'] = '123456' gitlab_rails['redis_database'] = 2
volumes: - ~/dbs/gitlab/config:/etc/gitlab - ~/dbs/gitlab/logs:/var/log/gitlab - ~/dbs/gitlab/data:/var/opt/gitlab
networks: dbnetwork: external: true
|
如果遇到redis持久化错误,
1
| MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
|
检查redis的compose配置挂载位置是否与redis.conf中持久化存储位置dir配置相同。
测试
访问http://localhost:8989/,创建root账号,配置好ssh keys后,新建项目gateway。
在本地gateway项目执行以下命令:
1 2 3 4 5 6 7 8 9
| git init
git remote add origin ssh://git@127.0.0.1:10022/root/gateway.git
git add .
git commit -m "init"
git push -u origin master
|
刷新网页即可看见已经提交的文件了。
清理none image
在配置redis时,因为修改Dockerfile文件导致有名称为 none 的image产生,使用 docker rmi $(docker images -f “dangling=true” -q) 命令移除多余的image。
参考