Skip to content

This project provides a comprehensive guide to containerizing a Python application using Docker. Follow the steps below to set up your environment, create a Dockerfile, build and push your Docker image, and run your container.

License

Notifications You must be signed in to change notification settings

atulkamble/docker-python-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dockerizing Python Code

Clone repo

git clone https://github.com/atulkamble/docker-python-app.git
cd docker-python-app

Prerequisites

  1. Ensure Docker is installed on your system: Install Docker if it’s not already installed:

    sudo yum install docker -y
  2. Install Git: If Git is not installed, install it:

    sudo yum install git -y
  3. Install Python: If Python is not installed, install it:

    sudo yum install python -y

Steps to Dockerize Python Code

  1. Clone the Docker Python Project Repository:

    git clone https://github.com/atulkamble/dockerpythonproject.git
    cd dockerpythonproject
  2. Setup Docker: Start the Docker service:

    sudo systemctl start docker

Enable Docker to start on boot: bash sudo systemctl enable docker Verify Docker installation: bash docker --version

  1. Clone the Test DevOps Project Repository:

    git clone https://github.com/atulkamble/testdevopsproject.git
    cd testdevopsproject/
  2. Configure Git: Set up your Git user details:

    git config --global user.name "atulkamble"
    git config --global user.email "atul_kamble@hotmail.com"
  3. Create and Test Python Script: Create a Python script file:

    touch sum.py
    sudo nano sum.py

Add your Python code to sum.py and test it: bash python3 sum.py

  1. Create Dockerfile: Create a Dockerfile:
    touch Dockerfile
    sudo nano Dockerfile

Add the following content to your Dockerfile: ```dockerfile # Use the official Python image from the Docker Hub FROM python:3.8-slim

 # Set the working directory in the container
 WORKDIR /app

 # Copy the current directory contents into the container at /app
 COPY . /app

 # Install any needed packages specified in requirements.txt
 RUN pip install --no-cache-dir -r requirements.txt

 # Make port 80 available to the world outside this container
 EXPOSE 80

 # Define environment variable
 ENV NAME World

 # Run sum.py when the container launches
 CMD ["python", "sum.py"]
 ```
  1. Build and Push Docker Image: Log in to Docker Hub:
    sudo docker login

Build your Docker image: bash sudo docker build -t atuljkamble/pythonproject . Verify the Docker image is built: bash sudo docker images Push the Docker image to Docker Hub: bash sudo docker push atuljkamble/pythonproject

  1. Run Docker Container: Run your Docker container:
    sudo docker run atuljkamble/pythonproject

List all Docker containers to verify it is running: bash sudo docker ps -a

By following these steps, you will have successfully Dockerized your Python application, built a Docker image, and run it as a container.

About

This project provides a comprehensive guide to containerizing a Python application using Docker. Follow the steps below to set up your environment, create a Dockerfile, build and push your Docker image, and run your container.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published