Deploy an image from a GitHub Packages registry
- Skill required
- DevOps Proficiency
At some point a simple subscription membership site is not enough. If you are deploying custom business logic for your application as a server-side compoment, you will have to:
- Build a Docker image for your application
- Upload the image to a Docker registry
- Deploy an image to a DjaoApp website
In this tutorial, we will walk through the steps of building a Docker image, uploading it to a GitHub Packages registry and deploying from that registry to a DjaoApp Website.
Build a Docker image for your application
The requirements to deploy a Docker container are minimal. We need to:
- Run the Webserver on port 80 in the container
- Write the logs on stdout and stderr
Let's look at a Dockerfile example
...
# Bundle app source
COPY . /app/reps/app_name
WORKDIR /app/reps/app_name
RUN /app/bin/pip install -r requirements.txt
# Expose application http port
Expose 80
# Run
CMD ["/app/bin/gunicorn", "-c", "/etc/djaopsp/gunicorn.conf", "djaopsp.wsgi"]
We have a Dockerfile, so let's build the image and run it locally to verify it responds to HTTP requests as expected.
$ docker build -t app_name .
Successfully built IMAGE_ID
$ docker run -d -p 8000:80 -t app_name
$ wget http://localhost:8000/
If all goes well, you should see messages from the Docker container that the HTTP server is listening on port 80 and the HTTP requests are being processed correctly.
$ docker ps
CONTAINER IMAGE
...
CONTAINER_ID IMAGE_ID
...
$ docker logs CONTAINER_ID
...
[INFO] Listening at: http://0.0.0.0:80 (1)
...
172.17.0.1 localhost:8000 "GET / HTTP/1.1" 200 15403 "-" "Wget/1.14 (linux-gnu)"
Sometimes you will have to connect to the container and poke around to check the correct files were installed as expected. To connect to a running container, you can use the following command.
$ docker exec -it CONTAINER_ID /bin/bash
Upload the image to a Docker registry
At this point we are ready to push the image to a Docker registry. Since the code repository is on GitHub, we will pick GitHub Packages as a registry.
We first create a personal access token (classic) on GitHub by going to Settings > Developer settings > Personal access tokens > Tokens (classic), then click on Generate new token. We add a note and select write:packages.
We safely save write down the generated token, later referenced as token.
At this point we authenticate with the Docker registry (GitHub Packages here).
$ export GITHUB_TOKEN=*token*
$ echo $GITHUB_TOKEN | docker login ghcr.io -u USERNAME --password-stdin
Login Succeeded
Then we push the container image we previously built to the Docker registry.
$ docker push ghcr.io/NAMESPACE/IMAGE_NAME:latest
We then verify the package is present on GitHub Packages by browsing to the Packages section of the repository on GitHub. Great!
Deploy an image to a DjaoApp website
We verified the package is present on GitHub Packages by browsing to the Packages section of the repository on GitHub. We browse to the Image page and note the container image location.
We browse to the DjaoDjin Control Panel for the website and enter the information required under the App panel. We need:
-
- A container location
- i.e. where the Docker image has been uploaded (ex: ghcr.io/djaodjin/djaopsp/livedemo:main).
-
- Authentication credentials for the Docker registry
- (Since our image is public, there is no to enter credentials here.)
-
- Runtime Environment variables
- (if any)
After we get the message that the container was deployed successfuly, we browse to the DjaoApp Website (i.e. https://subdomain.djaoapp.com if we did not set a custom domain name for the Website). Since we tested the Docker image locally earlier, we expect to see the same page, now on a public URL.
- Need help?
- Contact us
- Curious how it is built?
- Visit us on GitHub