Container Management (Docker commands)
Category: Advanced Linux Administration
Type: Linux Commands
Generated on: 2025-07-10 03:15:41
For: System Administration, Development & Technical Interviews
Docker Container Management Cheatsheet (Sysadmin & Developer Focused)
Section titled “Docker Container Management Cheatsheet (Sysadmin & Developer Focused)”This cheatsheet covers essential Docker commands for managing containers, useful for both developers and system administrators.
1. Command Overview
Docker containers are isolated environments running applications. These commands allow you to create, run, manage, monitor, and troubleshoot these containers. They are fundamental for deploying and managing applications in a containerized environment.
2. Basic Syntax
docker <command> [options] <arguments>3. Practical Examples
- Running a simple container:
docker run hello-worldExpected Output:
Unable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world...Hello from Docker!This message shows that your installation appears to be working correctly....- Running a container in detached mode (background):
docker run -d --name my_nginx -p 8080:80 nginxExpected Output:
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6- Listing running containers:
docker psExpected Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1b2c3d4e5f6 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp my_nginx- Listing all containers (including stopped ones):
docker ps -a- Stopping a container:
docker stop my_nginxExpected Output:
my_nginx- Starting a stopped container:
docker start my_nginxExpected Output:
my_nginx- Restarting a container:
docker restart my_nginxExpected Output:
my_nginx- Removing a container:
docker rm my_nginxExpected Output:
my_nginx- Removing a running container (force):
docker rm -f my_nginxWARNING: This will forcefully stop and remove the container.
- Executing a command inside a running container:
docker exec -it my_nginx bashThis will open a bash shell inside the my_nginx container.
- Viewing container logs:
docker logs my_nginx- Viewing container resource usage (live):
docker stats my_nginx4. Common Options
| Option | Description | Example |
|---|---|---|
-d | Run the container in detached mode (background). | docker run -d ... |
--name | Assign a name to the container. | docker run --name my_app ... |
-p | Publish a container’s port(s) to the host. host_port:container_port | docker run -p 8080:80 ... |
-v | Bind mount a volume. host_path:container_path | docker run -v /data:/var/www/html ... |
-e | Set environment variables. | docker run -e MY_VAR=value ... |
--restart | Restart policy for the container (e.g., always, on-failure). | docker run --restart always ... |
-it | Interactive terminal. Combines -i (interactive) and -t (allocate a pseudo-TTY). Often used for shells. | docker exec -it my_container bash |
-f | Force the command (e.g., remove a running container). | docker rm -f my_container |
--rm | Automatically remove the container when it exits. | docker run --rm ... |
--network | Connect a container to a network. | docker run --network my_network ... |
--privileged | Give the container extended privileges. Use with caution! | docker run --privileged ... |
--user | Run the process inside the container as a specific user. | docker run --user 1000:1000 ... |
5. Advanced Usage
- Creating a custom network:
docker network create my_network- Connecting a container to a custom network:
docker run -d --name my_app --network my_network my_image- Creating a volume:
docker volume create my_volume- Mounting a volume to a container:
docker run -d -v my_volume:/data my_image- Inspecting a container:
docker inspect my_nginxThis will output a JSON document with detailed information about the container.
- Copying files from/to a container:
docker cp my_nginx:/path/to/file /host/path/docker cp /host/path/ my_nginx:/path/to/destination/- Using health checks: Define a health check in your Dockerfile, and Docker will monitor the container’s health.
Example Dockerfile:
FROM nginx:latestHEALTHCHECK --interval=5s --timeout=3s CMD curl -f http://localhost/ || exit 1Then use docker ps to see the health status.
- Using Docker Compose for multi-container applications: Define your application stack in a
docker-compose.ymlfile and manage it as a single unit. (See separate Docker Compose cheatsheet).
6. Tips & Tricks
- Use container names instead of IDs: Container names are easier to remember and manage.
- Use
--restart alwaysfor critical containers: This ensures that the container restarts automatically if it crashes. - Use volumes for persistent data: This ensures that data is not lost when the container is removed.
- Use Docker Compose for complex applications: This simplifies the management of multi-container applications.
- Keep your images small: Smaller images download faster and consume less disk space. Use multi-stage builds to optimize image size.
- Use
.dockerignore: Exclude unnecessary files from your build context to improve build performance. - Use a robust logging solution: Docker logs are useful for debugging, but for production environments, consider using a dedicated logging solution like ELK stack (Elasticsearch, Logstash, Kibana) or Graylog.
- Regularly prune unused resources: Use
docker system prune -ato remove unused images, containers, volumes, and networks. WARNING: This will remove all stopped containers, unused networks and dangling images. Use with caution. Usedocker system prunefor a safer option, only removing dangling resources. - Use
docker topto see the processes running inside the container: This can be useful for troubleshooting performance issues.
7. Troubleshooting
- Container fails to start:
- Check the container logs (
docker logs <container_id>). - Verify that all required ports are available on the host.
- Check for errors in the Dockerfile.
- Ensure that the image exists.
- Check the container logs (
- Container exits unexpectedly:
- Check the container logs (
docker logs <container_id>). - Verify that the application inside the container is running correctly.
- Check for resource constraints (e.g., memory, CPU).
- Check the container logs (
- Cannot connect to the container:
- Verify that the container is running (
docker ps). - Check the port mappings (
docker ps). - Ensure that the network configuration is correct.
- Check firewall rules.
- Verify that the container is running (
- “No such container” error:
- Verify that the container name or ID is correct.
- Ensure that the container has not been removed.
- Disk space issues:
- Docker images and containers can consume a lot of disk space. Use
docker system dfto see disk space usage. - Regularly prune unused resources (
docker system prune -a). - Consider using a separate volume for Docker data.
- Docker images and containers can consume a lot of disk space. Use
- Permission Denied Errors: Ensure the user inside the container has the necessary permissions to access files and directories. Use the
--userflag or change ownership inside the Dockerfile.
8. Related Commands
- Image Management:
docker build,docker pull,docker push,docker images,docker rmi - Network Management:
docker network create,docker network connect,docker network disconnect,docker network ls,docker network inspect - Volume Management:
docker volume create,docker volume inspect,docker volume ls,docker volume rm - Docker Compose:
docker-compose up,docker-compose down,docker-compose ps,docker-compose logs - Docker Swarm: (For orchestrating containers across multiple hosts - outside the scope of this cheatsheet, but related).
This cheatsheet provides a foundation for managing Docker containers. Refer to the official Docker documentation for more detailed information and advanced features. Always test commands in a non-production environment before applying them to production systems.