How to Run Containers

Introduction Containers have revolutionized the way software is developed, shipped, and run. They provide a lightweight, portable environment that encapsulates an application and its dependencies, ensuring consistency across different computing environments. Understanding how to run containers is essential for developers, system administrators, and IT professionals aiming to improve deployment eff

Nov 17, 2025 - 10:53
Nov 17, 2025 - 10:53
 2

Introduction

Containers have revolutionized the way software is developed, shipped, and run. They provide a lightweight, portable environment that encapsulates an application and its dependencies, ensuring consistency across different computing environments. Understanding how to run containers is essential for developers, system administrators, and IT professionals aiming to improve deployment efficiency, scalability, and resource utilization.

This tutorial offers a comprehensive, step-by-step guide on running containers, covering foundational concepts, practical instructions, best practices, essential tools, real-world examples, and frequently asked questions. By the end, you will be equipped with the knowledge to confidently deploy and manage containers in various environments.

Step-by-Step Guide

1. Understanding Containers

Before running containers, its important to grasp what containers are. A container is a standardized unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Containers share the host system's kernel but run in isolated user spaces.

2. Installing Container Runtime

The most popular container runtime is Docker. To run containers, you first need to install Docker on your machine.

Installation steps for Docker:

  • Visit the official Docker website and download the Docker Desktop for your operating system (Windows, macOS, Linux).
  • Follow the installation instructions.
  • Verify installation by running docker --version in your terminal or command prompt.

3. Pulling a Container Image

Containers are created from images. Images are read-only templates with instructions for creating containers. You can pull official images from Docker Hub or other container registries.

Example command to pull an Ubuntu image:

docker pull ubuntu

4. Running Your First Container

Once the image is pulled, you can start a container. The basic Docker run command starts a container from an image.

Example:

docker run -it ubuntu

This command runs an Ubuntu container in interactive mode with a terminal attached.

5. Managing Running Containers

To view running containers, use:

docker ps

To see all containers (running and stopped):

docker ps -a

To stop a running container:

docker stop [container_id]

To remove a container:

docker rm [container_id]

6. Running Containers in Detached Mode

Sometimes, you want containers to run in the background (detached mode).

Example:

docker run -d nginx

This runs the Nginx container in detached mode.

7. Mapping Ports

To access services running inside a container, you need to map container ports to your host machine ports.

Example:

docker run -d -p 8080:80 nginx

This maps port 80 inside the container to port 8080 on the host.

8. Persisting Data with Volumes

Containers are ephemeral by default, so use volumes to persist data.

Example:

docker run -d -v /host/path:/container/path nginx

This mounts a directory from the host to the container.

9. Inspecting Container Logs

To view logs from a running container:

docker logs [container_id]

10. Removing Images

To free up space, remove unused images:

docker rmi [image_id]

Best Practices

1. Use Official and Trusted Images

Always prefer official images or those from trusted sources to ensure security and reliability.

2. Keep Images Lightweight

Use minimal base images like Alpine Linux to reduce image size and improve performance.

3. Regularly Update Images

Keep your images updated to avoid vulnerabilities and bugs.

4. Avoid Running Containers as Root

For security, run containers with non-root users whenever possible.

5. Use Environment Variables for Configuration

Pass configuration settings via environment variables to keep images generic and reusable.

6. Clean Up Unused Containers and Images

Regularly prune unused containers and images to conserve disk space.

7. Monitor Container Performance

Use monitoring tools to track resource usage and optimize containerized applications.

Tools and Resources

1. Docker

The most widely used containerization platform with a vast ecosystem.

2. Kubernetes

An open-source system for automating deployment, scaling, and management of containerized applications.

3. Podman

An alternative to Docker that runs containers without a daemon.

4. Docker Hub

A cloud-based registry service for sharing container images.

5. Container Orchestration Tools

Tools like Docker Swarm and Apache Mesos help manage clusters of containers.

6. Monitoring Tools

Prometheus, Grafana, and cAdvisor provide container monitoring and metrics collection.

7. Learning Resources

Real Examples

Example 1: Running a Simple Web Server

Deploy an Nginx web server container accessible on your local machine.

Commands:

docker run -d -p 8080:80 nginx

Access the web server by opening http://localhost:8080 in your browser.

Example 2: Running a Python Application in a Container

Create a Dockerfile for a Python app:

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Build and run the container:

docker build -t my-python-app .

docker run -it --rm my-python-app

Example 3: Using Volumes to Persist Data

Run a MySQL container with persistent data storage:

docker run -d -p 3306:3306 -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

This will keep your database data even if the container is removed.

FAQs

Q1: What is the difference between a container and a virtual machine?

Containers share the host OS kernel and isolate the application processes, making them lightweight and faster to start. Virtual machines run a full guest OS on top of a hypervisor, which requires more resources.

Q2: Can I run containers on any operating system?

Containers run natively on Linux. For Windows and macOS, Docker uses a lightweight virtual machine to run Linux containers.

Q3: How do I expose multiple ports from a container?

You can map multiple ports using multiple -p flags, for example: docker run -p 8080:80 -p 443:443 nginx.

Q4: What happens to data inside a container when it is stopped?

By default, data inside a container is ephemeral and lost when the container is removed. Use volumes or bind mounts to persist data.

Q5: How do I update a running container?

You typically update the container image and redeploy the container since containers are immutable.

Conclusion

Running containers is a fundamental skill in modern software development and IT operations. Containers bring portability, efficiency, and scalability to application deployment. This tutorial has provided you with the essential knowledge to start running containers effectively, from installation to best practices and real-world examples.

As container technology continues to evolve, staying updated with tools and best practices will help you maximize the benefits of containerization in your projects and infrastructure.