Docker Containers
Estimated reading: 4 minutes 34 views

πŸš€ How to Run Docker Images as Containers – Step-by-Step Guide for Beginners

Running containers from images is at the heart of Docker’s functionality. After creating or pulling a Docker image, the next step is to launch it into action. This article walks you through how to run Docker images as containers using practical examples, syntax tips, and real-world use cases.


🧲 Introduction – Why Learn How to Run Docker Images?

In modern DevOps and software deployment workflows, Docker containers enable fast, consistent, and isolated environments for running applications. Learning how to convert an image into a live container is foundational for managing your infrastructure efficiently.

🎯 In this guide, you’ll learn:

  • The difference between Docker images and containers
  • How to run Docker images as containers using various options
  • Real-world examples, port mapping, naming, volumes, and interactive mode
  • Useful commands to manage, stop, or restart containers

πŸ“˜ What’s the Difference Between Docker Image and Docker Container?

ComponentDocker ImageDocker Container
DefinitionBlueprint that includes app, libs, configExecutable runtime instance of the image
MutabilityImmutableMutable (temporary at runtime)
UsageUsed to create containersRuns and performs tasks based on the image

🧠 In short:
Image = Blueprint πŸ—οΈ | Container = Running App 🏒


βœ… Prerequisites

Before starting, ensure the following:

  • 🐳 Docker is installed and running
  • πŸ“₯ You’ve pulled or built a Docker image locally
  • πŸ” You have command-line access (CLI)

πŸ› οΈ How to Run Docker Images as Containers

πŸ“ Docker Run Basic Syntax

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

▢️ Example 1: Docker Run a Container from a Public Image

docker run nginx

βœ… This command:

  • Pulls the NGINX image from Docker Hub (if not available locally)
  • Launches it as a container
  • Stops if not in detached mode

▢️ Example 2: Docker Run in Detached Mode (Background)

docker run -d nginx

βœ… Runs NGINX in the background β€” ideal for servers or background apps.


▢️ Example 3: Assign a Name to Your Container

docker run -d --name mynginx nginx

βœ… Named containers are easier to reference than using random container IDs.


▢️ Example 4: Map Ports Between Host and Container

docker run -d -p 8080:80 nginx

βœ… This maps port 80 inside the container to port 8080 on the host.
Now access the container via: http://localhost:8080


▢️ Example 5: Docker Run with Volume Mounts

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

βœ… Volumes help in:

  • Storing persistent data
  • Sharing files between host and container

▢️ Example 6: Interactive Containers (Run a Shell)

docker run -it ubuntu /bin/bash

βœ… Provides a fully interactive terminal session inside the Ubuntu container.


πŸ“¦ Container Management Commands

ActionCommand
View running containersdocker ps
View all containersdocker ps -a
Stop containerdocker stop <name_or_id>
Remove containerdocker rm <name_or_id>
Restart stoppeddocker start <name_or_id>
Auto-restartdocker run -d --restart unless-stopped ...

✍️ Example:

docker stop mynginx
docker rm mynginx
docker start mynginx

πŸ“š Docker Run Real-World Use Case Example

Let’s say you’ve built a custom Node.js app and tagged it as my-node-app.

Run it like this:

docker run -d -p 3000:3000 --name nodeapp my-node-app

πŸŽ‰ This deploys your Node.js app, running it inside a secure and isolated container.


πŸ“Œ Summary – Run Docker Images as containers

Running Docker images as containers is the gateway to containerized application development. Whether deploying a web server, testing environments, or scaling microservices, this command is your starting point.

πŸ” Key Takeaways:

  • docker run launches containers from images with various options
  • Use -d for background, -p for port mapping, -v for volumes
  • Manage containers with docker ps, stop, rm, and start
  • Named containers are easier to manage in real projects

βš™οΈ Real-world relevance: Mastering this lets you automate deployments and maintain app consistency across dev, staging, and production.


❓ Frequently Asked Questions (FAQs)

πŸ” Q1: What’s the difference between docker run and docker start?
βœ… docker run creates and starts a new container.
βœ… docker start restarts a stopped container.


πŸ” Q2: How do I see logs from a running container?

docker logs <container_name>
docker logs -f mynginx   # To follow logs live

πŸ” Q3: How do I check which ports a container is using?

docker port <container_name>
docker ps   # Also shows port mappings

πŸ” Q4: Can I run multiple containers from the same image?
βœ… Absolutely! Example:

docker run -d --name nginx1 nginx
docker run -d --name nginx2 nginx

πŸ” Q5: What happens when I delete a container?
βœ… Only the container is deleted. The original image remains for reuse.


πŸ” Q6: How do I remove all stopped containers?

docker container prune

⚠️ Deletes all stopped containers. Use cautiously.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

How to run Docker Images as containers

Or Copy Link

CONTENTS
Scroll to Top