Skip to content
Shop

Docker

What is Docker?

Docker is a platform that allows developers to build, ship, and run applications inside containers. Containers are lightweight, portable, and include everything needed to run an application, including the code, runtime, system tools, libraries, and dependencies.

Why Use Docker?

  • Consistency: Works the same across different environments (local, staging, production).
  • Isolation: No conflicts between dependencies of different projects.
  • Portability: Runs on any machine with Docker installed.
  • Scalability: Easily scale applications using Docker Compose or Kubernetes.

Docker with Python and Flask Example

We will create a simple Flask application and run it inside a Docker container. The files bellow should be placed in a folder named app for this example.

  1. Download Docker - Ensure Docker is installed on your system. You can also install docker using homebrew brew install docker. Check what version you have using docker -v or docker --version.
  2. Create a project folder and a file named app.py.
  3. Create a requirements.txt File. This file contains dependencies.
  4. Create a Dockerfile. A Dockerfile is a script that contains instructions to build a Docker image.
  5. Create a docker-compose.yml (Optional). This file helps in managing multi-container applications.
python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, Docker with Flask!"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
txt
flask
dockerfile
# Use an official Python runtime as a base image
FROM ubuntu

RUN apt update
RUN apt install python3-pip -y
RUN pip3 install Flask

# Set the working directory
WORKDIR /app

# Copy the application files
COPY . /app

# Install dependencies
RUN pip3 install -r requirements.txt

# Expose the port Flask runs on
EXPOSE 5000

# Run the application
CMD ["python3", "-m" "flask", "run" "--host=0.0.0.0"]
yaml
version: "3.8"
services:
  flask-app:
    build: .
    ports:
      - "5000:5000"

Build and Run the Container

Build the Docker image Without Docker Compose

sh
docker build -t flask-app .

Run the container:

sh
docker run -p 5000:5000 flask-app

You can run it dormant (in the background)using the -d flag. -p is for port forwarding.

Build the Docker image with Docker Compose

sh
docker-compose up --build

To access the Flask App Open your browser and visit:

[http://localhost:5000](http://localhost:5000)

Show Containers and Images

To show all containers in Docker, you can use the command.

docker ps -a or docker container ls -a.

The -a or --all flag shows both running and stopped containers.

To see what images are created

docker images

Stop and Remove Containers

To stop the container

sh
docker stop <container_id>

To remove all containers and images (if needed)

sh
docker system prune -a

Conclusion

This setup allows you to package your Flask app inside a container, making deployment simple and consistent across different environments. 🚀

Resources