Docker is a powerful tool that allows you to build, package, and deploy applications using containers. One of the most fundamental operations in Docker is running a container from an image.
In this blog, we’ll walk through exactly how to run a Docker image as a container, explain what happens behind the scenes, and show practical examples you can start using today.
🧩 What Is a Docker Image?
A Docker image is a snapshot of a file system that includes everything needed to run an application—code, dependencies, environment variables, and configuration.
Images are read-only and can be stored in Docker Hub or a private registry.
🧱 What Is a Docker Container?
A Docker container is a running instance of an image. It adds a writable layer on top of the image so your application can run and store temporary changes.
Think of it like this:
Concept | Analogy |
---|---|
Image | Blueprint |
Container | Building constructed from the blueprint |
🚀 How to Run a Docker Image as a Container
To run a Docker image, use the docker run
command:
docker run [OPTIONS] IMAGE_NAME[:TAG] [COMMAND]
✅ Basic Example:
docker run nginx
This command:
- Downloads the
nginx
image (if not already present) - Creates a new container
- Starts the Nginx web server
- Outputs logs to the terminal
🔧 Common docker run
Options
Option | Description |
---|---|
-d | Run container in background (detached mode) |
-p | Map ports (host:container ) |
--name | Assign a name to the container |
-it | Interactive mode (e.g., for shells) |
--rm | Automatically remove the container after it exits |
💡 Example: Run Nginx in Detached Mode
docker run -d -p 8080:80 --name webserver nginx
This will:
- Run Nginx in the background
- Map port 80 in the container to port 8080 on your machine
- Name the container
webserver
Open your browser and visit http://localhost:8080 to see Nginx running.
💡 Example: Run a Container Interactively
docker run -it ubuntu bash
This starts an Ubuntu container and drops you into an interactive shell (bash
). You can run commands like you’re inside a Linux VM.
🛑 How to Stop and Remove Containers
To stop a container:
docker stop <container_id_or_name>
To remove it:
docker rm <container_id_or_name>
Or combine both using:
docker run --rm ubuntu echo "Hello from Ubuntu"
This runs the container once and removes it after the command completes.
🧠 Summary
Command | What It Does |
---|---|
docker run nginx | Runs Nginx container interactively |
docker run -d -p 8080:80 nginx | Runs Nginx in background with port mapping |
docker run -it ubuntu bash | Opens an interactive Ubuntu shell |
docker stop <name> | Stops a running container |
docker rm <name> | Deletes a container |
--rm | Auto-remove container after run |
🏁 Final Thoughts
Running a Docker image as a container is a fundamental building block of containerized development. With just a single command, you can launch full environments, test apps, and deploy services in seconds.
As you continue working with Docker, mastering the docker run
command—and its options—will help you build faster, deploy smarter, and work more efficiently.