0% found this document useful (0 votes)
30 views34 pages

Unit 5 - Dockers

Uploaded by

21z207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views34 pages

Unit 5 - Dockers

Uploaded by

21z207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CLOUD COMPUTING INFRASTRUCTURE:

Introduction to Dockers
Deploying, maintaining and scaling containerized
applications using cloud infrastructure
Run a container
➢ Docker is an open platform for developing, shipping, and running applications.

➢ With Docker, you can separate your applications from your infrastructure and treat your
infrastructure like a managed application.

➢ Docker helps you ship code faster, test faster, deploy faster, and shorten the cycle between
writing code and running code.

➢ Docker does this by combining kernel containerization features with workflows and tooling
that helps you manage and deploy your applications.

➢ Docker containers can be directly used in Kubernetes, which allows them to be run in the
Kubernetes Engine with ease.

➢ Open up Cloud Shell and enter the following command to run a hello world container to get
started: docker run hello-world
➢ This simple container returns Hello from Docker! to your screen.

➢ While the command is simple, notice in the output the number of steps it performed.

➢ The docker daemon searched for the hello-world image, didn't find the image locally, pulled
the image from a public registry called Docker Hub, created a container from that image, and
ran the container for you.
➢ This is the image pulled from the Docker Hub public registry. The Image ID is in SHA256 hash
format—this field specifies the Docker image that's been provisioned.

➢ When the docker daemon can't find an image locally, it will by default search the public
registry for the image.

➢ Let's run the container again: docker run hello-world

➢ Notice the second time you run this, the docker daemon finds the image in your local registry
and runs the container from that image.

➢ It doesn't have to pull the image from Docker Hub.

➢ Finally, look at the running containers by running the following command: docker ps
➢ There are no running containers.

➢ The hello-world containers you ran previously already existed. In order to see all containers,
including ones that have finished executing, run docker ps -a
Build a Docker image
➢ Step 1 - create and switch into a folder named test mkdir test && cd test.
➢ Step 2 - Create a Dockerfile:
cat > Dockerfile <<EOF
# 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"]
EOF
This file instructs the Docker daemon on how to build your image.

The initial line specifies the base parent image, which in this case is the official Docker image for node
version 6.
In the second, we set the working (current) directory of the container.
In the third, we add the current directory's contents (indicated by the "." ) into the container.
Expose the container's port so it can accept connections on that port and finally run the node command to
start the application
➢ Step 3 - write the node application, and after that build the image
cat > app.js <<EOF This is a simple HTTP server that listens on port 80 and returns "Hello World".
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\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();
});
EOF
➢ Step 4 - let's build the image: docker build -t node-app:0.1 .

➢ The -t is to name and tag an image with the name:tag syntax.

➢ The name of the image is node-app and the tag is 0.1.

➢ The tag is highly recommended when building Docker images.


➢ Step 5 - run the following command to look at the images you built: docker images

node is the base image and node-app is the image you built.
Run Containers based on
Docker image built
➢ Step 1 - run containers based on the image you built.

➢ Server running at http://0.0.0.0:80/

➢ The --name flag allows you to name the container if you like.

➢ The -p instructs Docker to map the host's port 4000 to the container's port 80.

➢ Now you can reach the server at http://localhost:4000.

➢ Without port mapping, you would not be able to reach the container at localhost
➢ Step 2 - Close the initial terminal and then run the following command to stop and remove
the container: docker stop my-app && docker rm my-app docker ps

➢ Notice the container is running in the output of docker ps.

➢ You can look at the logs by executing docker logs [container_id].


➢ You don't have to write the entire container ID, as long as the initial characters uniquely
identify the container. For example
Modify the application ands
Run Container
➢ Step 1 - Edit app.js with a text editor of your choice (for example nano or vim) and replace
"Hello World" with another string.
Step 2 – . In your Cloud Shell, open the test directory you created earlier in the lab: cd test

➢ Build this new image and tag it with 0.2: docker build -t node-app:0.2 .
Step 3 - Run another container with the new image version. Notice how we map the host's
port 8080 instead of 80. We can't use host port 4000 because it's already in use.
docker run -p 8080:80 --name my-app-2 -d node-app:0.2
docker ps

You might also like