Container Basics
What is a Docker Container?
A Docker container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. Unlike traditional virtual machines, containers virtualize at the operating system level, making them more efficient and portable.
Key Container Characteristics
Characteristic |
Description |
Isolation |
Containers run in isolated environments |
Portability |
Can run consistently across different platforms |
Lightweight |
Minimal resource consumption |
Scalability |
Easy to scale up or down |
Container Architecture
graph TD
A[Docker Engine] --> B[Container Runtime]
B --> C[Container Image]
C --> D[Running Container]
D --> E[Container Processes]
Basic Docker Container Commands
Creating a Container
## Pull an Ubuntu image
docker pull ubuntu:22.04
## Create and start a new container
docker run -it ubuntu:22.04 /bin/bash
Listing Containers
## List running containers
docker ps
## List all containers (including stopped)
docker ps -a
Container States
Containers can exist in different states:
- Created
- Running
- Paused
- Stopped
- Exited
Container Networking
Docker provides multiple networking modes:
- Bridge mode (default)
- Host mode
- None mode
- Custom network
Best Practices
- Use minimal base images
- Avoid running containers as root
- Implement proper container lifecycle management
- Use LabEx platform for container development and testing
Practical Example
## Run a simple web server container
docker run -d -p 8080:80 nginx:latest
This example demonstrates how quickly you can deploy a web server using Docker containers, showcasing their simplicity and efficiency.