|
| 1 | +# Description |
| 2 | +Copy-Paste Ready™ how-to for using private [Gitlab Container Registry](https://docs.gitlab.com/ce/user/project/container_registry.html) with [Kubernetes](https://kubernetes.io). |
| 3 | + |
| 4 | +#### Create env file with credentials |
| 5 | + |
| 6 | +```bash |
| 7 | +cat <<EOF >dockercfg.env |
| 8 | +export DOCKER_REGISTRY_SERVER=<registry_url> |
| 9 | +export DOCKER_USER=<username> |
| 10 | +export DOCKER_PASSWORD=<password> |
| 11 | +export DOCKER_EMAIL=<email> |
| 12 | +export DOCKER_IMAGE_PATH=<image_path_with_tag> |
| 13 | +EOF |
| 14 | +``` |
| 15 | + |
| 16 | +#### Edit accordingly |
| 17 | +See `dockercfg.env.example` |
| 18 | + |
| 19 | +#### Export additional variables |
| 20 | +```bash |
| 21 | +export REGISTRY_NAME=`echo $DOCKER_REGISTRY_SERVER | sed -e 's/^http:\/\///g' -e 's/^https:\/\///g'` |
| 22 | +export DOCKER_IMAGE_FULL_PATH=$REGISTRY_NAME/$DOCKER_IMAGE_PATH |
| 23 | +``` |
| 24 | + |
| 25 | +#### Create secret in the Kubernetes Cluster |
| 26 | +```bash |
| 27 | +kubectl create secret docker-registry gitlab-registry \ |
| 28 | + --docker-server=$DOCKER_REGISTRY_SERVER \ |
| 29 | + --docker-username=$DOCKER_USER \ |
| 30 | + --docker-password=$DOCKER_PASSWORD \ |
| 31 | + --docker-email=$DOCKER_EMAIL |
| 32 | +``` |
| 33 | + |
| 34 | +#### Test |
| 35 | +```bash |
| 36 | +cat <<EOF | kubectl apply -f - |
| 37 | +apiVersion: v1 |
| 38 | +kind: Pod |
| 39 | +metadata: |
| 40 | + name: foo |
| 41 | +spec: |
| 42 | + containers: |
| 43 | + - name: foo |
| 44 | + image: $DOCKER_IMAGE_FULL_PATH |
| 45 | + imagePullPolicy: Always |
| 46 | + imagePullSecrets: |
| 47 | + - name: gitlab-registry |
| 48 | +EOF |
| 49 | +``` |
| 50 | + |
| 51 | +#### Check if the image was pulled successfully |
| 52 | +```bash |
| 53 | +kubectl describe po/foo | grep -i pull |
| 54 | +``` |
| 55 | + |
| 56 | +### Cleanup |
| 57 | +#### Remove pod |
| 58 | +```bash |
| 59 | +kubectl delete po/foo |
| 60 | +``` |
| 61 | +#### Remove file with environment variables |
| 62 | +```bash |
| 63 | +rm dockercfg.env |
| 64 | +``` |
| 65 | +#### Unset environment variables |
| 66 | +```bash |
| 67 | +unset DOCKER_REGISTRY_SERVER |
| 68 | +unset DOCKER_USER |
| 69 | +unset DOCKER_EMAIL |
| 70 | +unset DOCKER_PASSWORD |
| 71 | +unset DOCKER_IMAGE_PATH |
| 72 | +unset REGISTRY_NAME |
| 73 | +unset DOCKER_IMAGE_FULL_PATH |
| 74 | +``` |
0 commit comments