Docker/C2/Building-Docker-Images/English

From Script | Spoken-Tutorial
Revision as of 16:06, 20 January 2025 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show slide:

Welcome

Hello and welcome to the Spoken Tutorial on “Building Docker Images”.
Show Slide:
Learning Objectives
In this tutorial, we will learn about
  • Building Docker images
  • Pushing images to Docker Hub
  • Image layers and size optimization
Show Slide:
System Requirements
To record this tutorial,I am using
  • Ubuntu Linux OS 22.04 and
  • Docker version 27.0.2
Show Slide:
To follow this tutorial,
  • You must have basic knowledge of using Linux terminal
  • For pre-requisite Linux tutorials, please visit this website
Show slide:
Code files
  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show Slide:
These are the steps for building Docker images.
  • Write a Dockerfile and required code files
  • Build the Docker image
  • Run the Docker image
  • Push to Docker Hub
  • Pull and run the image on the target machine
Show Slide:
Building Docker Image Process Flowchart
In this tutorial, we'll build an Express web server using a custom Docker image.
This flowchart explains the process of building a Docker image.
Show Slide:
Hover over Dockerfile block
To build a Docker image, we need a Dockerfile which is a text document.
Show Slide:
Building Docker Image Process Flowchart
The folder express underscore api contains app dot js file for the application.
Show Slide:
Building Docker Image Process Flowchart
The image will be built using docker build command.
The image will run the application code.
We need a nodejs environment for running the Express web server.
We will use the official Node js image from Docker Hub as the base image.
Show Slide:
Building Docker Image Process Flowchart
After the image is built, docker run command is used to run the image.
A new container will be created based on the image.
It will execute the commands and run the Express server on our Linux OS.
Only narration Let us see how to implement this.
Only narration
Point to Downloads folder
I have downloaded the code files and saved it in my Downloads folder.
Open Downloads directory in file manager.
We can see Dockerfile and express underscore api folder.
Let us open the Dockerfile in the text editor.
Only narration Here,we have a set of commands required to create an image.
Highlight FROM node:latest
This command will pull the latest official Nodejs image from Docker Hub.
This is the base image for the application.
Highlight RUN Here, RUN keyword executes the command specified after it.
Highlight mkdir /home/app This command creates an app directory in the image’s file system.
Highlight COPY ./express_api /home/app COPY command copies the contents of the express api folder into the app directory.
Highlight WORKDIR /home/app WORKDIR command makes app directory as our working directory
Highlight RUN npm init -y && npm install express --save This command installs the required Node js packages for our application.
Highlight CMD [ "node", "app.js" ] The CMD command runs the Node js application.
Only narration Next let us look into the code of our application.
Switch to Downloads directory in File Manager
Open app.js
In the express underscore api folder, open the app dot js file in a text editor.
Only narration. This application uses the Express framework to create a web API.
Highlight const express = require('express'); First we bring in the Express tool to help build the server using this command.
Highlight const app = express(); Then, this command line creates the main app to handle requests and responses.
Highlight app.use(express.json()); This command line allows the app to understand data sent in JSON format.
Highlight const PORT = 3000;
This command line set the port number to 3000.
The application will listen for incoming HTTP requests on this port.
Highlight app.get('/', async (req, res) => {
res.send("Express server is running");
});
When someone opens the main page, this command listens for their request.
It thens ends back the message, Express server is running.
Highlight app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}`);
});
app dot listen command displays the messagein the terminal after the image runs.
It indicatest hat the server has started at the given site.
Only narration Now let us build the image.
Open a terminal by pressing the keys Ctrl, Alt and T simultaneously. Open a terminal by pressing the keys Ctrl, Alt and T simultaneously.
Type cd ~/Downloads and press Enter Change to the directory where you have downloaded the code files.
Type sudo docker build -t node-express:v1 .
To build the image, type the command as shown.
The flag hyphen t tags the image with a name.
In this case, the image will be tagged as node hyphen express with version 1.
If version is not specifically mentioned, latest will be given by default.
The dot represents the path of the current directory.
Press Enter and enter the password when prompted Press Enter.
Highlight the output We can see that the building of the node hyphen express image is finished.
Type sudo docker images and press Enter To show a list of images, type the command as shown and press Enter.
Highlight the node-express row
We can see our image, node hyphen express in the list.
Also|, we can see details about its Tag, Image ID, creation date, and size.
Tag identifies a specific version of the image.
In this case, we have given it as v1.
Our image is now successfully built.
Type sudo docker run -it --rm -p 3000:3000 node-express:v1 and press Enter
Now, let us run the image.
Type the commandas shown.
Highlight -it Hyphen it attachesa n interactive terminal session.
Highlight --rm
hyphen hyphen rm flag automatically removes the container once it exits.
It helps in cleaning up resources.
Highlight -p 3000:3000 This option maps port 3000 of the host to port 3000 of the container.
Only narration Press Enter.
Highlight the output We can see that the server has started.
Go to http://localhost:3000 Open the browser and enter the given link.
Highlight Express server is running The "Express server is running" message confirms the image is working.
Only narration
To stop the running image container, open a new terminal session.
You can do so by clicking on the plus symbol on the top panel of the terminal.
You can close the previous terminal session by clicking on the cross symbol.
Type sudo docker ps and press Enter
Enter the command as shown.
Enter the password when prompted.
This will retrieve the list of running containers.
Highlight node-express:v1 row
We can see the container of our image running.
Copy the container id.
Type sudo docker stop <Container_id> and press Enter
To stop the container, we will use docker stop command.
Type sudo space docker space stop space and paste the container id.
Press Enter.
Highlight the output We will get the container id of the stopped container as the output.
Only narration Now let us see how to push the image to Docker Hub.
Show Slide:
Pushing Images to Docker Hub
To push a Docker image to Docker Hub, we use the push command.
Before pushing, we need to tag the image.
It needs to be done with our Docker Hub username and the repository name.
Only narration Let us see the execution of it.
Docker hub website :

https://hub.docker.com/

Go to the web browser, and enter the URL as shown.
Create your account by signing up, or if it already exists then sign in.
On the top left corner, click the list icon and choose the repositories section.
The repositories section lists repositories, each containing Docker images.
Type sudo docker login and press Enter
Switch to the terminal.
Close the previous terminal session and use a new one.
Type the command as shown.
Using this command, we will log into our Docker Hub account.
Then we can easily pull and push images to it.
Press Enter
Enter the credentials for Docker Hub when prompted.
Highlight Login Succeeded We can see that our login has succeeded.
Type sudo docker tag node-express:v1 stuser1/node-express and press Enter
Before pushing, we need to tag the image with username.
To tag the image, enter the command as shown.
This creates a reference to the image with the specified tag.
Here, stuser1 is my username.
Substitute it with your username.
Highlight stuser1/node-express
Stuser1 forward slash node hyphen express will be our repository name
It will be assigned after pushing the image to Docker Hub.
Type sudo docker push stuser1/node-express and press Enter Next push the image to Docker Hub by typing the command as shown.
Highlight the output This indicates that our tagged image is successfully pushed to Docker Hub.
Go to Docker Hub website and refresh the page. Then we will go to Docker Hub website again and refresh the page.
Hover over stuser1/node-express We can see our image in our repository indicating it is successfully pushed.
Only narration
Now let us tryto pull the image back from the repository.
For that, firstl et us delete the image from our local system.
Type sudo docker rmi stuser1/node-express and press Enter
To remove the image, we will use docker rmi command.
Enter the command as shown.
Highlight the output We can see that the image is untagged, that is removed from the system.
Type sudo docker images and press Enter
To verify, enter the command as shown to get the list of images.
We can see that the tagged image is no longer in the list.
Type sudo docker pull stuser1/node-express and press Enter
Now let us pull the image from Docker Hub.
For that, type the command as shown and press Enter.
Highlight the output We can see that the image is successfully pulled and downloaded.
Only narration Now let us move forward to image layers and size optimization.
Show Slide:
Image Layers and Size Optimization
Docker images are built in layers.
Each Dockerfile instruction that changes the filesystem, represents a layer.
To optimize image size, the number of layers are reduced.
Only narration Nowl et us see how this actually works.
Type sudo docker images grep node-express and press Enter
Switch to the terminal.
First, we will check the size of our image.
Type the command as shown.
Highlight grep node-express
I have extended the command with pipe spacegrep space image name.
We don’t need to specifyany version or tag of the image.
It filters the image list and retrieves details for the specified image name only.
Press Enter Press Enter.
Highlight the output
We can see that the size of our imageis 1.13GB.
This size may be different for you.
Type cd ~/Downloads and press Enter Make sure you are in the directory where you have downloaded the code files.
Type sudo nano Dockerfile and press Enter
Let us go to Dockerfile, using the nano Dockerfile command.
Enter the password when prompted.
Only narration
In this file, there area total of six instructions.
However, WORKDIR and CMD do not create new filesystem layers.
Thus , the Docker image will have four layers from the Dockerfile instructions.
Highlight node:latest
Here the latest version of the node base image is used.
To reduce the size, we can use a smaller base image.
Replace node:latest with node:slim
Replace the word latest and type slim.
Then save by pressing Ctrl+S and exit by pressing Ctrl+X.
In terminal, type sudo docker build -t node-express:v1 . and press Enter
Again build the image.
Type the command as shown and press Enter.
Highlight the output The image is built successfully.
Type sudo docker images grep node-express and press Enter
Nowlet us check the size of the image.
Enter the commandas shown.
Highlight the output
We can see that now our image size is reduced to 232MB.
This size may be different for you.
Show Slide:
Summary
This brings us to the end of this tutorial. Let us summarize.
In this tutorial, we have learnt about
  • Building Docker images
  • Pushing images to Docker Hub
  • Image layers and size optimization
Show Slide:
Assignment
As an assignment, please do the following:
  • Modify the image size by using alpine variant base image.
  • Compare the sizes before and after modifying the base image.
Show slide:
Assignment Observation
The size of the imageis optimized to 160MB.
This size may be different for you.
Show Slide:
About Spoken Tutorial project
The video at the following link summarizes the Spoken Tutorial project.
Please download and watch it.
Show Slide:
Spoken Tutorial Workshops
The Spoken Tutorial Project teamconducts workshops and gives certificates.
For more details,please write to us.
Show Slide:
Answers for THIS Spoken Tutorial
Please post your timed queries in this forum.
Show Slide:
FOSSEE Forum
For any generalor technical questions on docker, visit the FOSSEE forumand post your question.
Show slide:
Acknowledgement
Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Slide:
Thankyou
This is Pranjal Mahajan, a FOSSEE Semester Long Intern 2024, IIT Bombay signing off
Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat