Hi Folks !
Kubernetes is an container orchestration system by Google. I’m planning to have series of blog posts around this topic covering introduction to Docker, creation and configuring docker images and containers and deploying full fledged application to Kubernetes.
Docker Basics:
First released in 2013 and it is an open platform for developing, deploying and running application knows as containers. With help of docker you can treat your infrastructure as managed application. Kubernetes had strong support for docker.
To begin I will go through the following artifacts:
- Build and run the docker containers
- Pull the docker images from Google container or Docker Hub
- Push docker images to Google container registry i.e. gcr
Lets login to the cloud console and open the integrated cloud terminal. Now execute following command
docker images
It will list down all docker images, as of now its showing nothing because we haven’t created one.
docker run hello-world
Yes right, we not yet developed any application and still we are executing command. Perfectly fine, docker first search the hello-world in local repository and if not found any then will fetch from Docker Hub repo. now execute command
docker images
Bingo! It will show you the following output
docker ps : displays list of running containers
Though we ran docker hello-world its now showing in output because the container runs and finish. To find such containers we use
docker ps -a : displays all containers including the one that have finished executing
the container id is UUID and if we don’t specify container name docker sets a random one. Its good practice to specify the container name as below
docker run --name [container-name] hello-world
Next, I will show you how to build docker image based on node application for the same create a Dockerfile. Create a directory and I used vi editor to create Dockerfile
Dockerfile
# Use an official Node runtime as the parent image FROM node:6 # Set the working directory in the container to /app WORKDIR /app # Copy the current directory contents into the container at /app ADD . /app # Make the container's port 80 available to the outside world EXPOSE 80 # Run app.js using node when the container launches CMD ["node", "app.js"]
App.js
const http = require('http'); const hostname = '0.0.0.0'; const port = 80; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World - Kubernetes\n'); }); server.listen(port, hostname, () => { console.log('Server running at http://%s:%s/', hostname, port); }); process.on('SIGINT', function() { console.log('Caught interrupt signal and will exit'); process.exit(); });
To create docker image execute below command from same location:
docker build -t myapp:V1.0 .
Initially it will take few minute sto build image. The -t option specify the name:tag pair. Its good practice to give tag to build artifact. Verify the image creation by below command and The output will show you the name as repository and tag version-
docker images
Next RUN the docker image as container and the command is
docker run -p 4000:80 --name myimg myapp:V1.0
but to run docker image as background service use
docker run -p 4000:80 --name <any-name-to-run-container> -d <image:tag> docker run -p 4000:80 --name myimg1 -d myapp:V1.0
The attribute -p map the port 4000 to http port 80. Now visit http://localhost:4000 or curl http://localhost:4000. You should see hello world – Kubernetes as output.
To stop and remove docker image
docker stop myimg1 && docker rm myimg1
To view docker logs
docker logs [Container ID]
Now modify the app.js again and build the docker image
docker build -t myapp:V1.1
then again run the docker container as below
docker run -p 5000:80 --name myimg2 -d myapp:V1.1
Now check output as follows
If you notice we have two containers running with different versions of the applications at same time. That’s the power of containerization.
Now we are going to publish image to Google Container Registry – GCR. To push image to private registry hosted by gcr , you need to tag image with registry name.
[hostname]/[project-id]/[image]:[tag]
where:
- hostname: gcr.io (or asia.gcr.io or eu.gcr.io etc )
- project-id: your project id
- [image]: your docker image
- tag: any string of your choice. by default – latest
find active project id using
gcloud config list project
Now, Tag the local image with the registry name by using the command
docker tag [SOURCE_IMAGE] [HOSTNAME]/[PROJECT-ID]/[IMAGE]:[TAG]
e.g.
docker tag viappimg:img1 gcr.io/prj-docker/viappimg:tagimg1
push tag image to container registry
gcloud docker -- push [HOSTNAME]/[PROJECT-ID]/[IMAGE]:[TAG]
e.g gcloud docker — push gcr.io/prj-docker/viappimg:tagimg1
Check that the image exists in gcr by navigating to Tools > Container Registry or visit: http://gcr.io/%5Bproject-id%5D/angular-gcp-app
Now to run the docker by command but before that stop and remove all containers and images using docker rmi <image-name> and below command
docker stop $(docker ps -q) && docker rm $(docker ps -aq)
Now we have a fresh-environment so pull the docker image from registry
gcloud docker -- pull gcr.io/[project-id]/myapp:V1.1
if face any issue now a days you can use gcloud auth configure-docker as credential helper then use docker as you would for non-gcr
run the docker and test using curl as below
docker run -p 5000:80 -d gcr.io/[project-id]/myapp:V1.1
curl http://localhost:5000
Cheers !!! You could see the output.
Happy Coding !!!
One thought on “Docker Introduction: Google Cloud”