Docker Container Tutorial: Run Docker Images Step by Step
Docker Container Tutorial: Run Docker Images Step by Step
Docker is one of the most popular containerization platforms used by developers, DevOps engineers, and cloud engineers. With Docker, you can package applications into containers and run them consistently on any system.
In this tutorial, you will learn how to run Docker containers using Docker images with practical examples and beginner-friendly explanations.
What is Docker?
Docker is an open-source platform that helps developers build, ship, and run applications inside lightweight containers.
A Docker container includes:
- Application code
- Dependencies
- Libraries
- Runtime environment
This ensures the application works the same everywhere.
What is a Docker Image?
A Docker image is a blueprint used to create containers.
Examples:
- Ubuntu image
- Nginx image
- MySQL image
- Node.js image
You download Docker images from Docker Hub and run them as containers.
Docker Architecture
Docker works using:
| Component | Description |
|---|---|
| Docker Engine | Core Docker service |
| Docker Image | Template for containers |
| Docker Container | Running instance of image |
| Docker Hub | Online image repository |
Install Docker on Ubuntu
Update System
sudo apt update
Install Docker
sudo apt install docker.io -y
Start Docker Service
sudo systemctl start docker
Enable Docker
sudo systemctl enable docker
Check Docker Version
docker --version
Example Output:
Docker version 28.0.1
Verify Docker Installation
Run the hello-world container:
docker run hello-world
This command:
- Downloads image from Docker Hub
- Creates container
- Runs container
- Displays success message
Docker Images Commands
List Docker Images
docker images
Example Output:
REPOSITORY TAG IMAGE ID SIZE
ubuntu latest a04dc4851cbc 77MB
nginx latest 605c77e624dd 142MB
Pull Docker Image
Download image from Docker Hub.
Pull Ubuntu Image
docker pull ubuntu
Pull Nginx Image
docker pull nginx
Run Docker Container
Run Ubuntu Container
docker run ubuntu
This creates and starts a container.
Interactive Docker Container
Run container in interactive mode:
docker run -it ubuntu bash
Explanation
| Option | Meaning |
|---|---|
-i | Interactive mode |
-t | Terminal access |
bash | Start bash shell |
Now you can run Linux commands inside the container.
Example:
ls
pwd
apt update
Exit container:
exit
Run Container in Background
Use detached mode.
docker run -d nginx
Explanation
| Option | Meaning |
|---|---|
-d | Detached mode |
List Running Containers
docker ps
Example Output:
CONTAINER ID IMAGE STATUS
8a21cd4d2f32 nginx Up 5 minutes
List All Containers
docker ps -a
Stop Docker Container
docker stop container_id
Example:
docker stop 8a21cd4d2f32
Start Docker Container
docker start container_id
Restart Docker Container
docker restart container_id
Remove Docker Container
docker rm container_id
Force remove:
docker rm -f container_id
Remove Docker Image
docker rmi image_id
Example:
docker rmi ubuntu
Docker Port Mapping
Port mapping connects container ports to system ports.
Run Nginx Web Server
docker run -d -p 8080:80 nginx
Explanation
| Part | Meaning |
|---|---|
8080 | Host system port |
80 | Container port |
Open browser:
http://localhost:8080
Docker Container Name
Assign custom container name.
docker run -d --name mynginx nginx
Execute Commands Inside Container
docker exec -it mynginx bash
If bash not available:
docker exec -it mynginx sh
Docker Logs
View container logs.
docker logs mynginx
Live logs:
docker logs -f mynginx
Docker Volume Example
Volumes store persistent data.
docker run -d -v myvolume:/data nginx
Docker Environment Variables
docker run -e MYSQL_ROOT_PASSWORD=password mysql
Docker Networking Basics
List Docker networks:
docker network ls
Create custom network:
docker network create mynetwork
Run container using network:
docker run -d --network mynetwork nginx
Dockerfile Example
Create custom Docker image.
Create Dockerfile
FROM ubuntu
RUN apt update
CMD ["echo", "Welcome to Tech3Space"]
Build Docker Image
docker build -t myimage .
Run Custom Image
docker run myimage
Docker Container Lifecycle
Create → Start → Run → Stop → Remove
Useful Docker Commands
| Command | Description |
|---|---|
docker pull | Download image |
docker run | Run container |
docker ps | List running containers |
docker stop | Stop container |
docker rm | Remove container |
docker images | List images |
docker logs | Show logs |
docker exec | Run command inside container |
Real-World Docker Use Cases
Docker is used for:
- Web application deployment
- Microservices
- DevOps pipelines
- CI/CD automation
- Database containers
- Cloud applications
- Testing environments
Advantages of Docker
- Lightweight
- Fast deployment
- Easy scaling
- Consistent environments
- Portable applications
- Better resource usage
Common Docker Errors
Permission Denied
Fix:
sudo usermod -aG docker $USER
Logout and login again.
Container Exits Immediately
Some containers stop because no process is running.
Run interactive mode:
docker run -it ubuntu bash
Best Practices
- Use official images
- Keep images small
- Remove unused containers
- Use Docker volumes
- Monitor container logs
- Use
.dockerignore
Docker vs Virtual Machine
| Docker | Virtual Machine |
|---|---|
| Lightweight | Heavy |
| Faster | Slower |
| Shares OS kernel | Separate OS |
| Small size | Large size |
Conclusion
Docker makes application deployment simple, fast, and portable. Learning Docker containers and images is essential for modern DevOps, backend development, cloud computing, and server management.
Practice running containers regularly to master Docker concepts.
Frequently Asked Questions
What is a Docker container?
A Docker container is a lightweight isolated environment that runs applications.
What is Docker Hub?
Docker Hub is an online repository for Docker images.
Is Docker free?
Yes, Docker has free community editions.
Why use Docker?
Docker helps developers run applications consistently across systems.
Does Docker work on Ubuntu?
Yes, Docker works perfectly on Ubuntu Linux systems.
Related Tutorials
- Docker Compose Tutorial
- Kubernetes Beginner Guide
- Linux Commands Tutorial
- Nginx Docker Setup
- MySQL Docker Container
- DevOps Roadmap