Monday, December 30, 2024

How to create a docker image and push to Docker hub

Have Dockerfile like this below 

================================

# Use Python 3.12 as the base image

FROM python:3.12-slim

# Set the working directory

WORKDIR /app

# Copy the requirements and install dependencies

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code

COPY ./app ./app

# Expose the application port

EXPOSE 8000

# Start the FastAPI app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]


Have requirement.txt file like below 

=====================================

fastapi

uvicorn


Have the main.py like below 

===========================

from fastapi import FastAPI

app = FastAPI()

@app.get("/")

def read_root():

    return {"message": "Hello, Dockerized FastAPI!"}


Now the build process is like below 

The below steps to be executed after creating an account in docker hub. Instead of using password on terminal, access token can be created and that can be used. 


docker build -t mrrathish/crashing-docker-app:latest .

docker login

docker push mrrathish/crashing-docker-app:latest


docker run -p 8000:8000 mrrathish/crashing-docker-app:latest

That's it pretty much 


No comments:

Post a Comment