Skip to main content

Creating and Managing Docker Containers

Introduction

With a solid understanding of Docker images, it's time to bring them to life by creating and managing containers. Containers are the execution units of Docker—lightweight, portable, and isolated environments that run your applications. In this lesson, you'll learn how to create, start, stop, remove, and inspect containers, as well as explore best practices and common pitfalls.


What is a Docker Container?

A Docker container is an instance of a Docker image. It encapsulates an application and its dependencies, running as an isolated process on your host machine. Containers are ephemeral by default: they can be started, stopped, deleted, and re-created with ease.


Creating Containers

The docker run Command

The primary way to create (and start) a container is with the docker run command.

Syntax:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Example:

docker run hello-world

This command:

  • Downloads the hello-world image (if not present).
  • Creates a new container based on the image.
  • Runs the default command defined in the image.
  • Prints output and exits.

Running an Interactive Container

Some applications require interaction. Use the -it flags to run a container in interactive mode with a terminal.

Example:

docker run -it ubuntu /bin/bash

This opens a Bash shell inside the container, allowing you to interact directly.

Running in Detached Mode

To run a container in the background, use the -d flag:

docker run -d nginx

This starts an Nginx web server container and prints the container ID.


Managing Containers

Listing Containers

  • Running containers:
    docker ps
  • All containers (including stopped):
    docker ps -a

Stopping and Restarting Containers

  • Stop a running container:
    docker stop <container_id_or_name>
  • Start a stopped container:
    docker start <container_id_or_name>
  • Restart a container:
    docker restart <container_id_or_name>

Removing Containers

To clean up unused containers, remove them with:

docker rm <container_id_or_name>

Remove all stopped containers:

docker container prune

Note: You cannot remove a running container. Stop it first.


Inspecting Containers

Viewing Logs

To see the output of a containerized application:

docker logs <container_id_or_name>

Inspecting Container Details

To get detailed information about a container (network settings, mounts, etc.):

docker inspect <container_id_or_name>

Naming and Referencing Containers

By default, Docker assigns random names to containers (e.g., flamboyant_bardeen). You can specify a name for easier management:

docker run --name my-nginx -d nginx

Now you can refer to this container by the name my-nginx.


Use Cases

  • Web Development: Run web servers (e.g., Nginx, Apache) in isolated environments.
  • Testing: Test different versions of software without system conflicts.
  • Scripting: Run one-off tasks or scripts in lightweight containers without polluting the host.

Common Mistakes and Pitfalls

  • Forgetting to Clean Up: Not removing unused containers can consume disk space.
  • Not Using Detached Mode for Servers: Running long-lived services without -d keeps the terminal busy.
  • Port Collisions: Two containers can't bind to the same host port. Use the -p flag to map ports explicitly.
  • Data Loss: Containers are ephemeral. Data written inside the container is lost when it is removed unless volumes are used (covered in the next lesson).

Summary

  • Containers are instances of images and provide isolated environments for applications.
  • Use docker run to create and start containers, with options for interactive or detached modes.
  • Manage containers with docker ps, docker stop, docker start, docker rm, and related commands.
  • Inspect and debug containers using docker logs and docker inspect.
  • Give containers meaningful names for easier management.
  • Avoid common pitfalls like orphaned containers and data loss.

Quiz

1. What is the primary difference between a Docker image and a Docker container?
A) Images are running processes, containers are static files
B) Images are templates, containers are running instances of those templates
C) Images and containers are the same
D) Containers store Dockerfiles

Details

Answer B) Images are templates, containers are running instances of those templates


2. Which command would you use to run a container in the background?
A) docker run
B) docker run -it
C) docker run -d
D) docker start -d

Details

Answer C) docker run -d


3. True or False: You can remove a running container with docker rm without stopping it first.

Details

Answer False. You must stop the container before removing it.


4. How do you view the logs produced by a running container?
A) docker ps
B) docker logs <container_id_or_name>
C) docker inspect <container_id_or_name>
D) docker run logs

Details

Answer B) docker logs <container_id_or_name>


5. Why is it important to assign meaningful names to containers?
A) It is required by Docker
B) It helps in easily referencing and managing containers
C) It improves container performance
D) It decreases image size

Details

Answer B) It helps in easily referencing and managing containers