Docker Container Basics
What is a Docker Container?
A Docker container is a lightweight, standalone, and executable package that includes everything needed to run an application: code, runtime, system tools, libraries, and settings. Unlike virtual machines, containers virtualize the operating system instead of hardware, making them more efficient and portable.
Container Lifecycle
Containers go through several states during their lifecycle:
stateDiagram-v2
[*] --> Created
Created --> Running
Running --> Paused
Paused --> Running
Running --> Stopped
Stopped --> Removed
Removed --> [*]
Basic Container Management Commands
Command |
Description |
Example |
docker create |
Create a new container |
docker create ubuntu:latest |
docker start |
Start a stopped container |
docker start container_id |
docker run |
Create and start a container |
docker run -d ubuntu:latest |
docker stop |
Stop a running container |
docker stop container_id |
docker rm |
Remove a container |
docker rm container_id |
Creating and Managing Containers on Ubuntu 22.04
Pull an Image
docker pull ubuntu:latest
Create and Run a Container
## Run an interactive container
docker run -it --name mycontainer ubuntu:latest /bin/bash
List Containers
## List running containers
docker ps
## List all containers (including stopped)
docker ps -a
Container Networking and Storage
Containers can be connected to networks and have persistent storage volumes, enabling complex application architectures. LabEx provides comprehensive Docker training to help developers master these advanced concepts.
Best Practices
- Use minimal base images
- Avoid running containers as root
- Implement proper container lifecycle management
- Use Docker volumes for persistent data