How to Run a Docker Image as a Container

Docker images are the blueprints for containers—they define everything needed to run an application, including the OS layer, libraries, dependencies, and the application itself. But an image by itself is just a static file. To make it useful, you need to run it as a container.

In this guide, we’ll walk through the basics of running a Docker image as a container.


1️⃣ Pulling a Docker Image

If you don’t already have the image locally, you need to pull it from a registry (like Docker Hub).

docker pull nginx

This downloads the nginx image to your local machine.

You can confirm it’s available by listing images:

docker images

2️⃣ Running a Container from an Image

The most basic way to run a container is:

docker run nginx

This will:

  • Create a container from the nginx image
  • Start it with the default command defined in the image’s Dockerfile

⚠️ Note: By default, it runs in the foreground. To keep it running in the background, use detached mode.


3️⃣ Running in Detached Mode

Detached mode lets the container run in the background:

docker run -d nginx

You can now list running containers:

docker ps

4️⃣ Mapping Ports

Containers are isolated, so to access services inside, you need to map ports.

Example: Run Nginx and expose it on port 8080:

docker run -d -p 8080:80 nginx

Now, visit http://localhost:8080 and you’ll see the Nginx welcome page.


5️⃣ Naming Containers

Docker auto-generates container names, but you can assign one yourself:

docker run -d --name my-nginx -p 8080:80 nginx

This makes it easier to manage containers later:

docker stop my-nginx
docker start my-nginx

6️⃣ Interactive Containers

For interactive use (like running Ubuntu or Alpine):

docker run -it ubuntu bash
  • -i → interactive mode (keeps stdin open)
  • -t → allocates a pseudo-TTY

This drops you into a shell inside the container.


📌 Summary

  • Pull an image: docker pull <image>
  • Run in foreground: docker run <image>
  • Run in background (detached): docker run -d <image>
  • Map ports: docker run -d -p host:container <image>
  • Assign a name: docker run --name <container> <image>
  • Interactive shell: docker run -it <image> bash

👉 Running a Docker image as a container is the core of Docker’s workflow. From there, you can manage networking, volumes, and scaling with Docker Compose or Kubernetes.

Sharing Is Caring:

Leave a Comment