๐ 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