← World

AWS EC2 Docker Hosting a GitLab Container

←Blog ·

I want to setup an instance of GitLab to host my git repositories on my Amazon Cloud (AWS), to be available securely over the internet and backed up.

There's a few options:
  • Install it along side another existing EC2 instance running mail, web, etc..
  • Install it on it's own fresh instance using the provided deb package.
  • Install it on an instance using Docker.
  • Install it on an Elastic Beanstalk application using Docker.
After contemplating the pros and cons of each, I decided to give the last option a try. Mainly that GitLab requires Ruby, Redis, Postgre, Nginx and even tries to install Postfix. I was hoping to set it up along side my email server, but the package install would destroy my existing config, and I'd have existing mysql database, and apache web server not setup to work out of the box with GitLab. So for best results I think it makes sense not to try jamming them together and use the power of virtual machines.

For the benefit of documenting my experience; I spent quite a bit of time trying out the Elastic Beanstalk hoping that it would magically scale and monitor my docker environment, but after trying to wrap my head around the tool, I don't think it was going to be useful at my current scale. Also, it took me a while, and I was surprised to also find AWS's EC2 Container Service in preview, which might be just what I'd like.

Until then, I ended up going with option C. Even after everything I'm not certain that docker is going to be the jesus package manager, but with so much commotion, it's worth looking at I guess.

I'm going to quickly / briefly fill in what I ended up doing. It might not all be there, but I can come back and update if needed.

It also turns out that most EC2 instances don't have any swap space by default, so that's a problem for all of us memory hogs. I followed this helpful tut to get me going like follows:

# check for free space on an instance storage device that wont incur i/o costs
$ sudo df -h

# give ourselves 1G of space
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
$ sudo chown root:root /swapfile
$ sudo chmod 0600 /swapfile

# include a line in fstab so that it works on reboot
$ sudo vim /etc/fstab
/swapfile swap swap defaults 0 0

# see your space
$ sudo free -k
$ sudo df -h
Back to my setup. There's EC2 instances out there from CoreOS that look pretty cool for clustering, but I think I want to go the other way, stepping up from small to larger instances docking as many containers as possible on a single instance. So to go the vanilla route, I decided to just boot up a t2.micro ec2 instance with amazon's linux image, per the docker instructions.

$ sudo yum update
$ sudo yum install -y docker ; sudo service docker start

http://doc.gitlab.com/ce/install/requirements.html

https://gitlab.com/gitlab-org/gitlab-ce/tree/master/docker

$ sudo yum install git
$ git clone https://gitlab.com/gitlab-org/gitlab-ce.git ; cd gitlab-ce

# Build the source into an image and setup data volume
$ sudo docker build --tag gitlab_image docker/
$ sudo docker run --name gitlab_data gitlab_image /bin/true

# Run the container with our data and ports, with auto restart enabled on restart
$ sudo docker run --restart=always --detach --name gitlab_app --publish 80:80 --publish 22:22 --publish 443:443 --volumes-from gitlab_data gitlab_image
$ sudo docker logs -f gitlab_app
$ sudo docker run -ti -e TERM=linux --rm --volumes-from gitlab_data ubuntu
root@abc:/# sudo apt-get install vim -y
root@abc:/# sudo vim /etc/gitlab/gitlab.rb
external_url "https://git.yourdomain.com/";

# Prevent Postgres from trying to allocate 25% of total memory
postgresql['shared_buffers'] = '1MB'

# Configure GitLab to redirect PostgreSQL logs to the data volume
postgresql['log_directory'] = '/var/log/gitlab/postgresql'

# Some configuration of GitLab
tion
gitlab_rails['gitlab_email_from'] = 'noreply@yourdomainwefav.com'
gitlab_rails['gitlab_support_email'] = 'noreply@yourdomainwefav.com'
gitlab_rails['time_zone'] = 'America/Los_Angeles'

# SMTP settings
# You must use an external server, the Docker container does not install an SMTP server
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "mail.yourdomain.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "noreply@yourdomain.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_domain'] = "yourdomain.com"
gitlab_rails['smtp_authentication'] = "plain"
gitlab_rails['smtp_enable_starttls_auto'] = true

# Install SSL certs
# copy files into /etc/gitlab/ssl/ with files including your domain named in gitlab.rb

root@abc:/# sudo mkdir /etc/gitlab/ssl
root@abc:/# sudo vim /etc/gitlab/ssl/git.yourdomain.com.key
root@abc:/# sudo vim /etc/gitlab/ssl/git.yourdomain.com.crt

# Rebuild gitlab configs
$ sudo docker restart gitlab_app # just restart the container

# If you need to change run configuration
$ sudo docker rm -f gitlab_app # doesn't delete data volumes
$ sudo docker run --restart=always --detach --name gitlab_app --publish 80:80 --publish 22:22 --publish 443:443 --volumes-from gitlab_data gitlab_image

So now voila! A home for my code. I can host code repos, with issue tracking and snippets! If you need a hosted repo for a project, just let me know.