Difference between revisions of "Docker/C4/Advanced-Docker/English"
(Created page with "<div style="margin-left:1.27cm;margin-right:0cm;"></div> {| border="1" |- || '''Visual Cue''' || '''Narration''' |- |- | style="background-color:transparent;border-top:0.5pt...") |
|||
Line 1: | Line 1: | ||
− | + | ||
{| border="1" | {| border="1" | ||
|- | |- | ||
Line 5: | Line 5: | ||
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
− | + | ||
− | | | + | || Show Slide: |
'''Title Slide''' | '''Title Slide''' | ||
− | | | + | || Hello and welcome to the Spoken Tutorial on “'''Advanced Docker”.''' |
|- | |- | ||
− | | | + | || Show Slide: |
'''Learning Objectives''' | '''Learning Objectives''' | ||
− | + | ||
− | | | + | || In this tutorial, we will learn how to, |
− | * | + | * Write a dockerfile for python automation environment configurations. |
− | * | + | * Build a docker image from the dockerfile |
− | * | + | * Run containers using the docker image |
− | * | + | * Test the docker image to run python automation script |
|- | |- | ||
− | | | + | || Show Slide: |
'''System Requirements''' | '''System Requirements''' | ||
− | | | + | || To record this tutorial, I am using |
− | * | + | * '''Ubuntu Linux '''OS version '''22.04''' |
− | * | + | * '''Docker version 27.0.2''' |
|- | |- | ||
− | | | + | || Show Slide: |
'''Prerequisite''' | '''Prerequisite''' | ||
− | | | + | || To follow this tutorial, |
− | * | + | * You must have basic knowledge of using docker commands |
− | * | + | * For pre-requisite docker tutorials, please visit this website |
|- | |- | ||
− | | | + | || Show slide: |
'''Code files''' | '''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 files in the Downloads folder. |
− | | | + | || I have created the Code files required for this tutorial and saved it in my '''Downloads''' folder. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Now we will have a look at each of the files available over here. | |
|- | |- | ||
− | | | + | || Open '''Dockerfile''' |
+ | ||The '''Dockerfile''' contains a set of instructions to create the Docker image. | ||
− | + | It will set up the python environment required for automation inside docker image. | |
− | + | ||
|- | |- | ||
− | | | + | || Open '''main.py''' |
− | | | + | || '''main.py''' file used to test if the Docker image is working correctly. |
|- | |- | ||
− | | | + | || Open '''requirements.txt''' |
+ | || '''requirements.txt''' file contains various packages to run the automation scripts of python. | ||
+ | |- | ||
+ | ||Open '''start_script.sh ''' | ||
− | + | '''Highlight according to narration.''' | |
+ | * Usage: docker run my-python-app <filename> output, | ||
− | | | + | ||'''start_script.sh''' is a bash script that runs every time when the container starts. |
− | + | The script runs the specified Python file and returns the output. | |
|- | |- | ||
− | | | + | || Open '''Checker.py''' |
− | | | + | ||'''checker.py''' is the python file that we will test at the end. |
− | + | The code checks if a given phrase has any spelling or grammatical mistakes. | |
|- | |- | ||
− | | | + | ||Open Chatbot.py |
− | + | QnA_base.json | |
− | | | + | || '''Chatbot.py and QnA_base.json files ''' are used to check the chatter through text. |
|- | |- | ||
− | | | + | || Open a '''Dockerfike''' in '''text editor''' |
− | | | + | || Let us open the '''Dockerfile''' in the text editor. |
|- | |- | ||
− | | | + | ||Only narration |
− | + | ||
− | + | ||
− | + | '''Highlight Command''' | |
− | + | ||
− | + | '''FROM ubuntu:22.04''' | |
+ | || First, python:3.12.1 will be downloaded from the docker hub and used as a parent image. | ||
|- | |- | ||
− | | | + | ||'''ENV DEBIAN_FRONTEND=noninteractive''' |
− | + | '''ENV TZ=Etc/UTC''' | |
− | | | + | || We set environment variables to configure time-zone data non-interactively. |
|- | |- | ||
− | | | + | ||'''RUN apt-get update && apt-get install -y \''' |
− | |||
− | |||
− | + | || Run command will update the repositories. | |
− | + | It will install the basic packages required for automation. | |
|- | |- | ||
− | | | + | || '''RUN python3 -m pip install --upgrade pip''' |
− | | | + | || This''' '''command will upgrade pip to the latest version. |
|- | |- | ||
− | | | + | ||'''RUN espeak --version && \''' |
− | + | '''java -version && \''' | |
− | + | '''python3 --version''' | |
− | | | + | || This command verify the installation of espeak, java and python |
|- | |- | ||
− | | | + | || '''WORKDIR /app''' |
− | | | + | || This command sets the '''app''' as the working directory in the container. |
|- | |- | ||
− | | | + | || '''COPY requirements.txt /app''' |
− | | | + | ||Copy the requirements file into the '''app''' directory of the container. |
− | + | '''requirement.txt '''contains the name of the packages. | |
|- | |- | ||
− | | | + | || '''RUN pip install --no-cache-dir -r requirements.txt''' |
− | | | + | || Next it will install all the packages specified in '''requirements.txt''' |
|- | |- | ||
− | | | + | ||'''COPY . /app''' |
− | + | '''RUN chmod +x /app/start_script.sh''' | |
− | | | + | ||Then copy all the files into the container. |
− | + | '''chmod''' will set execute permission to '''start_script.sh''' | |
|- | |- | ||
− | | | + | || '''ENTRYPOINT ["/app/start_script.sh"]''' |
− | | | + | ||Next define the entrypoint command. |
− | + | Here we are using python3 as the Entry point file. | |
− | + | Close the''' text Editor.''' | |
|- | |- | ||
− | | | + | || |
− | | | + | || Now, let's build the Docker image using the Dockerfile we just created. |
|- | |- | ||
− | | | + | || '''Terminal''' window opens |
− | + | '''Type command ''' | |
− | + | '''docker build -t python_automation .''' | |
− | |||
− | + | || Open the terminal. | |
− | + | Change to the '''Downloads''' folder where the code files are available. | |
− | + | The commands used in this tutorial are available in the '''commands.txt''' file in the code file. | |
− | + | Copy and paste the commands wherever it is required. | |
− | + | Type the command as shown and press Enter. | |
− | + | This command builds the Docker image and tags it as '''python_automation'''. | |
− | + | ||
− | + | It will take sometime to build the image. Wait until it completes the process. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | || Type command '''docker images''' |
Highlight image ID | Highlight image ID | ||
− | | | + | || Once the build is complete, the running process will stop. |
To verify the created Docker image, type '''docker space images'''. | To verify the created Docker image, type '''docker space images'''. | ||
Line 193: | Line 174: | ||
You will see the '''python_automation''' with its corresponding image ID in the list. | You will see the '''python_automation''' with its corresponding image ID in the list. | ||
|- | |- | ||
− | | | + | || In the '''terminal''' |
'''docker run -it python_automation''' | '''docker run -it python_automation''' | ||
− | | | + | || Next, let's run a container using the created image. |
Type '''docker space run space hyphen it space python underscore automation''' | Type '''docker space run space hyphen it space python underscore automation''' | ||
Line 206: | Line 187: | ||
Press''' ctrl + d''' to exit the '''shell.''' | Press''' ctrl + d''' to exit the '''shell.''' | ||
|- | |- | ||
− | | | + | || |
− | + | || Now we will test by using the '''main.py''' file. | |
− | | | + | |
Type the command as shown. | Type the command as shown. | ||
Line 219: | Line 199: | ||
|- | |- | ||
− | | | + | || |
− | | | + | || Now let us test the python automation file. |
− | + | ||
− | Now let us test the python automation file. | + | |
We have a '''checker.py''' script which will check any spelling or grammatical mistakes. | We have a '''checker.py''' script which will check any spelling or grammatical mistakes. | ||
Line 228: | Line 206: | ||
It requires a GUI to enter the text and display the output. | It requires a GUI to enter the text and display the output. | ||
|- | |- | ||
− | | | + | || '''Slide:''' |
'''Set up X11 forwarding''' | '''Set up X11 forwarding''' | ||
− | | | + | || |
− | * | + | * To run '''GUI applications''' inside a '''Docker container''', you need to set up '''X11 forwarding'''. |
− | * | + | * It runs applications on a remote server and displays them on your local machine. |
|- | |- | ||
− | | | + | || '''Type''' |
− | + | ||
− | '''Type''' | + | |
'''sudo xhost +local:docker''' | '''sudo xhost +local:docker''' | ||
− | | | + | || Switch back to the terminal. |
Type '''sudo space xhost space +local:docke'''r | Type '''sudo space xhost space +local:docke'''r | ||
Line 249: | Line 225: | ||
It allows '''Xserver''' for Docker on the local connection. | It allows '''Xserver''' for Docker on the local connection. | ||
|- | |- | ||
− | | | + | || Type command |
− | | | + | || Next, run the container with X11 forwarding enabled. |
Type the following command as shown. | Type the following command as shown. | ||
Line 259: | Line 235: | ||
It also mounts the '''Checker.py''' script into the container. | It also mounts the '''Checker.py''' script into the container. | ||
− | It uses '''tkinter''', a | + | It uses '''tkinter''', a standard Python interface to the Tk GUI toolkit. |
If the container starts successfully a '''Spelling and Grammar Checker''' window will appear. | If the container starts successfully a '''Spelling and Grammar Checker''' window will appear. | ||
Line 277: | Line 253: | ||
Close the window. | Close the window. | ||
|- | |- | ||
− | | | + | || Narration only |
Show the code file | Show the code file | ||
Line 283: | Line 259: | ||
'''chabot.py '''& '''QnA_base.json''' | '''chabot.py '''& '''QnA_base.json''' | ||
− | | | + | || Next, we will run a chatbot script. |
Here you can see two files '''chabot.py '''& '''QnA_base.json''' | Here you can see two files '''chabot.py '''& '''QnA_base.json''' | ||
Line 295: | Line 271: | ||
It learns new answers from the user, and updates the database accordingly'''.''' | It learns new answers from the user, and updates the database accordingly'''.''' | ||
|- | |- | ||
− | | | + | || '''docker run -it -v $PWD/:/app/ python_automation Chatbot.py''' |
− | | | + | || Switch back to the terminal. |
To run this python script using docker image, type the command as shown | To run this python script using docker image, type the command as shown | ||
Line 306: | Line 282: | ||
Press '''Enter''' to run the image | Press '''Enter''' to run the image | ||
|- | |- | ||
− | | | + | || '''Terminal''' |
'''Type ''' | '''Type ''' | ||
'''Hello''' | '''Hello''' | ||
− | | | + | || If the image ran successfully you can see the message from chatbot |
Saying '''Hello! I'm here to help you with your questions. Type 'quit' to exit.''' | Saying '''Hello! I'm here to help you with your questions. Type 'quit' to exit.''' | ||
Line 325: | Line 301: | ||
Type ‘quit’ to exit the chatbot.n | Type ‘quit’ to exit the chatbot.n | ||
|- | |- | ||
− | | | + | || |
− | | | + | || This is how we can work with the python scripts using this docker image. |
|- | |- | ||
− | | | + | || '''Slides:''' |
'''Features of docker images''' | '''Features of docker images''' | ||
− | | | + | || |
− | * | + | * Ensures the script runs consistently across different systems and environments |
− | * | + | * Prevents conflicts by isolating the script from the host system |
− | * | + | * Easily move and run the script on any Docker-compatible platform |
− | * | + | * Packages all dependencies, avoiding manual installations and version conflicts |
− | * | + | * Containers limit access, reducing potential security risks to the host system |
|- | |- | ||
− | | | + | || Show Slide: |
'''Summary''' | '''Summary''' | ||
− | | | + | || This brings us to the end of this tutorial. Let us summarize. |
In this tutorial, we have learnt how to | In this tutorial, we have learnt how to | ||
− | * | + | * Write a dockerfile for python automation environment configurations. |
− | * | + | * Build a docker image from the docker file |
− | * | + | * Run containers using the docker image |
− | * | + | * Test the docker image to run python automation script |
|- | |- | ||
− | | | + | || Show Slide: |
− | | | + | |
+ | '''About Spoken Tutorial project''' | ||
+ | || The video at the following link summarizes the '''Spoken Tutorial project'''. | ||
Please download and watch it | Please download and watch it | ||
|- | |- | ||
− | | | + | || Show Slide: |
− | | | + | |
+ | '''Spoken Tutorial Workshops''' | ||
+ | || '''Spoken Tutorial Project'''conducts workshops and gives certificates. | ||
For more details, please write to us. | For more details, please write to us. | ||
|- | |- | ||
− | | | + | || Show Slide: |
'''Answers for THIS Spoken Tutorial''' | '''Answers for THIS Spoken Tutorial''' | ||
− | | | + | || Please post your timed queries in this forum. |
|- | |- | ||
− | | | + | || Show Slide: |
− | | | + | |
+ | '''FOSSEE Forum''' | ||
+ | || For any general or technical questions on '''Docker''', visit the FOSSEE forum and post your question. | ||
|- | |- | ||
− | | | + | || Slide: '''Acknowledgement''' |
− | | | + | || Spoken Tutorial Project was established by the Ministry of Education, Government of India |
|- | |- | ||
− | | | + | || Show slide: |
'''Thank You''' | '''Thank You''' | ||
− | | | + | || This is Aditya Kushwaha, a FOSSEE Semester long intern 2024, IIT Bombay, signing off. |
− | + | ||
− | This is Aditya Kushwaha, a FOSSEE Semester long intern 2024, IIT Bombay, signing off. | + | |
Thanks for joining. | Thanks for joining. | ||
|- | |- | ||
|} | |} |
Revision as of 11:46, 11 March 2025
Visual Cue | Narration |
Show Slide:
Title Slide |
Hello and welcome to the Spoken Tutorial on “Advanced Docker”. |
Show Slide:
Learning Objectives
|
In this tutorial, we will learn how to,
|
Show Slide:
System Requirements |
To record this tutorial, I am using
|
Show Slide:
Prerequisite |
To follow this tutorial,
|
Show slide:
Code files |
|
Show files in the Downloads folder. | I have created the Code files required for this tutorial and saved it in my Downloads folder.
Now we will have a look at each of the files available over here. |
Open Dockerfile | The Dockerfile contains a set of instructions to create the Docker image.
It will set up the python environment required for automation inside docker image. |
Open main.py | main.py file used to test if the Docker image is working correctly. |
Open requirements.txt | requirements.txt file contains various packages to run the automation scripts of python. |
Open start_script.sh
Highlight according to narration.
|
start_script.sh is a bash script that runs every time when the container starts.
The script runs the specified Python file and returns the output. |
Open Checker.py | checker.py is the python file that we will test at the end.
The code checks if a given phrase has any spelling or grammatical mistakes. |
Open Chatbot.py
QnA_base.json |
Chatbot.py and QnA_base.json files are used to check the chatter through text. |
Open a Dockerfike in text editor | Let us open the Dockerfile in the text editor. |
Only narration
Highlight Command FROM ubuntu:22.04 |
First, python:3.12.1 will be downloaded from the docker hub and used as a parent image. |
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC |
We set environment variables to configure time-zone data non-interactively. |
RUN apt-get update && apt-get install -y \
|
Run command will update the repositories.
It will install the basic packages required for automation. |
RUN python3 -m pip install --upgrade pip | This command will upgrade pip to the latest version. |
RUN espeak --version && \
java -version && \ python3 --version |
This command verify the installation of espeak, java and python |
WORKDIR /app | This command sets the app as the working directory in the container. |
COPY requirements.txt /app | Copy the requirements file into the app directory of the container.
requirement.txt contains the name of the packages. |
RUN pip install --no-cache-dir -r requirements.txt | Next it will install all the packages specified in requirements.txt |
COPY . /app
RUN chmod +x /app/start_script.sh |
Then copy all the files into the container.
chmod will set execute permission to start_script.sh |
ENTRYPOINT ["/app/start_script.sh"] | Next define the entrypoint command.
Here we are using python3 as the Entry point file. Close the text Editor. |
Now, let's build the Docker image using the Dockerfile we just created. | |
Terminal window opens
Type command docker build -t python_automation .
|
Open the terminal.
Change to the Downloads folder where the code files are available. The commands used in this tutorial are available in the commands.txt file in the code file. Copy and paste the commands wherever it is required. Type the command as shown and press Enter. This command builds the Docker image and tags it as python_automation. It will take sometime to build the image. Wait until it completes the process. |
Type command docker images
Highlight image ID |
Once the build is complete, the running process will stop.
To verify the created Docker image, type docker space images. This command lists all the Docker images on your system. You will see the python_automation with its corresponding image ID in the list. |
In the terminal
docker run -it python_automation |
Next, let's run a container using the created image.
Type docker space run space hyphen it space python underscore automation We can see python interactive shell This means that our docker image is running perfectly Press ctrl + d to exit the shell. |
Now we will test by using the main.py file.
Type the command as shown. Main.py is the python file . It will return an output saying This is a test python code. This shows that the docker image is successfully running. | |
Now let us test the python automation file.
We have a checker.py script which will check any spelling or grammatical mistakes. It requires a GUI to enter the text and display the output. | |
Slide:
Set up X11 forwarding |
|
Type
sudo xhost +local:docker |
Switch back to the terminal.
Type sudo space xhost space +local:docker and press Enter. It allows Xserver for Docker on the local connection. |
Type command | Next, run the container with X11 forwarding enabled.
Type the following command as shown. This command mounts the X11 socket and sets the DISPLAY variable to enable GUI forwarding. It also mounts the Checker.py script into the container. It uses tkinter, a standard Python interface to the Tk GUI toolkit. If the container starts successfully a Spelling and Grammar Checker window will appear. You can test the checker by typing a mis-spelled word. I will type the sentence Docker is amazing with a spelling mistake in the word amzing Now click on the Check button. It will show the spelling error returning the wrong spelling. Correct the spelling and check it once more. Now no errors were found. Close the window. |
Narration only
Show the code file chabot.py & QnA_base.json |
Next, we will run a chatbot script.
Here you can see two files chabot.py & QnA_base.json QnA_base.json file acts as the database for the ChatBot. It has question-answer pairs stored in a dictionary format. This chatot.py script finds the closest matching question. It learns new answers from the user, and updates the database accordingly. |
docker run -it -v $PWD/:/app/ python_automation Chatbot.py | Switch back to the terminal.
To run this python script using docker image, type the command as shown Here the hyphen ti flag enables interactive shell We need this to interact with chatbot Press Enter to run the image |
Terminal
Type Hello |
If the image ran successfully you can see the message from chatbot
Saying Hello! I'm here to help you with your questions. Type 'quit' to exit. Your question: Now you can type your question Here I will type Hello and press Enter You will get a response Bot says: hey there. Type ‘quit’ to exit the chatbot.n |
This is how we can work with the python scripts using this docker image. | |
Slides:
Features of docker images |
|
Show Slide:
Summary |
This brings us to the end of this tutorial. Let us summarize.
In this tutorial, we have learnt how to
|
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 |
Spoken Tutorial Projectconducts 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 general or technical questions on Docker, visit the FOSSEE forum and post your question. |
Slide: Acknowledgement | Spoken Tutorial Project was established by the Ministry of Education, Government of India |
Show slide:
Thank You |
This is Aditya Kushwaha, a FOSSEE Semester long intern 2024, IIT Bombay, signing off.
Thanks for joining. |