How to Run a Docker Image After Pulling from Docker Hub
After pulling an image from Docker Hub, you can run it as a container using the docker run command. Below is a step-by-step guide with examples.
Step 1: Pull the Image from Docker Hub
Before running an image, you need to download it.
Command:
docker pull <image-name>:<tag>
<image-name>: Name of the image (e.g.,nginx,ubuntu).<tag>: Version of the image (default islatestif not specified).
Example:
docker pull nginx:latest # Pulls the latest Nginx image
docker pull ubuntu:20.04 # Pulls Ubuntu 20.04
Verify the Pulled Image
docker images # Lists all downloaded images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest abc123... 2 weeks ago 133MB
ubuntu 20.04 def456... 3 weeks ago 72.8MB
Step 2: Run the Docker Image
Use docker run to create and start a container from the image.
Basic Syntax:
docker run [options] <image-name>
Common options:
| Option | Description |
|---|---|
-d |
Run in detached (background) mode |
-p <host-port>:<container-port> |
Map host port to container port |
--name <container-name> |
Assign a custom name to the container |
-it |
Interactive mode (for shells like bash) |
-v <host-path>:<container-path> |
Mount a volume |
Examples:
1. Run a Simple Container
docker run nginx # Starts Nginx in the foreground
- Press
Ctrl+Cto stop it.
2. Run in Detached Mode (Background)
docker run -d --name my-nginx nginx
- Check running containers:
docker ps
3. Expose a Port (e.g., Nginx on Port 8080)
docker run -d -p 8080:80 --name web-server nginx
- Access Nginx at:
http://localhost:8080
4. Run an Interactive Shell (e.g., Ubuntu)
docker run -it ubuntu bash
- This opens a shell inside the container.
- Type
exitto leave.
5. Mount a Volume (Persistent Storage)
docker run -v /host/folder:/container/folder nginx
Step 3: Managing the Running Container
Stop a Container
docker stop <container-name-or-id>
Example:
docker stop my-nginx
Start a Stopped Container
docker start <container-name-or-id>
Remove a Container
docker rm <container-name-or-id>
Remove an Image
docker rmi <image-name>
Common Use Cases
1. Running a MySQL Database
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 mysql:latest
2. Running a Python Flask App
docker run -d -p 5000:5000 --name flask-app my-python-image
3. Running Redis (In-Memory Database)
docker run -d --name my-redis -p 6379:6379 redis
Conclusion
- Pull an image →
docker pull <image> - Run it →
docker run [options] <image> - Manage containers →
docker ps,docker stop,docker rm
Now you can easily run any Docker image from Docker Hub! 🚀
Got questions? Ask in the comments! 🐳
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.