Docker/C4/Advanced-Docker/English

From Script | Spoken-Tutorial
Jump to: navigation, search
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,
  • 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

To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04
  • Docker version 27.0.2
Show Slide:

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

  • 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 tutorials 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.

requirements.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.


ENTRYPOINT ["/app/start_script.sh"] Here we are using python3 as the Entry point.

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 and press Enter.

We can see python interactive shell.

This means that our docker image is running perfectly.

Press ctrl + D to exit the shell.

Main.py is the python file . Now we will test by using the main.py file.

Type the command as shown and press Enter.


It will return an output saying This is a test python code.

This shows that the docker image is successfully running.

cursor on terminal 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

  • 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

sudo xhost +local:docker

Switch back to the terminal and

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 and press Enter.

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, as 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.

This is how we can work with the python scripts using this docker image.
Slides:

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

This brings us to the end of this tutorial. Let us summarize.

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

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.
Show 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.

Contributors and Content Editors

Madhurig, Nirmala Venkat