The Complete Guide to Docker Container Management

Hey there! Working with Docker containers? I‘m going to walk you through the 15 most vital Docker commands for managing containers.

Whether you‘re just getting started with Docker or have some experience, this guide will give you a firm grasp of container management. I‘ll provide plenty of examples so you can apply these Docker tricks confidently.

Let‘s quickly recap what Docker containers are all about, discuss why they‘ve become so popular, and see how core Docker components fit together.

Why Docker Matters Today

As developers, we‘re increasingly building sophisticated, microservices-based applications that have complex dependencies. Deploying and running these apps consistently across environments is tough.

This is where Docker shines. Docker containers encapsulate your application plus its dependencies into a single standardized unit. This container can then run smoothly regardless of the target infrastructure.

No wonder Docker adoption has skyrocketed:

  • 90% of IT organizations now use containers in production based on a recent Flexera report
  • 5 million developers actively use Docker as per Docker Inc
  • Leading cloud platforms like AWS, Google Cloud and Azure all support container deployments

Beyond deploying apps faster, containers also save resources through consolidation and automation. Large organizations have reported 50% cost savings and 80% reductions in deployment failures after switching to Docker.

Now let‘s quickly run through the key concepts before diving into managing containers.

Docker Concepts in a Nutshell

Docker packages applications within images that can be instantiated as containers. Images and containers comprise the core of the Docker ecosystem:

  • Docker Engine: The back-end service that runs on hosts and handles building/running images and containers
  • Docker CLI: Command line tool that talks to the Docker Engine
  • Images: Read-only templates used to create container instances
  • Containers: Running instances of Docker images
  • Dockerfile: Text file with steps to build a Docker image
  • Registry: Central hub to store and distribute images

That covers Docker basics! Let‘s now explore those critical container management commands.

Running Containers with docker run

The fundamental command for launching new containers from images is docker run. The syntax is simple:

$ docker run image_name 

For example:

$ docker run nginx

This fires up a new container using the latest nginx image, pulls the image from Docker Hub if missing locally, and starts Nginx.

Some useful docker run options:

  • -d – Detached/background mode
  • --name – Name containers meaningfully
  • -p – Publish ports for external access
  • -e – Set environment variables
  • -v – Mount host directories as data volumes

Here‘s an example with some of these parameters:

$ docker run -d --name my_friendly_nginx -p 8000:80 -v ${PWD}:/usr/share/nginx/html nginx  

This runs Nginx exposed on port 8000, mounted with a custom HTML folder from the host filesystem. The container is named clearly as well.

Listing Existing Containers: docker ps

The standard command to list containers on a Docker host is docker ps. With no arguments, it shows running containers:

$ docker ps 

CONTAINER ID  IMAGE         COMMAND                 CREATED       STATUS       PORTS     NAMES
dcad7fcb298b  nginx:latest  "/docker-entrypoint...."  5 hours ago   Up 5 hours   80/TCP    my_friendly_nginx

To include stopped containers too, use docker ps -a:

$ docker ps -a

CONTAINER ID  IMAGE         COMMAND                 CREATED       STATUS                    PORTS     NAMES
dcad7fcb298b  nginx:latest  "/docker-entrypoint...."  5 hours ago   Up 5 hours                80/TCP    my_friendly_nginx   
f63abdc2dae8  nginx:latest  "/docker-entrypoint...."  5 hours ago   Exited (0) 5 hours ago              boring_lewin   

Now you can view all containers on your system.

Stopping and Removing Containers

You may need to stop containers gracefully or clean up unwanted ones completely. Here‘s how:

  • Stop containers (like shutting down cleanly) using docker stop:
$ docker stop my_friendly_nginx
  • Remove stopped containers to clear clutter with docker rm:
$ docker rm boring_lewin

Some good container hygiene tips:

  • Avoid force-stopping containers using docker kill to prevent data corruption
  • Delete unused stopped containers to save disk space
  • Define restart policies suited to your application

Speaking of application health, what about looking inside running containers?

Executing Commands: docker exec

To run shell commands or interact with running containers, use docker exec.

For example, to start an interactive Bash shell inside your my_friendly_nginx container:

$ docker exec -it my_friendly_nginx bash 

From inside this shell, you can now run Linux commands to inspect the container OS, check logs, tweak configurations etc.

Some other handy examples of docker exec:

  • Check environment variables with docker exec my_nginx env
  • Inspect running processes using docker exec my_nginx ps aux
  • Tail logs continuously using docker exec -it my_nginx tail -f access.log

So docker exec opens up tons of troubleshooting scenarios!

Output truncated for brevity...