Introduction
If I am running some software, written by me or someone else, on a server, such as a DigitalOcean droplet, the aim is to always deploy it via docker. On my server, I want a single large docker compose file that has everything I want running.
This gives the benefit that nothing special needs to be installed/configured on the server other than installing docker, which most providers provide pre-baked server images for - so you can just assume docker is already present on the system.
Even my own apps, such as my api run as docker containers but before a container can be run an image needs to be built and pushed to a registry somewhere that the server which will pull it can reach it.
In this post I will walkthrough how I have my self hosted docker registry setup, where images are pushed to automatically as I push code to main on github.
Components
- A server with docker installed on it
- Docker registry will be run as a container on a server
- Nginx on the same server as a proxy to handle tls and forward requests to the registry container
- Project pom file to build and push image using the Jib Maven plugin
- Github actions ci pipeline to build and push docker image on commit to main
And a DNS, such as Cloudflare, that will provide routing to the server
A server with docker installed on it
This can be any server that you have running around. I use a DigitalOcean droplet that comes preinstalled with Docker on it. You can use my referral link to get $200 in credits to try it out https://m.do.co/c/b5f565690240
The droplet with docker installed on it can be created with one curl request [replace the token with your auth token]
1 |
curl -X POST -H 'Content-Type: application/json' \ |
Docker registry will be run as a container on a server
The docker registry runs as a docker container itself. Setup a docker compose file which will have our container specifications
The registry can be run using the following yaml.
1 |
registry: |
The registry has the registry folder mounted so that the data persists after container restarts. The password file registry.password
contains the auth information and the /data
folder will contain the image bytes. The path to these folders are then passed to the container as environment variables REGISTRY_AUTH_HTPASSWD_PATH
and REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY
respectively
The authentication file is created using htpasswd
using the command htpasswd -Bc <output-file-name> <wanted-username-of-registry>
this will then prompt for a password after which the auth information will be dropped on disk as a file.
Nginx on the same server as a proxy to handle tls and forward requests to the registry container
Nginx is also running as a docker container and is part of the docker compose file. The yaml for it is as follows
1 |
nginx: |
The cert and key pem files are for TLS - I use the ones that Cloudflare provides for my domain
The nginx.conf file has a server block that listens on the domain and forwards request to the registry docker container on port 5000
1 |
upstream registry { |
run docker-compose up -d
to have a registry and nginx proxy running in the background
Project pom file to build and push image using the Jib Maven plugin
I use Jib, which is a maven plugin to build and push my docker images. The main advantages are that it doesn’t require the docker daemon nor does it need hand written dockerfiles
Add a plugin tag to configure jib
1 |
<plugin> |
To compile the application, build and push the docker image run mvn -T32 clean compile package jib:build -DtagSha=$(git rev-parse HEAD)
This will push the image tagged with the sha of the commit so you know exactly what code was used to build the image.
Github actions ci pipeline to build and push docker image on commit to main
To automate the process above, a github actions pipeline can be setup which will push an image on every commit to main. In the app folder create .github/workflows/main-ci.yaml
with the following content
1 |
name: main-ci |
Now on every commit to a main a new image will automatically be built and pushed to the registry.
The tags pushed can be seen by making a request on https://registry.xyz.io/v2/awesome-app/tags/list
Conclusion
Point your DNS to the IP of the server and you should be good to go
Once the image is pushed then all that is left is to update the sha of the app’s compose file and restart to get the new version running
from Hacker News https://ift.tt/7Gre9x4
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.