To facilitate my move to Kubernetes for this and future websites/applications, I needed to containerize this site.

I use Gitlab CI so this post will cover building a Docker container and pushing it to a registry.

Dockerize

Since this is a static site that is generated by CI when a new commit is merged to master, it is straightforward to Dockerize. Previously this site was hosted on a VPS with nginx serving the staic pages so I decided to use the nginx base image for this container. I simply need to copy the static site that is generated by the build in _site into the correct directory.

Following the directions on the nginx Docker Hub page, my Dockerfile looks as below:

FROM nginx:latest

COPY _site /usr/share/nginx/html

Gitlab CI

Currently my Gitlab CI has a Docker runner, so I use Kaniko to build Docker containers without having to give privileged mode as Docker in Docker would require.

The containerize stage in my Gitlab CI config looks as below:

containerize:
  stage: containerize
  variables:
    CI_REGISTRY: index.docker.io
    CI_REGISTRY_IMAGE: andrewtchin/andrewtchin.com
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/devops/Dockerfile --destination $CI_REGISTRY_IMAGE:latest

To allow Kaniko to push the image to Docker Hub, the registry credentials are set from Environment Variables set in Gitlab CI. The Dockerfile is in the devops directory and the resulting image is tagged as andrewtchin/andrewtchin.com:latest and pushed.

Conclusion

After the image is pushed to Docker Hub, it is ready to be used in a Kubernetes Deployment which will be described in a future post.