|
| 1 | +# Defining ImagePullSecrets for Coder workspaces |
| 2 | + |
| 3 | +<div> |
| 4 | + <a href="https://github.com/ericpaulsen" style="text-decoration: none; color: inherit;"> |
| 5 | + <span style="vertical-align:middle;">Your Name</span> |
| 6 | + <img src="https://github.com/ericpaulsen.png" width="24px" height="24px" style="vertical-align:middle; margin: 0px;"/> |
| 7 | + </a> |
| 8 | +</div> |
| 9 | +January 12, 2024 |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +Coder workspaces are commonly run as Kubernetes pods. When run inside of an |
| 14 | +enterprise, the pod image is typically pulled from a private image registry. |
| 15 | +This guide walks through creating an ImagePullSecret to use for authenticating |
| 16 | +to your registry, and defining it in your workspace template. |
| 17 | + |
| 18 | +## 1. Create Docker Config JSON File |
| 19 | + |
| 20 | +Create a Docker configuration JSON file containing your registry credentials. |
| 21 | +Replace `<your-registry>`, `<your-username>`, and `<your-password>` with your |
| 22 | +actual Docker registry URL, username, and password. |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "auths": { |
| 27 | + "<your-registry>": { |
| 28 | + "username": "<your-username>", |
| 29 | + "password": "<your-password>" |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## 2. Create Kubernetes Secret |
| 36 | + |
| 37 | +Run the below `kubectl` command in the K8s cluster where you intend to run your |
| 38 | +Coder workspaces: |
| 39 | + |
| 40 | +```console |
| 41 | +kubectl create secret generic regcred \ |
| 42 | + --from-file=.dockerconfigjson=<path-to-docker-config.json> \ |
| 43 | + --type=kubernetes.io/dockerconfigjson \ |
| 44 | + --namespace=<workspaces-namespace> |
| 45 | +``` |
| 46 | + |
| 47 | +Inspect the secret to confirm its contents: |
| 48 | + |
| 49 | +```console |
| 50 | +kubectl get secret -n <workspaces-namespace> regcred --output="jsonpath={.data.\.dockerconfigjson}" | base64 --decode |
| 51 | +``` |
| 52 | + |
| 53 | +The output should look similar to this: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "auths": { |
| 58 | + "your.private.registry.com": { |
| 59 | + "username": "ericpaulsen", |
| 60 | + "password": "xxxx", |
| 61 | + "auth": "c3R...zE2" |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## 3. Define ImagePullSecret in Terraform template |
| 68 | + |
| 69 | +```hcl |
| 70 | +resource "kubernetes_pod" "dev" { |
| 71 | + metadata { |
| 72 | + # this must be the same namespace where workspaces will be deployed |
| 73 | + namespace = "workspaces-namespace" |
| 74 | + } |
| 75 | +
|
| 76 | + spec { |
| 77 | + image_pull_secrets { |
| 78 | + name = "regcred" |
| 79 | + } |
| 80 | + container { |
| 81 | + name = "dev" |
| 82 | + image = "your-image:latest" |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
0 commit comments