In this post, we will take our new Dockerfile and layers, and build the images, and push them up to a registry, so we can easily use them in Kubernetes.
Table of contents
Building our images, and pushing them into a registry
First thing that needs to happen before we can move into Kubernetes, is to build our Docker images containing everything, and ship those to a Container Registry where Kubernetes can reach them
Docker hub offers free registries, but only 1 private repo.
For our use case we are going to use Gitlab.
It makes it easy to build CI/CD pipelines, as well as has a really nice registry for our images to be stored securely.
Creating the Registry
We need to create a new registry in Gitlab.
You'll need a new repository first.
Once you have created one, go to Packages & Registries > Container Registry, and you'll see instructions on how to login, and get the url for your container registry
In my case this is registry.gitlab.com/laravel-in-kubernetes/laravel-app
Login to the registry
Depending on whether you have 2 factor auth enabled, you might need to generate credentials for your local machine.
You can create a pair in Settings > Repository > Deploy Tokens, and use these as a username and password to login to the registry. The Deploy Token needs write access to the registry.
$ docker login registry.gitlab.com -u [username] -p [token] Login Succeeded
Building our images
We now need to build our application images, and tag them to our registry.
In order to do this we need to point at the specific stage we need to build, and tag it with a name.
$ docker build . -t registry.gitlab.com/[your_repository]/cli:v0.0.1 --target cli $ docker build . -t registry.gitlab.com/[your_repository]/fpm_server:v0.0.1 --target fpm_server $ docker build . -t registry.gitlab.com/[your_repository]/web_server:v0.0.1 --target web_server $ docker build . -t registry.gitlab.com/[your_repository]/cron:v0.0.1 --target cron
Pushing our images
Next we need to push our images to our new registry to be used with Kubernetes
$ docker push registry.gitlab.com/[your_repository]/cli:v0.0.1 $ docker push registry.gitlab.com/[your_repository]/fpm_server:v0.0.1 $ docker push registry.gitlab.com/[your_repository]/web_server:v0.0.1 $ docker push registry.gitlab.com/[your_repository]/cron:v0.0.1
Our images are now available inside the registry, and ready to be used in Kubernetes.
Onto the next
Next we will setup our Kubernetes Cluster where we will run our images.