Skip to content

Instantly share code, notes, and snippets.

@baamenabar
Last active December 9, 2021 21:30
Show Gist options
  • Save baamenabar/47ddfea96ebf20d0355f4fbcf2e47123 to your computer and use it in GitHub Desktop.
Save baamenabar/47ddfea96ebf20d0355f4fbcf2e47123 to your computer and use it in GitHub Desktop.
Installing Docker, setting it up for Jenkins, and installing Jenkins.
#!/bin/bash
# Copied from: https://raw.githubusercontent.com/wardviaene/jenkins-course/master/scripts/install_jenkins.sh
# Updated with : https://docs.docker.com/install/linux/docker-ce/ubuntu/
# this script is only tested on ubuntu xenial and bionic
# sudo is only if you are not on root (you should not be on root)
# Uninstall previous versions of docker
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
# Install docker using the repository
## Install dependencies
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
## Add the docker gpg key (should be 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88)
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# verification command
sudo apt-key fingerprint 0EBFCD88
# add the docker-ce repo
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# update again (no idea why) and install docker
sudo apt-get update && sudo apt-get install -y docker-ce
# add current user to the docker group
sudo usermod -aG docker $USER
# Reload the user
## At this point it is adviced to:
## log out and then back in
## ...so the new permisions are available, making all the following sudo calls in the docker commands not necessary
# Allow jenkins to build docker images
## Expose docker socket to run docker from inside the container
## Explanations here:
### https://getintodevops.com/blog/the-simple-way-to-run-docker-in-docker-for-ci
### https://github.com/wardviaene/jenkins-course/blob/master/scripts/install_jenkins.sh
### https://jenkins.io/doc/book/installing/#installing-docker
# create jenkins home dir to mount
mkdir -p /var/jenkins_home
chown -R 1000:1000 /var/jenkins_home/
# this command below (uncomment to use) pulls and runs a jenkins image mounting the docker socket inside the container
# docker run -p 8080:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -d --name jenkins jenkins/jenkins:lts
# this command below pulls and runs a jenkinsci/blueocean image mounting the docker socket inside the container
docker run \
-u root \
--rm \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v /var/jenkins_home/:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
--name jenkins-blueocean \
jenkinsci/blueocean
# show endpoint
echo 'Jenkins installed'
echo 'You should now be able to access jenkins at: http://'$(curl -s ifconfig.co)':8080'
#!/bin/bash
# copied from: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04
# update apt and install nginx
sudo apt update
sudo apt install nginx
# List modes to be alowed
sudo ufw app list
# for now use por 80
sudo ufw allow 'Nginx HTTP'
# get a list of rules
sudo ufw status
# if disabled enable the ufw firewall (when prompted, answer yes)
sudo ufw enable
# chack that nginx is running
systemctl status nginx
# get the IPs of the server and check via browser that it is working
# Warning! black magic below
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
# stop nginx
sudo systemctl stop nginx
# start nginx
sudo systemctl start nginx
# restart nginx
sudo systemctl restart nginx
# reload configs in nginx
sudo systemctl reload nginx
# disabling and enabling auto start when booting the system
sudo systemctl disable nginx
sudo systemctl enable nginx
# create a folder for a sub-domain (must already be being sent to the server from a DNS server)
sudo mkdir -p /var/www/jenkins.medula.cl/html
# change user and premisions
sudo chown -R $USER:$USER /var/www/jenkins.medula.cl/html
sudo chmod -R 755 /var/www/jenkins.medula.cl/html
# create new block config (paste from the subdomain_nginx_config file)
sudo nano /etc/nginx/sites-available/jenkins.medula.cl
# or download it from here and edit:
sudo wget --output-document=/etc/nginx/sites-available/jenkins.medula.cl https://gist.githubusercontent.com/baamenabar/47ddfea96ebf20d0355f4fbcf2e47123/raw/845d95766965bf0a3f78757de579d21f5854046a/subdomain_nginx_config
# link the config to sies-enabled
sudo ln -s /etc/nginx/sites-available/jenkins.medula.cl /etc/nginx/sites-enabled/
# edit the nginxconf bucket size (no idea what that means)
sudo vim /etc/nginx/nginx.conf
### Uncomment the line with server_names_hash_bucket_size
# save and close
# verify the config has no syntax errors
sudo nginx -t
# restart
sudo systemctl restart nginx
# for https certificate:
# https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04
# for configuring the reverse proxy for jenkins
# https://www.digitalocean.com/community/tutorials/how-to-configure-jenkins-with-ssl-using-an-nginx-reverse-proxy-on-ubuntu-18-04
server {
listen 80;
listen [::]:80;
root /var/www/jenkins.medula.cl/html;
index index.html index.htm index.nginx-debian.html;
server_name jenkins.medula.cl;
location / {
try_files $uri $uri/ =404;
}
}
@baamenabar
Copy link
Author

VERY goo article about this setup that I found after having done most of the work:

https://itnext.io/setting-up-https-for-jenkins-with-nginx-everything-in-docker-4a118dc29127

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment