Automatically deploy docker containers to a server after you commit to GitHub
After I push to GitHub, I have a GitHub action that automatically creates a docker package. My goal was to create a system that would automatically deploy my docker container after the main branch of my project had been pushed to.
– Ubuntu Server >=17
– Webhook ( https://github.com/adnanh/webhook )
Things you should have prepared:
– Server IP address
– Project Name
– Github action to automatically create a package when you push to master or however else you define it (https://gist.github.com/ARezaK/ad0697302915f34c53224239d293bc7d)
Setup webhook on github
- Go to your Github project repository -> settings -> webhooks -> add webhook
- Create a new webhook.
- Use the payload URL as your
- Click “Let me select individual events” -> Click “Packages” and uncheck everything else
- Click Save
Setup webhook on your server
sudo apt-get install webhook. I found out using the apt-get method that it already creates a service for me which you can see with
systemctl status webhook. You won’t see this on the documentation page of the project repo. Use this command to restart the service
sudo systemctl restart webhookif you make changes.
- Using the status command above you can see that the service is using a file called /
etc/webhook.confThat is the equivalent of your
hooks.jsonwhich you will see mentioned in the project repo.
- I also went to and edited the file
/lib/systemd/system/webhook.serviceto add the
-verbosetag so now it looks like this
Description=Small server for creating HTTP endpoints (hooks)
ExecStart=/usr/bin/webhook -nopanic -hooks /etc/webhook.conf -verbose
Below is what my webhook.conf looks for my 2 projects. Adjust it to your liking.
“response-message”: “Deploy Project_name”
“response-message”: “Deploy Project_name_2”
Go to your root folder and create a folder called webhooks. This is where you will place your bash files
docker pull ghcr.io/
docker system prune -f
docker run –name=“
” -d –restart always -e “PORT=8765” -e “DEBUG=1” -p 8012:8765 ghcr.io/ :main
with their respective names being deploy_project_name.sh and deploy_project_name_2.sh
Once you have created those you should be all set. Now whenever your package is built on GitHub it should be automatically deployed on your server. You can always manually deploy by going to the url (http://server_ip_address:9000/hooks/deploy_Project_name)