Docker: The Tupperware Party for Your Code

Intro

Are you tired of your code taking up valuable space in your computer's fridge? Do you have a bunch of leftover code lying around that you're not sure what to do with? Well, fear not! Just like Tupperware keeps your leftovers fresh and organized, Docker can do the same for your code.

Docker is a tool that allows you to package your code and its dependencies into a lightweight container, which can then be easily transported and deployed on any machine that has Docker installed. Think of it like a Tupperware container for your code!

Installation

There's no better documentation for installation other than the official documentation.
Hence I will just link the documentation here - https://docs.docker.com/get-docker/

How does Docker work?

Docker is made up of several components, including:

  1. Docker Engine: This is the core component of Docker that is responsible for creating and managing containers.

  2. Docker Hub: This is a central repository where you can store and share Docker images.

  3. Docker CLI: This is a command-line interface that you can use to interact with Docker.

To create a Docker container, you start by creating a Docker image.
Now, what is a Docker image?
It is a read-only template that includes everything that a container needs to run. You can create an image by starting with a base image that includes a specific operating system and runtime. Then you can add your application code and any necessary dependencies on top of the base image. Once you have created an image, you can use it to create one or more containers.

Each container that you create from an image is an isolated environment that includes its file system, network interface, and IP address. You can run multiple containers from the same image, and each container will behave as if it is running on its independent system.

Example

Fast Api Project Setup

Let's start by creating a Fast API project that generates random "leftover" recipes based on a list of ingredients:

Lets first install packages like fast api and uvicorn with pip

pip install fastapi uvicorn

Now, we are creating a file named recipes.py and the following contents:

from fastapi import FastAPI
from typing import List
import random

app = FastAPI()

@app.get("/tupperware/{ingredients}")
async def generate_recipe(ingredients: List[str]):
    recipes = {
        "Spicy Meatball Sandwich": ["meatballs", "tomato sauce", "mozzarella cheese", "hoagie roll", "red pepper flakes"],
        "Vegetable Stir Fry": ["assorted veggies", "soy sauce", "rice noodles", "garlic"],
        "Taco Salad": ["ground beef", "lettuce", "tomatoes", "taco seasoning", "tortilla chips"],
        "Frittata": ["eggs", "cheese", "spinach", "mushrooms", "onions"],
        "Mediterranean Salad": ["chickpeas", "feta cheese", "kalamata olives", "cucumber", "red onion"],
        "Spaghetti and Meatballs": ["spaghetti noodles", "meatballs", "tomato sauce", "parmesan cheese"],
        "Potato and Ham Casserole": ["potatoes", "ham", "cheddar cheese", "sour cream", "butter"],
        "Pizza Rolls": ["pizza dough", "pepperoni", "mozzarella cheese", "pizza sauce", "garlic powder"],
        "Burrito Bowl": ["black beans", "rice", "salsa", "sour cream", "shredded cheese"]
    }

    recipe_name = random.choice(list(recipes.keys()))
    recipe_ingredients = recipes[recipe_name]

    # check if the user provided ingredients match the recipe ingredients
    for ingredient in ingredients:
        if ingredient not in recipe_ingredients:
            return {"message": f"Sorry, we couldn't find a recipe that matches those ingredients!"}

    return {"recipe": recipe_name, "ingredients": recipe_ingredients}

To run the above project, you can use the following command:

uvicorn main:app --reload

Now, you can open the below url in your browser
http://localhost:8000/tupperware/meatballs,tomato%20sauce,mozzarella%20cheese,hoagie%20roll,red%20pepper%20flakes

Dokerise the project

Create a file named Dockerfile in the same directory where the above fast api project exists and add the below contents

FROM python:3.9-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

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

This Dockerfile starts with a base image of Python 3.9 on Debian Buster and sets the working directory to /app. It then copies the requirements.txt file into the container and installs the necessary dependencies with pip. Finally, it copies the rest of the project files into the container and sets the default command to run the FastAPI app with Uvicorn.

To build the Docker image, you can use the docker build command:

docker build -t fastapi-recpie-app .

This will build the image with the tag my-fastapi-app.

Once the image is built, you can run it with the docker run command:

docker run -p 8000:8000 my-fastapi-app

Now that your FastAPI app is running in a Docker container, you can access it by visiting http://localhost:8000 in your web browser. And with FastAPI's ability to generate random recipes based on a list of ingredients, you can "Tupperware" your leftover ingredients into a tasty meal with just a few clicks.

Docker is popular for several reasons:

  1. Portability: Docker containers are highly portable and can be easily moved from one system to another, making them ideal for deploying applications in a cloud-based environment.

  2. Efficiency: Docker containers are much more lightweight than virtual machines, which means that you can run more containers on a single system than you can on virtual machines.

  3. Consistency: Docker containers ensure that your application runs in the

So what are you waiting for? Start packaging up your code with Docker, and join the Tupperware party for your code today!