0% found this document useful (0 votes)
4 views4 pages

Exp 7

This document outlines an experiment to understand Dockerfile by creating a custom Docker image for a simple Python application. It includes steps for creating a project directory, writing a Python script, crafting a Dockerfile, building the Docker image, running the container, and cleaning up afterward. The experiment emphasizes the use of Docker commands and the structure of a Dockerfile.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Exp 7

This document outlines an experiment to understand Dockerfile by creating a custom Docker image for a simple Python application. It includes steps for creating a project directory, writing a Python script, crafting a Dockerfile, building the Docker image, running the container, and cleaning up afterward. The experiment emphasizes the use of Docker commands and the structure of a Dockerfile.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment 7

Aim: Understanding Dockerfile by Creating a Custom Docker Image

Objective:

To understand how a Dockerfile is structured and how to build a Docker image from it by containerizing a
simple Python application.

Step 1: Create a Project Directory

Open a terminal and create a new directory for this experiment:

mkdir docker-experiment

cd docker-experiment

Step 2: Create a Simple Python Script

Inside the docker-experiment folder, create a Python script named app.py:

print("Hello, Docker! This is a simple Python application running inside a container.")

Save the file.

Step 3: Write a Dockerfile

Create a new file named Dockerfile (without any extension) inside the docker-experiment directory
and add the following content:

# Use an official Python runtime as a base image

FROM python:3.9-slim

# Set the working directory inside the container

WORKDIR /app
# Copy the local script into the container

COPY app.py .

# Define the command to run the script

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

This Dockerfile:

• Uses python:3.9-slim as the base image.

• Sets /app as the working directory.

• Copies app.py from the local directory into the container.

• Runs the script using Python.

Step 4: Build the Docker Image

Run the following command in the terminal to build the Docker image:

docker build -t my-python-app .

• docker build: Command to build a Docker image.

• -t my-python-app: Assigns the image name my-python-app.

• . (dot): Specifies the current directory as the build context.


Step 5: Run the Docker Container

After the image is built, run a container from it:

docker run my-python-app

Expected output:

Hello, Docker! This is a simple Python application running inside a container.

Step 6: Remove the Docker Image and Container (Cleanup)

• Stop a running container (if any):

docker stop <container_id>

• Remove the container:

docker rm <container_id>

• Remove the Docker image:

docker rmi my-python-app


Conclusion

In this experiment, we:

1. Created a project directory.

2. Created a Python script.

3. Wrote a Dockerfile to containerize the script.

4. Built a Docker image using docker build.

5. Ran a Docker container using docker run.

6. Explored Docker commands like images, ps, stop, rm, and rmi.

You might also like