Share
Explore

Dockerizing your PYTHON App

megaphone

Getting Started Checklist:

__ Sign up at
__ Create an Account on


Download and install Docker Desktop:

image.png

Sign up for Docker HUB:
How to Sign Up for Docker Hub to Share Docker Containers
To sign up for Docker Hub and start sharing Docker containers, you can follow these steps:
Create a Docker ID:
A Docker ID is required to access Docker Hub and share container images. You can create a Docker ID for free if you don't have one.
To do this, visit the Docker Hub website and sign up for a Docker account

Create a Repository:
After creating a Docker ID, you will need to create a repository on Docker Hub to push your Docker images.
To create a repository, sign in to Docker Hub and select the option to create a repository on the Docker Hub welcome page

Explore Available Images:
Once you have signed up and created a repository, you can explore available images from the community and verified publishers on Docker Hub using your Docker ID
.
Share Your Images:
After completing the above steps, you will be able to share your Docker images on Docker Hub.


By following these steps, you can sign up for Docker Hub and start sharing your Docker containers with the community.

megaphone

This is part of your Project deliverables.

For this Lab, you will take the PYTHON code you have been building up in the Python Network Programming work sheet and wrap it up in a Docker Container.



This Lab guide will teach you how to Dockerize a Python application, starting with a "Hello World" program.
Docker is a popular containerization platform that enables developers to package and deploy applications in a standardized environment.
Working in the Windows PowerShell Terminal:
Open PowerShell as Administrator
cd to the Directory with your PYTHON Router Program -- or -- Make some new difectory for this Experiment.
image.png
In this new working directory, container your PYTHON CODE:
Let’s make a small program to practice Dockering:

image.png

info

Lab Workbook: A comprehensive guide for students to dockerize a Python application in a Windows environment.

Student Lab Learning Guide: Dockerizing Your Python Program on Windows


In this lab, you'll learn how to use Docker, a powerful tool for developing, shipping, and running applications.
We will focus on dockerizing a Python application and running it on Windows using Docker Desktop.
### Prerequisites
- Windows 10 or newer. - Basic knowledge of command line interface (CLI) commands. - A Python program you wish to containerize.
## Step-by-Step Instructions

Step 1: Install Docker Desktop

1. Visit [Docker Hub](https://hub.docker.com/editions/community/docker-ce-desktop-windows). 2. Download Docker Desktop for Windows. 3. Run the `.exe` file and follow the installation prompts. 4. After installation, open Docker Desktop from your Start menu.
*Note: Initial start-up might take a few minutes.*
Step 2: Prepare Your Python Program
1. Ensure your Python program runs correctly locally. 2. Create a new directory for your project. 3. Place your Python script (e.g., `app.py`) in this directory.
Step 3: Handle Dependencies with `requirements.txt`
1. If your program has dependencies, generate a `requirements.txt` file: - In your project directory, activate your Python environment. - Run `pip freeze > requirements.txt` to create the file with all dependencies.
Dependencies are what you pip install.
If you don't have any dependencies for your Python application, you won't need to include a "requirements.txt" file or run the "pip install" command to install any dependencies. This can happen when your application doesn't rely on any external Python packages or libraries.
In such a scenario, you can simply omit the "pip install" instruction from your Dockerfile. The absence of a "requirements.txt" file or the "pip install" command indicates that your application doesn't require any external dependencies to function.
For example → If you have used "pip install wmi" to install the "wmi" package for your Python application, your `requirements.txt` file would typically contain a single line specifying this package.
Here is how your `requirements.txt` file would look like:
```plaintext wmi ```
This simple `requirements.txt` file lists the specific package "wmi" that needs to be installed when the `pip install -r requirements.txt` command is executed within your Docker container.
Each line in the `requirements.txt` file represents a separate Python package to be installed. The Docker PYTHON RUNTIME will do the work of installing these.

Step 4: Create a `Dockerfile`
1. In your project directory, create a file named `Dockerfile`. 2. Edit the `Dockerfile` to include:
```dockerfile # Use an official Python runtime as a parent image FROM python:3.8-slim
# Set the working directory in the container WORKDIR c:\dockerdemo
— or whatever is the name of the directory your Python app code is in —
# Copy the current directory contents into the container COPY . .
# Install any dependencies RUN pip install --no-cache-dir -r requirements.txt
# (Optional) Expose port 80 if your app is a web application EXPOSE 80
# Run your application CMD ["python", "./pythonprogram1.py"]
The "CMD" instruction is not something you would type at the command line. Instead, it forms part of a Dockerfile, which is a set of instructions used to create a Docker image.
Here's what "CMD ["python", "./app.py"]" means in the context of a Dockerfile:
→ This line specifies the command that should be executed when the Docker container is startedwithin the Docker Desktop application — from the containerized image you have provided.

In this case, it indicates that the Python interpreter should run the "app.py" file within the container.

To use this in practice, you would typically create a file named `Dockerfile` containing all the necessary instructions, including the "CMD" instruction, and then build the Docker image using the `docker build` command.
Once the image is built, you can create and start a container from the image using the `docker run` command.
The "CMD" instruction ensures that the specified command is automatically executed when the container is started.
Remember that the Dockerfile is used to:
define the environment and behavior of a Docker container,
the "CMD" instruction is just one part of this definition.

*Note: Adjust the Python version as needed.*

Step 5: Build and Run Your Docker Container
1. Open your CLI Command Line Interface (PowerShell Terminal) and navigate to your project directory.
image.png
2. Build your Docker image: - Run `docker build -t my-python-app .`
3. Run your Docker container: - For non-web apps: `docker run my-python-app` - For web apps: `docker run -p 80:80 my-python-app`
Step 6: Volume Mounting for Development (Optional)
1. To see code changes without rebuilding the image, use volume mounting: - Run `docker run -p 80:80 -v $(pwd):/usr/src/app my-python-app`
### Additional Information
- **Exposing Ports**: Only expose ports necessary for your application (like port 80 for web apps). - **Networking**: For advanced applications requiring inter-container communication, explore Docker networking features. - **Docker Compose**: For multi-container applications, consider learning Docker Compose.
### Conclusion
You've now learned the basics of Dockerizing a Python application on Windows! Explore further to understand more complex Docker functionalities and how they can enhance your development workflow.

image.png

Don’t forget the . at the end of the command!!

image.png

So, myimage is what I upload to docker hub ?

Yes, exactly. myimage is the Docker image you upload to Docker Hub.
Before you do that, you need to tag it with your Docker Hub username and optionally, a version tag.
Here's how you can do it:
Tag the Image: First, you need to tag your local image with your Docker Hub username and repository name.
Suppose your Docker Hub username is username and you want to name your repository repositoryname, you can tag the image like this:
In PowerShell:
docker tag myimage username/repositoryname:tag
image.png

Here, tag is optional and can be used to specify different versions of your image, like 1.0, latest, etc. If you don't specify a tag, Docker will use latest as the default tag.
Login to Docker Hub: Before you push the image, make sure you're logged in to Docker Hub from your command line:
shellCopy code
docker login
This command will prompt you for your Docker Hub username and password.
Push the Image to Docker Hub:

docker push username/repositoryname:tag
image.png
Replace username, repositoryname, and tag with your actual username, repository name, and tag. This command uploads your image to Docker Hub.
Check Docker Hub: After pushing the image, you can go to Docker Hub in your web browser and check your repository. Your new image should be listed there.
Pulling and Using the Image: Anyone (or you from another machine) can now pull and use your image from Docker Hub using:
shellCopy code
docker pull username/repositoryname:tag
Remember, when you push an image to a public repository on Docker Hub, anyone can pull and use your image.
If your image contains sensitive data or proprietary software, consider using a private repository. Docker Hub offers both public and private repositories.

What is username? What is repositoryname ?

In the context of Docker Hub and working with Docker images, username and repositoryname are important identifiers:

Username:
This is your Docker Hub account username. It's the identifier you use to log into Docker Hub. When you push or pull images from Docker Hub, you need to prefix the image name with your username to specify your personal repository on Docker Hub. For example, if your Docker Hub username is johndoe, and you are pushing an image named myapp, you would tag and push the image as johndoe/myapp.

Repository Name: This is the name you give to your image when you upload (push) it to Docker Hub.
It's a way to organize and identify different images. For instance, if you have developed a web application, you might name the repository webapp. Combined with your username, the full image name becomes something like johndoe/webapp.

When you put them together, the format looks like this:

[username]/[repositoryname]:[tag]
Tag: The tag is an optional part of the image name. It's typically used to indicate different versions of the same image. Common tags include version numbers like 1.0, 2.1, or descriptors like latest.
So, when you prepare your Docker image for Docker Hub, you're effectively labeling it in a way that it can be uniquely identified and retrieved by anyone (or just by you, if it's a private repository).

This naming convention ensures that your images do not conflict with images uploaded by other users.
image.png
image.png
image.png


image.png

megaphone

How to provide people with instructions to download your Docker Image:

To provide access to people for them to download your Docker image from Docker Hub:

You provide the command that others can use to pull the image from Docker Hub using the Docker command-line interface.
image.png
Here’s how you can obtain the command for pulling your image:
1. **Log in to Docker Hub** using your web browser.
2. **Navigate to your repository** where you've pushed your Docker image.
3. **Find the image you want to share** within the repository. If it’s the only one or the latest, you don’t need a specific tag; otherwise, you’ll need to choose the tag that corresponds to the image you want to share.
4. **Construct the pull command**.
The basic format for the command is:
docker pull username/repositoryname:tag
Where `username` is your Docker Hub username
`repositoryname` is the name of your repository,
`tag` is the tag of the specific image you want to pull.
If you didn’t specify a tag when you pushed your image, Docker will have tagged it as `latest` by default.
For the image you’ve uploaded, based on the screenshot provided,
that your username is `petersigurdson`, the repository name is `repositoryname`, and the tag is `SpaceCommandV1`.
Therefore, the command to pull this image would be:
docker pull petersigurdson/repositoryname:SpaceCommandV1

To share this with others, simply give them the command, and they can use it in their own terminal to pull the image from Docker Hub.
This is what you put into your Latex Document.
**Note**:
The Docker CLI must be installed on their machine for them to execute this command, and they must have an internet connection to pull the image from Docker Hub.
megaphone

You have an image in the Docker Hub repository named

spacecommandrepository

and the tag for the image is 202404081412

With this information, you can pull the image using the following command in your Docker CLI:

My instruction, to give to people to get to my Docker Image:

docker pull petersigurdson/spacecommandrepository:202404081412

Here's what each part of the command means:
docker pull: This is the command to download the Docker image.
petersigurdson: This is the Docker Hub username.
spacecommandrepository: This is the repository name on Docker Hub where the image is stored.
202404081412: This is the tag of the image you want to download.
Once you run this command, Docker will reach out to Docker Hub, look for the image tagged 202404081412 in the spacecommandrepository repository under the user petersigurdson, and then download it to your local Docker image registry.
After pulling the image, you can run it as a container using:
shCopy code
docker run -it petersigurdson/spacecommandrepository:202404081412

The -it option is used for interactive processes (like a bash shell), but you might need different options depending on the nature of the Docker image and the application within.

docker pull petersigurdson/repositoryname:SpaceCommandV1

image.png

Now that you've got the image pulled down, lets start working with a Docker image locally.


This involves running a container from that image.

Here’s how you can do it using Docker Desktop:

Running a Container from the Image, using the Docker CLI:

Use the Docker CLI to run a container from the image you've pulled.
You'll need to use the docker run command with any necessary options:
Terminal:
docker run -it --name mycontainer petersigurdson/spacecommandrepository:202404081412
-it allows you to interact with the container via the terminal.
--name mycontainer gives your running container a name. You can choose any name you prefer.
The last part petersigurdson/spacecommandrepository:202404081412 specifies which image to use to create the container.
Interacting with the Container:
If the container runs an interactive application, such as a shell or a text editor, you’ll be able to interact with it directly after the docker run command.

Working with Docker Desktop:


Docker Desktop provides a GUI where you can manage your images and containers. After running your container, you can:
Open Docker Desktop.
Go to the "Containers / Apps" tab.
You'll see your container listed there. You can start, stop, restart, or delete your container using the available controls.
You can also open the CLI for the container, view logs, and more.
Accessing the Container’s Shell:
If your container is running a service in the background and you want to interact with the shell, you can exec into the container:
shCopy code
docker exec -it mycontainer /bin/bash
This opens a bash shell inside the container. You might need to replace /bin/bash with /bin/sh or another shell if bash is not available in your container.
Working with Files Inside the Container:
To copy files from your local filesystem to the container or vice versa, use the docker cp command:
To copy a file from your local system to the container:
shCopy code
docker cp /path/to/local/file mycontainer:/path/in/container
To copy a file from the container to your local system:
shCopy code
docker cp mycontainer:/path/in/container /path/to/local/destination
Saving Changes:
If you’ve made changes to your container and want to create a new image from it, you can commit those changes:
shCopy code
docker commit mycontainer petersigurdson/mynewimage:mytag
This creates a new image petersigurdson/mynewimage:mytag with the changes you’ve made in mycontainer.
Remember that changes made in a container do not affect the original image. If you stop and remove the container, the changes will be lost unless you commit them to a new image as shown above.
ChatGPT can mak




megaphone

Training Drill Song:


That sounds like a fun and engaging way to inspire your class! Given the theme and content, here's a draft for your class song:
🎵 (Cadence-style rhythm, think of a marching beat)
(Chorus)
"We are Python programmers, Docker's our stage,
Alpha Force Elite, coding rage!
In the container, out again,
No domain too tough, we claim the win!"
(Verse 1)
"Sign up for Docker Hub, we make our mark,
Building images, bright as a spark.
Pull and push, our code sails through,
In a world of containers, our dreams come true!"
(Chorus)
"We are Python programmers, Docker's our stage,
Alpha Force Elite, coding rage!
In the container, out again,
No domain too tough, we claim the win!"
(Verse 2)
"From 'Hello World' to apps that soar,
Windows Docker Desktop opens the door.
Commands we type, in PowerShell's light,
Our Python code, running just right!"
(Chorus)
"We are Python programmers, Docker's our stage,
Alpha Force Elite, coding rage!
In the container, out again,
No domain too tough, we claim the win!"
(Bridge)
"Dependencies listed, requirements clear,
In our Dockerfile, there's nothing to fear.
Build and run, see our creation,
Python and Docker, a powerful combination!"
(Chorus)
"We are Python programmers, Docker's our stage,
Alpha Force Elite, coding rage!
In the container, out again,
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.