sameersbn / docker-gitlab

Dockerized GitLab
http://www.damagehead.com/docker-gitlab/
MIT License
7.9k stars 2.14k forks source link

docker-compose using different postgre port? #1222

Closed thomasWeise closed 7 years ago

thomasWeise commented 7 years ago

Hi. First of all, thanks for your great images! I am using redmine and want to use gitlab on the same server. I want the databases to be separate, so I thought I just assign a new port to the one of gitlab, à la:

...
postgresql:
  restart: always
  image: sameersbn/postgresql:9.6-2
  ports:
   - "15432:5432" # <----------------------------- here
  volumes:
  - /srv/docker/gitlab/postgresql:/var/lib/postgresql:Z
  environment:
  - DB_USER=gitlab
  - DB_PASS=password
  - DB_NAME=gitlabhq_production
  - DB_EXTENSION=pg_trgm

gitlab:
  restart: always
  image: sameersbn/gitlab:9.1.4
  depends_on:
  - redis
  - postgresql
  ports:
  - "10080:80"
  - "10022:22"
  volumes:
  - /srv/docker/gitlab/gitlab:/home/git/data:Z
  environment:
  - DEBUG=false

  - DB_ADAPTER=postgresql
  - DB_HOST=postgresql
  - DB_PORT=15432 # <----------------------------- here
  - DB_USER=gitlab
  - DB_PASS=password
  - DB_NAME=gitlabhq_production
...

But this does not seem to work. In particular, I get

...
Installing configuration templates...                                                                                            
Configuring gitlab...                                                                                                            
Configuring gitlab::database...........................................................                                          
Initializing logdir...                                                                                                           
Initializing datadir...                                                                                                          
Installing configuration templates...                                                                                            
Configuring gitlab...                                                                                                            
Configuring gitlab::database...........................................................                                          
Initializing logdir...                                                                                                           
Initializing datadir...                                                                                                          
Installing configuration templates...                                                                                            
Configuring gitlab...                                                                                                            
Configuring gitlab::database...........................................................
...

in a loop until I give up. Pretty much like in issue #999. This seems to indicate that the database is not found, which is reasonable since I mess with the ports.

Do you have any suggestion what I am doing wrong? Many thanks, Thomas.

lazyfrosch commented 7 years ago

15432 is the port used on the Docker host, to forward data to the container.

The container still uses 5432, and since you are linking the containers (depends_on), the internal port is still on postgresql:5432

thomasWeise commented 7 years ago

I see. Is there any way to change this? I mean, is there a method to achieve what I want to do, to use the postresql image two times with different ports (in two different compositions)?

lazyfrosch commented 7 years ago

By the hostname, each container gets linked with a different hostname. This is basically a entry into /etc/hosts.

So if you have multiple postgresql databases, call it db1 and db2. And tell the gitlab container to connect to db1

thomasWeise commented 7 years ago

Sorry, this may sound incredibly stupid, but does this mean that you mean instead of doing

...
postgresql:
  restart: always

I should do

...
gitlabpostgresql:
  restart: always

and change all corresponding occurences of postgresql to gitlabpostgresql? Could it be that easy??

lazyfrosch commented 7 years ago

Yes, plus editing the depends_on and gitlab environment variables.

thomasWeise commented 7 years ago

(and the DB_HOST variable)

Wow, cool. It indeed seems to work.

May I therefore suggest to @sameersbn to change all host names in his great docker-compose projects to unique names? He already seemingly uses unique ports. This way, beginners like me can more easily run several of them next to each other without collisions.

Again, many thanks, Thomas.

solidnerd commented 7 years ago

And why do you want to start one database for two gitlabs. This would require to change the database schemas.

All names are unique because you are using docker-compose. You have your own project for with the containers and the scale indicator. For example:

projectname_containername_scale-indicator. gitlab_postrgresql_1

For this you need only to use the -p paramater to your docker-compose.

docker-compose -p my-cool-gitlab up -d 

And the container names would be looking like this my-cool-gitlab-postrgresql_1

thomasWeise commented 7 years ago

No, not two gitlabs. I want one gitlab and one redmine. The redmine is running. I try to docker-compose up the gitlab, I get an error à la postgre is already used... The reason probably is, I guess, that both compositions use the same host name and port for the database(??) And both should be using different databases. With your suggestion, I got it to work. Now I have a system with gitlab, redmine, and seafile, all hosted neatly on the same server ^_^

chihkaiyu commented 7 years ago

I have same problem here... The original gitlab database was mysql, and I'm trying to convert it to postgresql since official recommend it.
The entire gitlab system works fine if I start up them separately, but fail every time when I try to start up via docker-compose. I stuck in Configuring loop like below...

Starting gitlab_postgresql_1 ...
Starting gitlab_postgresql_1
Creating gitlab_redis_1 ...
Creating gitlab_redis_1 ... done
Creating gitlab_gitlab_1 ...
Creating gitlab_gitlab_1 ... done
Attaching to gitlab_postgresql_1, gitlab_redis_1, gitlab_gitlab_1
postgresql_1  | Initializing datadir...
redis_1       | Starting redis-server...
redis_1       | [1] 31 Jul 15:48:32.830 # Server started, Redis version 2.8.4
redis_1       | [1] 31 Jul 15:48:32.831 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command
 'sysctl vm.overcommit_memory=1' for this to take effect.
gitlab_1      | Initializing logdir...
gitlab_1      | Initializing datadir...
gitlab_1      | Installing configuration templates...
gitlab_1      | Configuring gitlab...
postgresql_1  | Initializing certdir...
postgresql_1  | Initializing logdir...
postgresql_1  | Initializing rundir...
postgresql_1  | Setting resolv.conf ACLs...
postgresql_1  | Creating database user: gitlab
postgresql_1  | Creating database: gitlabhq_production...
postgresql_1  | ‣ Loading pg_trgm extension...
postgresql_1  | ‣ Granting access to gitlab user...
postgresql_1  | Starting PostgreSQL 9.6...
postgresql_1  | LOG:  database system was shut down at 2017-07-31 15:48:34 UTC
postgresql_1  | LOG:  MultiXact member wraparound protections are now enabled
postgresql_1  | LOG:  autovacuum launcher started
postgresql_1  | LOG:  database system is ready to accept connections
gitlab_1      | Configuring gitlab::database...........................................................gitlab_gitlab_1 exited with code 1
gitlab_1      | Initializing logdir...
gitlab_1      | Initializing datadir...
gitlab_1      | Installing configuration templates...
gitlab_1      | Configuring gitlab...
gitlab_1      | Configuring gitlab::database...........................................................Initializing logdir...
gitlab_1      | Initializing datadir...
gitlab_1      | Installing configuration templates...
gitlab_1      | Configuring gitlab...
gitlab_1      | Configuring gitlab::database...........................................................Initializing logdir...
gitlab_1      | Initializing datadir...
gitlab_1      | Installing configuration templates...
gitlab_1      | Configuring gitlab...
gitlab_1      | Configuring gitlab::database..........................................................

And this is my docker-compose.yml :

services: 
  redis:
    restart: always
    image: sameersbn/redis:latest
    command:
    - --loglevel warning
    volumes:
    - /home/kai/gitlab/redis:/var/lib/redis:Z

  postgresql:
    restart: always
    image: sameersbn/postgresql:9.6-2
    volumes:
    - /home/kai/gitlab/postgres:/var/lib/postgresql:Z
    environment:
    - DB_USER=gitlab
    - DB_PASS=password
    - DB_NAME=gitlabhq_production
    - DB_EXTENSION=pg_trgm

  gitlab:
    restart: always
    image: sameersbn/gitlab:9.0.5
    depends_on:
    - redis
    - postgresql
    ports:
    - "10080:80"
    - "10022:22"
    volumes:
    - /home/kai/gitlab/data:/home/git/data:Z
    environment:
    - DEBUG=false
    - DB_ADAPTER=postgresql
    - DB_HOST=postgresql
    - DB_PORT=5432
    - DB_USER=gitlab
    - DB_PASS=password
    - DB_NAME=gitlabhq_production

I skip some other environment variables.

And if I start up via a shell scirpt like below:

#!/bin/bash

docker run --name gitlab-postgresql -d \
    --env 'DB_NAME=gitlabhq_production' \
    --env 'DB_USER=gitlab' --env 'DB_PASS=password' \
    --env 'DB_EXTENSION=pg_trgm' \
    --volume /home/kai/gitlab/postgres:/var/lib/postgresql \
    sameersbn/postgresql:9.6-2

docker run --name gitlab-redis -d \
        -v /home/kai/Documents/services_env/gitlab/redis:/var/lib/redis \
        sameersbn/redis:latest

docker run --name gitlab-instance -d \
        -e 'GITLAB_HOST=gitlab.local.bridgewell.com' \
        -e 'GITLAB_SSH_PORT=10022' \
        -e 'GITLAB_PORT=10080' \
        -e 'DB_USER=gitlab' -e 'DB_PASS=pasword' \
        -e 'DB_NAME=gitlabhq_production' \
        -e 'GITLAB_SECRETS_DB_KEY_BASE=LONG-STRING-I-JUST-SKIP-IT' \
        -e 'GITLAB_SECRETS_SECRET_KEY_BASE=LONG-STRING-I-JUST-SKIP-IT' \
        -e 'GITLAB_SECRETS_OTP_KEY_BASE=LONG-STRING-I-JUST-SKIP-IT' \
        -e 'SMTP_HOST=mail.test.com' \
        -e 'SMTP_ENABLED=true' \
        -e 'SMTP_PORT=25' \
        -e 'SMTP_STARTTLS=false' \
        --link gitlab-postgresql:postgresql \
        --link gitlab-redis:redisio \
        -p 10080:80 -p 10022:22 \
        -v /home/kai/gitlab/data:/home/git/data \
        sameersbn/gitlab:9.0.5

Everything works perfectly! Has anyone encountered the same problem?