Docker Commands
This section provides a comprehensive reference for Docker commands, including common use cases and best practices.
Basic Commands
Container Management
# Run a container
docker run [OPTIONS] IMAGE [COMMAND]
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a container
docker stop CONTAINER_ID
# Remove a container
docker rm CONTAINER_ID
# Remove a running container (force)
docker rm -f CONTAINER_ID
Image Management
# List images
docker images
# Pull an image
docker pull IMAGE_NAME
# Build an image
docker build -t IMAGE_NAME .
# Remove an image
docker rmi IMAGE_NAME
Common Use Cases
Running a Web Application
# Run a web application with port mapping
docker run -d -p 8080:80 nginx
# Run with environment variables
docker run -d -e DB_HOST=db -e DB_USER=user myapp
Working with Volumes
# Create a named volume
docker volume create my_volume
# Mount a volume
docker run -v my_volume:/data myapp
# Mount a host directory
docker run -v /host/path:/container/path myapp
Networking
# Create a network
docker network create my_network
# Connect a container to a network
docker network connect my_network container_id
# List networks
docker network ls
Best Practices
- Use Specific Tags: Always use specific version tags instead of
latest
docker pull nginx:1.25.3
- Clean Up Resources: Regularly remove unused containers, images, and volumes
# Remove all stopped containers docker container prune # Remove all unused images docker image prune # Remove all unused volumes docker volume prune
- Use Multi-stage Builds: For smaller production images
# Build stage FROM node:16 AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # Production stage FROM nginx:alpine COPY --from=builder /app/build /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
Troubleshooting
Common Issues
- Container Won’t Start
# Check container logs docker logs CONTAINER_ID # Check container details docker inspect CONTAINER_ID
- Port Conflicts
# Check which process is using a port lsof -i :8080
- Permission Issues
# Run container with correct user docker run -u $(id -u):$(id -g) myapp