README.md (3000B)
1 # Example 10 — Build & Publish Docker Images 2 3 This Jaypore CI example builds a production Docker image for the Django 4 project, runs the test suite inside the container, and optionally pushes the 5 image to a Docker registry. 6 7 | Step | What it does | 8 |------|--------------| 9 | 1 | **Build** the Docker image, tagged with the commit SHA and `latest`. | 10 | 2 | **Test** — run `manage.py test` inside the freshly built container. | 11 | 3 | **Inspect** — save `docker inspect` output and image size info. | 12 | 4 | **Push** — if `DOCKER_REGISTRY` is set, log in and push both tags. | 13 14 ## Dockerfile 15 16 The included `Dockerfile` creates a slim production image: 17 18 - **Base** — `python:3.12-slim` 19 - **Dependencies** — installs `requirements.txt` plus `gunicorn` 20 - **App code** — copies `manage.py`, `mysite/`, `core/`, and `setup.cfg` 21 - **Entrypoint** — runs Gunicorn on port 8000 with 2 workers 22 23 ## Artifacts produced 24 25 | File | Description | 26 |------|-------------| 27 | `docker-build.log` | Full `docker build` output. | 28 | `docker-test.log` | Test suite output from inside the container. | 29 | `image-inspect.json` | `docker inspect` metadata for the built image. | 30 | `image-info.txt` | Image repository, tag, ID, and size. | 31 | `docker-push.log` | Registry push output (only when pushing). | 32 33 ## Environment variables 34 35 | Variable | Required | Description | 36 |----------|----------|-------------| 37 | `DOCKER_REGISTRY` | No | Registry hostname (e.g. `registry.example.com`). If unset, the image is built and tested locally but not pushed. | 38 | `DOCKER_USERNAME` | No | Username for `docker login`. | 39 | `DOCKER_PASSWORD` | No | Password / token for `docker login`. | 40 41 ## Project layout assumed 42 43 ``` 44 your-repo/ 45 ├── manage.py 46 ├── requirements.txt 47 ├── setup.cfg 48 ├── mysite/ 49 │ └── settings.py 50 ├── core/ 51 └── 10-build-publish-docker/ 52 ├── Dockerfile 53 └── .jci/ 54 └── run.sh # ← this script 55 ``` 56 57 ## How to use 58 59 1. Copy the `.jci/` directory and `Dockerfile` into your repository: 60 61 ```bash 62 cp -r 10-build-publish-docker/.jci /path/to/your-repo/.jci 63 cp 10-build-publish-docker/Dockerfile /path/to/your-repo/Dockerfile 64 ``` 65 66 2. Make sure Docker is available on the CI runner. 67 68 3. Run Jaypore CI: 69 70 ```bash 71 git jci run 72 ``` 73 74 To also push to a registry, export the environment variables first: 75 76 ```bash 77 export DOCKER_REGISTRY=registry.example.com 78 export DOCKER_USERNAME=deploy-bot 79 export DOCKER_PASSWORD=secret-token 80 git jci run 81 ``` 82 83 ## Customisation 84 85 - **Image name** — change `IMAGE_NAME` in `run.sh` to match your project. 86 - **Build context** — adjust the `-f` flag and build context path if your 87 `Dockerfile` lives elsewhere. 88 - **Multi-platform builds** — replace `docker build` with `docker buildx build 89 --platform linux/amd64,linux/arm64` for multi-arch images. 90 - **Gunicorn workers** — edit the `CMD` in `Dockerfile` or set the 91 `WEB_CONCURRENCY` environment variable at runtime.