DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Running Containers with Docker: A Complete Guide to Creating and Managing Docker Containers

Running Containers with Docker: A Comprehensive Guide

Docker containers are lightweight, portable, and isolated environments that run applications. After building or pulling Docker images, the next step is running containers. This guide will walk you through how to run containers using Docker, explain various Docker commands, and showcase the best practices for managing containers in your environment.


1. Introduction to Docker Containers

Docker containers encapsulate applications and their dependencies into a single, portable unit. This isolation makes it easier to run applications consistently across different environments.

  • Container vs. Image: An image is a read-only template from which containers are created. A container is a running instance of an image.
  • Running a Container: Running a container essentially means creating a container instance from an image, and running it in the background or foreground.

2. Basic Docker Commands to Run Containers

a. docker run

The most fundamental command to run a container in Docker is docker run. It allows you to create a new container from an image and start it.

  • Syntax:
  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Enter fullscreen mode Exit fullscreen mode

b. Example of Running a Container

  • Run a Container in the Foreground (Interactive Mode):
  docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode
  • -it: This option runs the container in interactive mode, attaching your terminal to the container.
  • ubuntu: The Docker image used to run the container (in this case, the official Ubuntu image).

    • Run a Container in the Background (Detached Mode):
  docker run -d nginx
Enter fullscreen mode Exit fullscreen mode
  • -d: Runs the container in detached mode, meaning it runs in the background.

c. Options to docker run

Here are some commonly used options with docker run:

  • -d: Detached mode (run container in the background).
  • -p: Port mapping, maps a container port to a host port.
  docker run -d -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode
  • Maps port 80 of the container to port 8080 on the host machine.

    • --name: Assign a custom name to the container.
  docker run -d --name my_nginx nginx
Enter fullscreen mode Exit fullscreen mode
  • -v: Mount volumes to persist data or share files between the container and host.
  docker run -d -v /host/path:/container/path nginx
Enter fullscreen mode Exit fullscreen mode
  • -e: Set environment variables.
  docker run -d -e "MY_ENV_VAR=value" nginx
Enter fullscreen mode Exit fullscreen mode
  • --rm: Automatically remove the container when it stops.
  docker run --rm ubuntu
Enter fullscreen mode Exit fullscreen mode

3. Managing Running Containers

Once a container is running, Docker provides several commands to inspect, stop, restart, and manage containers.

a. List Running Containers (docker ps)

To view running containers, use the docker ps command:

docker ps
Enter fullscreen mode Exit fullscreen mode

This will display a list of active containers with details such as container ID, names, ports, and status.

  • To list all containers, including stopped ones, use docker ps -a.

b. Stopping Containers (docker stop)

To stop a running container, use the docker stop command:

docker stop container_name_or_id
Enter fullscreen mode Exit fullscreen mode

Example:

docker stop my_nginx
Enter fullscreen mode Exit fullscreen mode

This will gracefully stop the container.

c. Restarting Containers (docker restart)

You can restart a container using:

docker restart container_name_or_id
Enter fullscreen mode Exit fullscreen mode

Example:

docker restart my_nginx
Enter fullscreen mode Exit fullscreen mode

d. Inspecting a Running Container (docker inspect)

To get detailed information about a container (e.g., its configuration, networking, and volumes), use the docker inspect command:

docker inspect container_name_or_id
Enter fullscreen mode Exit fullscreen mode

4. Managing Container Networking

Docker provides several networking modes to manage communication between containers and between containers and the host machine.

a. Default Network Mode

By default, Docker containers are attached to the bridge network mode, which allows containers to communicate with each other using the container's IP address or localhost.

b. Custom Networks

You can create custom networks to allow more fine-grained control over how containers communicate.

  • Create a network:
  docker network create my_network
Enter fullscreen mode Exit fullscreen mode
  • Run containers on a custom network:
  docker run -d --network my_network nginx
Enter fullscreen mode Exit fullscreen mode

c. Host Network Mode

In host network mode, containers use the host's network stack. This can be useful when you want the container to use the host's network directly.

docker run -d --network host nginx
Enter fullscreen mode Exit fullscreen mode

d. Container-to-Container Communication

If containers need to communicate with each other, you can link them or use custom networks.

Example using a custom network:

docker network create my_custom_network
docker run -d --network my_custom_network --name web nginx
docker run -d --network my_custom_network --name db postgres
Enter fullscreen mode Exit fullscreen mode

5. Running Containers with Docker Compose

For multi-container applications, Docker Compose provides an easy way to define and manage containerized services. You define a docker-compose.yml file to specify the services, networks, and volumes.

Example of a docker-compose.yml file:

version: '3.8'

services:
  web:
    image: nginx
    ports:
      - "8080:80"

  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example
Enter fullscreen mode Exit fullscreen mode
  • Start the application:
  docker-compose up
Enter fullscreen mode Exit fullscreen mode
  • Stop the application:
  docker-compose down
Enter fullscreen mode Exit fullscreen mode

6. Best Practices for Running Docker Containers

  • Limit Resource Usage: Use resource constraints to avoid a single container consuming all system resources.
  docker run -d --memory="512m" --cpus="1.0" nginx
Enter fullscreen mode Exit fullscreen mode
  • Use the Right Network Mode: Prefer bridge or custom networks for containers that need to communicate. Use host mode only when necessary for performance.

  • Monitor Containers: Use Docker’s built-in tools to monitor container logs (docker logs), inspect containers (docker inspect), and view resource usage (docker stats).

  • Use Named Volumes for Persistent Data: Avoid relying on host-mounted directories; use Docker-managed named volumes for better portability and data isolation.

  • Automatic Restarts: Use the --restart option to automatically restart containers in case of failure.

Example:

  docker run -d --restart=always nginx
Enter fullscreen mode Exit fullscreen mode

7. Troubleshooting Common Issues

  • Container Won’t Start: Inspect logs using docker logs <container_id> to troubleshoot.
  • Port Conflicts: Ensure the port you're trying to map (e.g., 8080:80) is not already in use on your host system.
  • Container is Restarting Continuously: This may indicate a crash or misconfiguration. Check the logs to find the root cause.

8. Conclusion

Running containers with Docker provides a flexible and efficient way to deploy applications in isolated environments. By mastering basic commands, managing networking, and using tools like Docker Compose, you can easily deploy, scale, and manage your containerized applications. With Docker, the barrier to running applications in production is lowered, and you can achieve consistency across different environments.


Top comments (0)