🟢 1️⃣ What is Docker?
-
Docker is a tool to package applications with all dependencies into a container.
-
Container = isolated environment that runs your app anywhere (Windows, Linux, cloud).
-
Benefit: “It works on my machine” problem is solved.
Analogy: Docker is like a shipping container for your app. It includes your app, .NET runtime, libraries, and everything needed.
🟢 2️⃣ Why Use Docker with .NET Core?
-
Cross-platform: Run .NET Core apps on Linux, Windows, or cloud.
-
Easy deployment: Package app + dependencies.
-
Isolation: No conflicts between apps.
-
Microservices: Each service can run in its own container.
🟢 3️⃣ Steps to Dockerize a .NET Core App
Step 1: Create a .NET Core App
-
Your API runs locally on
http://localhost:5000
.
Step 2: Create a Dockerfile
-
In the root folder, create a file named Dockerfile (no extension):
Explanation:
-
Stage 1 (base): Runtime image to run the app.
-
Stage 2 (build): SDK image to build and publish the app.
-
Stage 3 (final): Copy published files to runtime image and run.
Step 3: Build Docker Image
-
mywebapi
→ image name,1.0
→ version tag. -
Docker reads the Dockerfile and creates the image.
Step 4: Run the Container
-
-d
→ run in background -
-p 8080:80
→ map container port 80 to local port 8080 -
--name
→ container name
Now open http://localhost:8080
→ your API is running inside a Docker container!
Step 5: (Optional) Docker Compose
-
Useful if you have multiple services (like API + database).
-
Create
docker-compose.yml
:
-
Run with:
docker-compose up -d
🟢 4️⃣ Tips
-
Always use multi-stage Dockerfile → keeps image size small.
-
Expose only needed ports (
EXPOSE 80
). -
Use environment variables for secrets/configuration.
-
Use .dockerignore to exclude unnecessary files (like
bin
,obj
).
🟢 5️⃣ Summary
-
Docker packages your .NET Core app + dependencies into a container.
-
Steps:
-
Create .NET Core app
-
Write Dockerfile
-
Build Docker image (
docker build
) -
Run container (
docker run
)
-
-
Optional: Use Docker Compose for multi-container apps.