Skip to content

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

Terminal window
docker <command> [options] <arguments>

3. Practical Examples

  • Running a simple container:
Terminal window
docker run hello-world

Expected Output:

Unable to find image 'hello-world:latest' locally
latest: 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):
Terminal window
docker run -d --name my_nginx -p 8080:80 nginx

Expected Output:

a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
  • Listing running containers:
Terminal window
docker ps

Expected Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp my_nginx
  • Listing all containers (including stopped ones):
Terminal window
docker ps -a
  • Stopping a container:
Terminal window
docker stop my_nginx

Expected Output:

my_nginx
  • Starting a stopped container:
Terminal window
docker start my_nginx

Expected Output:

my_nginx
  • Restarting a container:
Terminal window
docker restart my_nginx

Expected Output:

my_nginx
  • Removing a container:
Terminal window
docker rm my_nginx

Expected Output:

my_nginx
  • Removing a running container (force):
Terminal window
docker rm -f my_nginx

WARNING: This will forcefully stop and remove the container.

  • Executing a command inside a running container:
Terminal window
docker exec -it my_nginx bash

This will open a bash shell inside the my_nginx container.

  • Viewing container logs:
Terminal window
docker logs my_nginx
  • Viewing container resource usage (live):
Terminal window
docker stats my_nginx

4. Common Options

OptionDescriptionExample
-dRun the container in detached mode (background).docker run -d ...
--nameAssign a name to the container.docker run --name my_app ...
-pPublish a container’s port(s) to the host. host_port:container_portdocker run -p 8080:80 ...
-vBind mount a volume. host_path:container_pathdocker run -v /data:/var/www/html ...
-eSet environment variables.docker run -e MY_VAR=value ...
--restartRestart policy for the container (e.g., always, on-failure).docker run --restart always ...
-itInteractive terminal. Combines -i (interactive) and -t (allocate a pseudo-TTY). Often used for shells.docker exec -it my_container bash
-fForce the command (e.g., remove a running container).docker rm -f my_container
--rmAutomatically remove the container when it exits.docker run --rm ...
--networkConnect a container to a network.docker run --network my_network ...
--privilegedGive the container extended privileges. Use with caution!docker run --privileged ...
--userRun the process inside the container as a specific user.docker run --user 1000:1000 ...

5. Advanced Usage

  • Creating a custom network:
Terminal window
docker network create my_network
  • Connecting a container to a custom network:
Terminal window
docker run -d --name my_app --network my_network my_image
  • Creating a volume:
Terminal window
docker volume create my_volume
  • Mounting a volume to a container:
Terminal window
docker run -d -v my_volume:/data my_image
  • Inspecting a container:
Terminal window
docker inspect my_nginx

This will output a JSON document with detailed information about the container.

  • Copying files from/to a container:
Terminal window
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:latest
HEALTHCHECK --interval=5s --timeout=3s CMD curl -f http://localhost/ || exit 1

Then use docker ps to see the health status.

  • Using Docker Compose for multi-container applications: Define your application stack in a docker-compose.yml file 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 always for 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 -a to remove unused images, containers, volumes, and networks. WARNING: This will remove all stopped containers, unused networks and dangling images. Use with caution. Use docker system prune for a safer option, only removing dangling resources.
  • Use docker top to 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.
  • 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).
  • 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.
  • “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 df to see disk space usage.
    • Regularly prune unused resources (docker system prune -a).
    • Consider using a separate volume for Docker data.
  • Permission Denied Errors: Ensure the user inside the container has the necessary permissions to access files and directories. Use the --user flag 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.