Basic Docker Commands
Docker Command Structure
Docker commands follow a standard syntax:
docker [command] [options] [arguments]
Image Management Commands
1. Pull Images
docker pull ubuntu:latest
docker pull nginx:alpine
2. List Images
docker images
docker image ls
3. Remove Images
docker rmi ubuntu:latest
docker image rm nginx:alpine
Container Management Commands
1. Create and Run Containers
docker run -d --name web-server nginx
docker run -it ubuntu:latest /bin/bash
2. List Containers
docker ps ## Running containers
docker ps -a ## All containers
3. Start, Stop, Restart Containers
docker start web-server
docker stop web-server
docker restart web-server
4. Remove Containers
docker rm web-server
docker container prune ## Remove all stopped containers
Container Interaction Commands
1. Execute Commands in Container
docker exec -it web-server bash
docker exec web-server ls /var/www/html
2. View Container Logs
docker logs web-server
docker logs -f web-server ## Follow log output
Docker Workflow
graph TD
A[Pull Image] --> B[Create Container]
B --> C[Start Container]
C --> D[Interact/Execute]
D --> E[Stop Container]
E --> F[Remove Container]
Advanced Docker Commands
1. Build Custom Images
docker build -t myapp:v1 .
2. Docker Volumes
docker volume create myvolume
docker run -v myvolume:/app nginx
3. Network Management
docker network create mynetwork
docker run --network mynetwork nginx
Common Docker Command Categories
Category |
Purpose |
Example Commands |
Image Management |
Manage Docker images |
pull, push, build |
Container Management |
Control containers |
run, start, stop |
System Management |
Docker system info |
info, version |
Network Management |
Manage networks |
network create |
Volume Management |
Manage persistent data |
volume create |
Best Practices
- Use specific image tags
- Clean up unused resources
- Use Docker Compose for complex setups
- Implement multi-stage builds
LabEx Docker Learning
LabEx offers interactive environments to practice these Docker commands, helping developers gain practical containerization skills efficiently.
Useful Docker Command Options
Option |
Description |
Example |
-d |
Detached mode |
docker run -d nginx |
-it |
Interactive terminal |
docker run -it ubuntu |
--name |
Name a container |
docker run --name web nginx |
-p |
Port mapping |
docker run -p 8080:80 nginx |