Docker Containers
Estimated reading: 4 minutes 6 views

๐Ÿš€ How to Run Docker Images as Containers

Containers are the core of Docker’s functionality. Once you have a Docker image, the next crucial step is running it as a container. This guide walks you through everything you need to know to successfully launch Docker containers from imagesโ€”whether you’re a beginner or brushing up your skills.


๐Ÿง  Whatโ€™s the Difference Between Docker Image and Docker Container?

  • Docker Image: A snapshot or blueprint that contains the application, libraries, and dependencies.
  • Docker Container: A running instance of a Docker image. Itโ€™s the actual executable unit that can perform tasks.

In short:

Image = Blueprint ๐Ÿ—๏ธ | Container = Building ๐Ÿข


๐Ÿ› ๏ธ Prerequisites

Before running Docker images, ensure the following:

  • Docker is installed and running on your machine.
  • You have pulled or built a Docker image.

๐Ÿ”— Need help installing Docker? Check out:
How to Install Docker on Linux โ€“ Complete Guide (replace with actual link)


โœ… How to Run Docker Images as Containers

๐Ÿ“ Syntax:

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

โ–ถ๏ธ Example 1: Run a Container from a Public Image

Letโ€™s say you want to run the official NGINX image:

docker run nginx

๐ŸŽฏ This command will:

  • Pull the nginx image (if not already downloaded)
  • Run it as a container
  • Automatically stop if not set to detach mode

โ–ถ๏ธ Example 2: Run in Detached Mode (Background)

docker run -d nginx

๐Ÿงช What Happens:

  • The container runs in the background
  • Useful for web servers or long-running processes

โ–ถ๏ธ Example 3: Assign a Name to Your Container

docker run -d --name mynginx nginx

๐Ÿ”– Why Use Names?

  • Easier to manage and reference containers by name instead of container ID

โ–ถ๏ธ Example 4: Map Ports (Host โ†”๏ธ Container)

docker run -d -p 8080:80 nginx

๐ŸŒ Port Mapping:

  • Maps port 80 inside the container to port 8080 on your host machine
  • Now, you can access NGINX at http://localhost:8080

โ–ถ๏ธ Example 5: Run with Volume Mounts

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

๐Ÿ’พ Volumes Help With:

  • Persistent storage
  • Sharing data between container and host

โ–ถ๏ธ Example 6: Interactive Containers

docker run -it ubuntu /bin/bash

๐Ÿ–ฅ๏ธ What You Get:

  • An interactive terminal session inside the Ubuntu container

๐Ÿ“ฆ List Running and All Containers

docker ps        # List running containers
docker ps -a # List all containers (including stopped ones)

๐Ÿ›‘ Stop and Remove Containers

docker stop <container_name_or_id>      # Stops container
docker rm <container_name_or_id> # Removes container

โœ๏ธ Example:

docker stop mynginx
docker rm mynginx

๐Ÿ” Reuse and Restart Containers

Restart a stopped container:

docker start mynginx

Automatically restart container on failure or reboot:

docker run -d --restart unless-stopped nginx

๐Ÿ“š Real-World Use Case

Letโ€™s say you have a custom Node.js app built into an image called my-node-app.

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

Youโ€™ve now successfully deployed your application in an isolated environment. ๐ŸŽ‰


๐Ÿงฉ Final Thought

Running Docker images as containers is a foundational skill in containerization and DevOps workflows. With just a few commands, you can deploy apps, test environments, or entire services in isolated and portable containers.


๐Ÿ’ก Pro Tip: Always name your containers, map essential ports, and use volumes for data persistence!

โ“ Frequently Asked Questions (FAQs)

๐Ÿ” Q1: Whatโ€™s the difference between docker run and docker start?

A: docker run creates and starts a new container. docker start restarts a previously stopped container.


๐Ÿ” Q2: How can I view the logs of a running container?

A:

docker logs <container_name_or_id>

Use -f to follow logs:

docker logs -f mynginx

๐Ÿ” Q3: How do I know which ports my container is using?

A:

docker port <container_name_or_id>

Or check with:

docker ps

๐Ÿ” Q4: Can I run multiple containers from the same image?

A: Absolutely! Each container runs in isolation:

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

๐Ÿ” Q5: What happens when I delete a container?

A: Deleting a container does not delete the image. You can reuse the image to launch new containers.


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

A:

docker container prune

โš ๏ธ This deletes all stopped containers. Use with caution.



Leave a Reply

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

Share this Doc

How to run Docker Images as containers

Or copy link

CONTENTS
Scroll to Top