Programming & Development / April 18, 2025

Hello World in Python with Docker Compose

Python Hello World Docker Dockerfile Docker Compose example app containerized Python script

๐Ÿ Basic Python Hello World Program

Create a simple file named hello_world.py:

python

# hello_world.py
def main():
    print("Hello, World!")

if __name__ == "__main__":
    main()

๐Ÿณ Step 1: Create a Dockerfile

Dockerfile

# Dockerfile
FROM python:3.10-slim

WORKDIR /app

COPY hello_world.py .

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

๐Ÿ› ๏ธ Step 2: Create docker-compose.yml

yaml

# docker-compose.yml
version: '3.8'

services:
  hello-python:
    build: .
    container_name: hello-python-container

๐Ÿ“‚ Final Project Structure

hello-python-docker/
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ hello_world.py

โ–ถ๏ธ Step 3: Run with Docker Compose

From the terminal, navigate to the project folder and run:

bash

docker-compose up --build

โœ… Expected Output:

bash

hello-python-container  | Hello, World!

๐Ÿงน Optional: Stop and Clean Up

To stop and remove the container:

bash

docker-compose down



Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex