Lädt...

🔧 Getting Started with Docker: How to Containerize Your First Application


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

🚀 Why Docker?

Ever heard someone say, "It works on my machine" while debugging a nightmare in production? Yeah, we've all been there. Docker eliminates this problem by containerizing your applications—ensuring they run the same everywhere, from your local laptop to a massive cloud deployment.

If you're a DevOps enthusiast or a developer aiming to streamline deployments, mastering Docker is a game-changer. So, let’s cut the fluff and jump straight into containerizing your first application.

🛠 Step 1: Install Docker

Before diving in, you need Docker installed. If you haven’t already:

  • Windows & Mac: Install Docker Desktop
  • Linux: Use the following commands to install:
  sudo apt update
  sudo apt install docker.io -y
  sudo systemctl enable --now docker

Verify installation:

  docker --version

📦 Step 2: Create Your First Application

Let’s keep it simple with a Python Flask app.

1️⃣ Create a new project folder:

mkdir my-docker-app && cd my-docker-app

2️⃣ Create app.py:

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

3️⃣ Add requirements.txt:

flask

🛳 Step 3: Write a Dockerfile

A Dockerfile is a blueprint for creating Docker containers. Create a file named Dockerfile (no extension) and add:

# Use an official Python runtime as a parent image
FROM python:3.9

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . .

# Install any dependencies
RUN pip install -r requirements.txt

# Make port 5000 available
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

⚙️ Step 4: Build and Run the Container

Let’s bring this to life! Run the following command to build the image:

docker build -t my-flask-app .

Once the image is built, start a container:

docker run -p 5000:5000 my-flask-app

🎉 Boom! Your Flask app is now running inside a Docker container. Open http://localhost:5000 in your browser to see it in action.

🔥 Step 5: Share Your Container (Optional but Cool)

Want to share your containerized app? Push it to Docker Hub:

docker login
docker tag my-flask-app your-dockerhub-username/my-flask-app
docker push your-dockerhub-username/my-flask-app

Now, anyone can run your app with:

docker run -p 5000:5000 your-dockerhub-username/my-flask-app

📌 Step 6: Manage Containers

Now that you have a running container, let’s explore some useful commands:

  • List running containers:
  docker ps
  • List all containers (including stopped ones):
  docker ps -a
  • Stop a running container:
  docker stop <container_id>
  • Remove a container:
  docker rm <container_id>

🏗 Step 7: Use Docker Compose

For multi-container applications, Docker Compose is your best friend. Create a docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"

Run your app with:

docker-compose up

🧹 Step 8: Clean Up Docker Resources

Docker can consume disk space quickly. Use these commands to clean up:

  • Remove all stopped containers:
  docker container prune
  • Remove unused images:
  docker image prune -a
  • Remove all unused volumes:
  docker volume prune

🔑 Step 9: Run Containers in Detached Mode

Running a container in the background ensures it doesn’t block your terminal:

docker run -d -p 5000:5000 my-flask-app

Check logs of a running container:

docker logs <container_id>

🔥 Step 10: Deploy Containers to the Cloud

Once you're comfortable running containers locally, try deploying them to the cloud using:

  • AWS ECS (Elastic Container Service)
  • Google Cloud Run
  • Azure Container Apps
  • Kubernetes for large-scale orchestration

💡 Final Thoughts

Docker is a must-have skill for modern developers and DevOps engineers. With just a few commands, you’ve successfully containerized your first app. Now, imagine scaling this up with Kubernetes, CI/CD, and cloud deployments! 🚀

What’s Next?

  • Try containerizing a Node.js or Go app.
  • Learn about Docker Compose for multi-container apps.
  • Dive into Kubernetes to orchestrate containers at scale.

Found this helpful? Share your thoughts below or flex your first containerized app in the comments! 💬🔥

...

🔧 Getting Started with Docker: How to Containerize Your First Application


📈 56.24 Punkte
🔧 Programmierung

🔧 Step-by-step guide to containerize your full-stack MERN application, using Docker Compose.


📈 36.45 Punkte
🔧 Programmierung

🔧 Containerize your Django Web Application with Docker


📈 36.45 Punkte
🔧 Programmierung

🔧 Docker Applied Skills: Containerize a Node.js application


📈 33.89 Punkte
🔧 Programmierung

🔧 How to Containerize a Node.js Application Using Docker – A Beginner's Guide


📈 33.89 Punkte
🔧 Programmierung

🔧 How to containerize Django application with Docker compose


📈 33.89 Punkte
🔧 Programmierung

🔧 Containerize Rust Application in 2 Minutes using Docker Init


📈 33.89 Punkte
🔧 Programmierung

🔧 How to containerize a Flask Python application using Docker


📈 33.89 Punkte
🔧 Programmierung

🔧 Containerize a Web Application using docker compose


📈 33.89 Punkte
🔧 Programmierung

🔧 Containerize your NodeJS App using Docker and deploy it to Azure Container Apps


📈 31.44 Punkte
🔧 Programmierung

🔧 How to Containerize Your Backend Applications Using Docker


📈 31.44 Punkte
🔧 Programmierung

🔧 Master Docker Commands for Developers: 50 Tips to Containerize Confidently


📈 28.88 Punkte
🔧 Programmierung

🔧 Containerize ASP.NET Core API, Entity Framework with SQL, Let's Encrypt, Docker, and Nginx (Part 1)


📈 28.88 Punkte
🔧 Programmierung

🔧 How To: Containerize a Django and Postgres App with Docker


📈 28.88 Punkte
🔧 Programmierung

🐧 Bought a large VPS node, would like to containerize my projects, Docker my best choice?


📈 28.88 Punkte
🐧 Linux Tipps

🔧 Getting Started with Cloud Computing: A Beginner's Guide to Deploying Your First Application


📈 27.36 Punkte
🔧 Programmierung

🔧 Getting Started with Laravel: Your First Application


📈 27.36 Punkte
🔧 Programmierung

📰 Should We Containerize Our Cloud-Based Application?


📈 27.11 Punkte
📰 IT Security Nachrichten

🔧 How to containerize a Node.js application


📈 27.11 Punkte
🔧 Programmierung

🔧 How to containerize an Application


📈 27.11 Punkte
🔧 Programmierung

🔧 How to Containerize a Node.js Application


📈 27.11 Punkte
🔧 Programmierung

🔧 How To Containerize a node.js Application


📈 27.11 Punkte
🔧 Programmierung

🔧 Getting Started with Docker: First Steps 🐳


📈 26.57 Punkte
🔧 Programmierung

🔧 Getting Started with HTML: Your Step-by-Step Guide to Creating Your First Web Page


📈 24.91 Punkte
🔧 Programmierung

🔧 How to containerize your web app- a beginner-friendly tutorial for Dockerfile


📈 24.66 Punkte
🔧 Programmierung

🔧 Containerize Your Flask App: Here's Why 0.0.0.0 Holds the Key!


📈 24.66 Punkte
🔧 Programmierung

🔧 How to Containerize Your Docusaurus (And Why You Should)


📈 24.66 Punkte
🔧 Programmierung

🐧 Getting started with Docker on your VPS | Serverwise


📈 24.26 Punkte
🐧 Linux Tipps

🎥 Building Your AppSec Program: Getting Started - Application Security Weekly #14


📈 22.49 Punkte
🎥 IT Security Video

🔧 Getting started with Amazon SageMaker: Building your first machine learning model


📈 22.35 Punkte
🔧 Programmierung

🔧 Getting Started with Python Your First Python Script


📈 22.35 Punkte
🔧 Programmierung

🔧 Getting Started with Azure Bot Service: Building Your First Chatbot


📈 22.35 Punkte
🔧 Programmierung

🔧 Your First Moves in Enterprise Architecture – A Strategic Guide to Getting Started


📈 22.35 Punkte
🔧 Programmierung

🔧 Building Your First Browser Game with Three.js and React: Part 1 - Getting Started


📈 22.35 Punkte
🔧 Programmierung

matomo