My Simple Docker Project

My Simple Docker Project

Source: Dev.to

🐳 Building a Simple "Hello World" Docker Project ## 🔹 System Preparation ## Add Docker’s Official GPG Key ## Add Docker Repository ## Install Docker ## 🔹 Install Docker Compose Plugin ## 🔹 Create Project Directory ## 🔹 Create Python Web Application ## 🔹 Define Dependencies ## 🔹 Create Dockerfile ## 🔹 Configure Docker Compose ## 🔹 Build and Run the Project ## 🔹 Test the Web App ## 🔹 Stop Containers 🚀 In this guide, we'll create a containerized Python web application that displays a "Hello World" message. This tutorial works on Ubuntu/Debian-based Linux distros Before we start, update and upgrade your system packages: Install Docker and other required dependencies: Start and enable Docker: Ubuntu now uses the Docker Compose plugin instead of the standalone binary: Install Python Pip for dependencies: Set up the directory structure: Create the app.py file: Copy & paste the following Python Flask app: Save the file (CTRL + X, then Y). Create the requirements.txt file: Add dependencies inside requirements.txt: Save (CTRL + X, then Y). Paste the following Dockerfile configuration: Save (CTRL + X, then Y). Create the docker-compose.yml file: Paste the Docker Compose config: Save (CTRL + X, then Y). ⚠ YAML files are sensitive to indentation—validate them before running! Ensure you're in the docker-projects directory: Start the containers: Open a browser and go to http://localhost:5000. Refreshing the page will increase the visitor count. Shut down the containers when you're done: Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK: sudo apt update sudo apt upgrade -y Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo apt update sudo apt upgrade -y COMMAND_BLOCK: sudo apt update sudo apt upgrade -y COMMAND_BLOCK: sudo apt install ca-certificates curl gnupg Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo apt install ca-certificates curl gnupg COMMAND_BLOCK: sudo apt install ca-certificates curl gnupg COMMAND_BLOCK: sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null sudo chmod a+r /etc/apt/keyrings/docker.asc Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null sudo chmod a+r /etc/apt/keyrings/docker.asc COMMAND_BLOCK: sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null sudo chmod a+r /etc/apt/keyrings/docker.asc COMMAND_BLOCK: echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null COMMAND_BLOCK: echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null COMMAND_BLOCK: sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin COMMAND_BLOCK: sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin COMMAND_BLOCK: docker --version Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: docker --version COMMAND_BLOCK: docker --version COMMAND_BLOCK: sudo systemctl enable --now docker Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo systemctl enable --now docker COMMAND_BLOCK: sudo systemctl enable --now docker COMMAND_BLOCK: sudo systemctl status docker Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo systemctl status docker COMMAND_BLOCK: sudo systemctl status docker COMMAND_BLOCK: sudo apt install docker-compose-plugin -y Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo apt install docker-compose-plugin -y COMMAND_BLOCK: sudo apt install docker-compose-plugin -y COMMAND_BLOCK: docker compose version Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: docker compose version COMMAND_BLOCK: docker compose version COMMAND_BLOCK: sudo apt install python3-pip python3 -m pip install --upgrade pip Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo apt install python3-pip python3 -m pip install --upgrade pip COMMAND_BLOCK: sudo apt install python3-pip python3 -m pip install --upgrade pip CODE_BLOCK: mkdir docker-projects cd docker-projects Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: mkdir docker-projects cd docker-projects CODE_BLOCK: mkdir docker-projects cd docker-projects CODE_BLOCK: touch app.py nano app.py Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: touch app.py nano app.py CODE_BLOCK: touch app.py nano app.py CODE_BLOCK: from flask import Flask import redis app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): try: return cache.incr('hits') except redis.exceptions.ConnectionError: return 1 @app.route('/') def hello(): count = get_hit_count() return f"Hello World! You have visited {count} times." if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: from flask import Flask import redis app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): try: return cache.incr('hits') except redis.exceptions.ConnectionError: return 1 @app.route('/') def hello(): count = get_hit_count() return f"Hello World! You have visited {count} times." if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) CODE_BLOCK: from flask import Flask import redis app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): try: return cache.incr('hits') except redis.exceptions.ConnectionError: return 1 @app.route('/') def hello(): count = get_hit_count() return f"Hello World! You have visited {count} times." if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) CODE_BLOCK: touch requirements.txt nano requirements.txt Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: touch requirements.txt nano requirements.txt CODE_BLOCK: touch requirements.txt nano requirements.txt CODE_BLOCK: Flask redis Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: Flask redis CODE_BLOCK: Flask redis CODE_BLOCK: touch Dockerfile nano Dockerfile Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: touch Dockerfile nano Dockerfile CODE_BLOCK: touch Dockerfile nano Dockerfile CODE_BLOCK: FROM python:3.9 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: FROM python:3.9 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"] CODE_BLOCK: FROM python:3.9 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"] CODE_BLOCK: touch docker-compose.yml nano docker-compose.yml Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: touch docker-compose.yml nano docker-compose.yml CODE_BLOCK: touch docker-compose.yml nano docker-compose.yml CODE_BLOCK: version: '3' services: web: build: . ports: - "5000:5000" depends_on: - redis redis: image: "redis:alpine" Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: version: '3' services: web: build: . ports: - "5000:5000" depends_on: - redis redis: image: "redis:alpine" CODE_BLOCK: version: '3' services: web: build: . ports: - "5000:5000" depends_on: - redis redis: image: "redis:alpine" CODE_BLOCK: cd docker-projects Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: cd docker-projects CODE_BLOCK: cd docker-projects COMMAND_BLOCK: sudo docker-compose up --build Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo docker-compose up --build COMMAND_BLOCK: sudo docker-compose up --build CODE_BLOCK: Hello World! You have visited 1 times. Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: Hello World! You have visited 1 times. CODE_BLOCK: Hello World! You have visited 1 times. COMMAND_BLOCK: sudo docker-compose down Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo docker-compose down COMMAND_BLOCK: sudo docker-compose down