- A remote server (I am using Digital Ocean)
- A Gitlab Account (for container registry)
- Create a droplet (ubuntu) on Digital Ocean and choose your suitable authentication method (ssh or password)
- log into the droplet using root and go to second step
- Add a new user with sudo privilages [see
user_add.sh
] - logout and log into with the new user
- enable and setup firewall for ssh, http, https [see
firewall_setup.sh
]' - Install
nginx
on that VPS [seenginx_install.sh
]
- You can follow official docker installation guide for ubuntu
- follow this guide to run docker without sudo
- You have to create image for your application which you want to deploy (
Dockerfile
I mean) - as dockerhub allows only one private repository, we will use Gitlab projects as our registry (We can use public registry if our repository is public)
- Now we have to pull those images and start the container based on those images
- Build and Test the docker image
docker login registry.gitlab.com
docker build registry.gitlab.com/<gitlab_username>/<project_name>
docker run registry.gitlab.com/<gitlab_username>/<project_name>
(run with required parameters which you need)docker push registry.gitlab.com/<gitlab_username>/<project_name>
- Now we have to pull those images on our VPS and run and also expose those application to the internet
- Login to the docker registry and pull required images
- You have to also setup nginx to load balance the traffic to the coorect backend service
- first you have to create a
<app_name>.nginx.conf
file at/etc/nginx/conf.d
[Seeexample-app.nginx.conf
] - Update the nginx configuration at
/etc/nginx/sites-available/default
[Seeserver.nginx.conf
]
- now create a bash script which will sync your docker images; pull new image and run the new container and delete the old running container
- this script will take application name as input and based on the application name it will check the avilable system port which is allocated to that application during step 6 (<app_name>.nginx.conf)
- this script will start the process for our auto-deployment if there is no running containers or our system restarted
- we have to register a service which will run this script when system restarted
- Registering
deployment.service
[see `deployment.service]$ sudo cp deployment.service /etc/systemd/system/deployment.service $ sudo chmod 644 /etc/systemd/system/deployment.service $ sudo systemctl start deployment $ sudo systemctl status deployment $ sudo systemctl enable deployment
- Now restart your system using
sudo reboot
- it will automatically install all the images and start all your applications
- DONE!! 🎉
- You can visit this repo to understand how to integrate this feature with github actions and setup auto deployment