0% found this document useful (0 votes)
2 views

Lab 2_ Building and Running a Custom Docker Image

This document outlines a lab exercise for building and running a custom Docker image using a Dockerfile. It includes steps for creating a working directory, writing a simple Python script, creating a Dockerfile, building the Docker image, running the container, verifying the image, and cleaning up afterwards. By completing the lab, participants will learn how to effectively use Docker commands to manage containerized applications.

Uploaded by

Ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 2_ Building and Running a Custom Docker Image

This document outlines a lab exercise for building and running a custom Docker image using a Dockerfile. It includes steps for creating a working directory, writing a simple Python script, creating a Dockerfile, building the Docker image, running the container, verifying the image, and cleaning up afterwards. By completing the lab, participants will learn how to effectively use Docker commands to manage containerized applications.

Uploaded by

Ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 2: Building and Running a Custom Docker Image

Objective

This lab demonstrates how to create a custom Docker image using a Dockerfile, build the
image, and run it as a container. By completing this lab, you will understand:

1. How to create a Dockerfile.


2. How to use the docker build command.
3. How to run a custom container.

Pre-requisites

1. Docker installed on your Windows machine.


2. A text editor to create and edit files (e.g., Notepad++, VS Code).

Step 1: Create a Working Directory

1. Open a terminal or file explorer.


2. Navigate to a folder where you want to work on this lab.

Create a new folder named rick-docker:

mkdir rick-docker
cd rick-docker

Explanation: The folder acts as a workspace to store your Dockerfile and related files.
Step 2: Write a Simple Python Script

1. Inside the rick-docker folder, create a file named app.py.

Open the file in a text editor and paste the following code (For example, you can use notepad.
See the video for detailed instructions.):

print("Hello from Docker!")

2. Save and close the file (Be sure to save as a .py file and not .txt)

Explanation:
○ This script is a simple Python program that prints a message. The script will be
executed inside the Docker container.

Step 3: Create a Dockerfile

A Dockerfile is a simple text file that contains a series of instructions used to create a Docker
image. It's like a recipe for building a containerized application. It tells Docker how to set up an
environment (such as installing software, setting up configurations, or copying files) for your
application so it can run in a Docker container.

1. In the same folder (rick-docker), create a file named Dockerfile (no extension).

Open the file in a text editor and add the following content:

# Use an official Python runtime as a base image


FROM python:3.9-slim

# Copy the Python script into the container


COPY app.py /app/app.py

# Set the working directory


WORKDIR /app

# Define the command to run the Python script


CMD ["python", "app.py"]

2. Save and close the file with no extension.

Explanation:
○ FROM: Specifies the base image to use (Python 3.9 slim version).
○ COPY: Copies the app.py file from your local system into the container at
/app/app.py.
○ WORKDIR: Sets the working directory for any subsequent commands in the
Dockerfile.
○ CMD: Specifies the command to run when the container starts (python
app.py).

Step 4: Build the Docker Image

1. Open a terminal in the rick-docker folder. (InWindows use cmd, and cd to navigate to
the correct folder)

Run the following command to build the image:

docker build -t my-python-app .

2. Explanation:
○ docker build: Command to build a Docker image.
○ -t my-python-app: Tags the image with the name my-python-app.
○ .: Refers to the current directory where the Dockerfile is located.

Expected Output:

● Docker will process the Dockerfile step-by-step.

The output should indicate a successful build:

Successfully built <image-id>


Successfully tagged my-python-app:latest

Step 5: Run the Docker Container

Run the image as a container:

docker run my-python-app

1. Explanation:
○ docker run: Creates and starts a container from the image.
○ my-python-app: Refers to the custom image you just built.

Expected Output:
Hello from Docker!

Step 6: Verify the Docker Image

List all available images:

docker images

1. Explanation:
○ docker images: Displays all locally built or pulled images.

Expected Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
my-python-app latest <image-id> A few seconds ago <size>

Step 7: Clean Up

Stop and remove any running containers:

docker ps -a
docker rm <CONTAINER ID>

Remove the image:

docker rmi my-python-app

Conclusion

In this lab, you:

1. Created a simple Python script.


2. Wrote a Dockerfile to containerize the script.
3. Built and ran a custom Docker image.

You might also like