In this article you will learn how to containerize a static website using nginx. Then we will push a private docker image in dockerhub. Finally we will use this private image to be pulled in our Kubernetes cluster ! I will split this article to 2 small and easy steps, you can skip any one you want !
# Containerize a static website and push it on dockerhub
In this Section we will choose a template from random websites that provides free css templates, then we will dockerize it !
# Dockerize the website
I’ll choose this template from this link
Download it and let’s create our Dockerfile !
We will use the nginx:alpine
image and copy all the assets of the website to the /usr/share/nginx/html
to be hosted by the nginx webserver.
1 | FROM nginx:alpine |
Now it’s time to build the container image
1 | raf@4n6nk8s:~$ sudo docker build . -t <user_name>/cars-app |
Now let’s create a container to test it before make push it!
1 | raf@4n6nk8s:~$ sudo docker run --name car-demo -p 8686:80 <user_name>/cars-app |
Sanity Check please ! Oh everything is OK !
# Push the container image to private repo
Now go and create a private repository in your dockerhub to push it !
1 | raf@4n6nk8s:~$ sudo docker image push <user_name>/cars-app |
Now it’s time to deal with our kubernetes cluster !
# Use the private docker image in Kubernetes
In this demo I will use a production kubernetes cluster with 1 master node and 1 worker node
1 | raf@4n6nk8s:~$ kubectl get nodes |
Let’s create a namespace for this demo !
1 | raf@4n6nk8s:~$ kubectl create ns private-docker |
To make our cluster pull private images we need to create a special secret object with specific type called docker-registry
secret. To make this secret work correctly you must specify the docker registry, username , password and docker email!
I prefer to put this params in variable environment to work with it easly
1 | export EMAIL=<email> |
In case you will use dockerhub as your registry you don’t have to specify the server !
1 | raf@4n6nk8s:~$ kubectl create -n private-docker secret docker-registry docker-secret --docker-username=$USERNAME --docker-password=$PASSWORD --docker-email=$EMAIL |
Now let’s create a pod with the image that we made it !
1 | apiVersion: v1 |
If you try create this pod with this YAML definition you’ll get a ErrImagePull
because we don’t specify the docker secret that we created
1 | raf@4n6nk8s:~$ kubectl get pods -n private-docker |
Let’s figure out the problem with kubectl describe
command. Take a look at the events that occur when the pod try to pull the container image!
1 | Events: |
Specifying the docker secret in the pod YAML definition will solve this problem. The pod will pull the container image without any problem
Adding imagePullSecrets
attributes in the spec
of the pod allow the pod to pull this image. The Final YAML definition will be similar to the following one !
1 | apiVersion: v1 |
Create the pod again and check it with kubectl get pods
and don’t forget to specify the namespace!
1 | raf@4n6nk8s:~$ kubectl get pods -n private-docker |
I want to make sure that everything is ok, so i will expose this pod using the NodePort service. The following service YAML Definition will expose the pod correctly :
1 | apiVersion: v1 |
Let’s now check the service and get the node port that we will use it to test the pod
1 | raf@4n6nk8s:~$ kubectl get svc -n private-docker |
Now you can visit either http://master:31651 or http://worker1:31651 and you will find the static website work without any problem!