Docker Basics
Get started with Docker: containers, images, and how to containerize your applications.
A Simple Analogy
Docker containers are like shipping containers. They package your application and everything it needs (code, runtime, libraries) into a standardized box. This box runs the same way everywhere—on your laptop, a server, or the cloud.
What Is Docker?
Docker is a containerization platform that packages applications and their dependencies into isolated, lightweight containers. Containers are portable, reproducible, and more efficient than virtual machines.
Why Use Docker?
- Consistency: "Works on my machine" → "Works everywhere"
- Isolation: Applications don't interfere with each other
- Efficiency: Lightweight compared to full VMs
- Scalability: Easy to deploy multiple instances
- Simplifies DevOps: Single image → multiple environments
Images vs. Containers
| Concept | Meaning | |---------|---------| | Image | Blueprint (read-only template) | | Container | Running instance of an image |
Basic Workflow
1. Create a Dockerfile
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
2. Build an Image
docker build -t my-app:1.0 .
3. Run a Container
docker run -p 3000:3000 my-app:1.0
Key Docker Commands
docker images # List images
docker ps # List running containers
docker ps -a # List all containers
docker logs <container-id> # View container logs
docker exec -it <id> bash # Enter container shell
docker stop <container-id> # Stop container
docker rm <container-id> # Remove container
Practical Example
# ASP.NET Core API
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["MyApi.csproj", "."]
RUN dotnet restore "MyApi.csproj"
COPY . .
RUN dotnet build "MyApi.csproj" -c Release -o /app/build
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app/build .
EXPOSE 80
ENTRYPOINT ["dotnet", "MyApi.dll"]
Run and check logs:
docker build -t my-api:1.0 .
docker run -p 80:80 my-api:1.0
docker logs <container-id>
Related Concepts to Explore
- Docker Compose (multi-container apps)
- Docker Registry and Docker Hub
- Container orchestration (Kubernetes)
- Networking and volumes in Docker
Summary
Docker enables developers to package, ship, and run applications reliably across any environment. Master Dockerfile creation and basic commands to get started with containerization and modern DevOps practices.