If you are learning DevOps, cloud computing, Linux or modern application deployment, Docker is one of the technologies you will encounter frequently.
Docker provides a way to package an application together with the components it needs so that it can run in a consistent environment. This approach is commonly referred to as containerization.
For beginners, however, Docker can initially seem confusing because several terms appear together: images, containers, registries, ports and volumes. The easiest way to understand Docker is to see how these pieces fit together.
A Docker image is a packaged template, while a Docker container is a running instance created from an image.
Docker in One Picture
Source Code
Build Instructions
Packaged Template
Running Application
What Is Docker?
Docker is a platform and ecosystem for building, distributing and running containerized applications.
Instead of installing an application's dependencies directly onto every server, developers can package the application environment into a Docker image and run containers from that image.
This can make application environments easier to reproduce across development, testing and production systems.
What Is a Docker Image?
A Docker image is a packaged, read-only template used to create containers.
An image can contain application code, libraries, configuration files and the filesystem components required by the application.
You can think of an image as a blueprint. The container is created when that blueprint is instantiated.
├── Application
├── Dependencies
├── Libraries
└── Filesystem
What Is a Docker Container?
A Docker container is a running instance of a Docker image.
Multiple containers can be created from the same image. Each container provides an isolated environment for its processes while using the host operating system's underlying kernel.
For example, imagine you have an image called:
my-web-app:1.0
You could create multiple containers from that image.
Image vs Container
| Docker Image | Docker Container |
|---|---|
| Packaged template | Running instance |
| Used to create containers | Runs application processes |
| Generally immutable | Has a writable container layer |
| Can be stored in a registry | Exists on a container host |
Running Your First Container
Once Docker is installed, one of the simplest examples is running an Nginx container:
docker run nginx
Docker will use the specified image and create a container from it.
To run it in the background:
docker run -d nginx
The -d option starts the container in detached mode.
How Do Docker Ports Work?
A common beginner question is: if an application is running inside a container, how can users access it from outside the container?
Docker can publish a container port to a port on the host.
localhost:8080
Port 8080
Port 80
For example:
docker run -d -p 8080:80 nginx
The general format is:
-p HOST_PORT:CONTAINER_PORT
In this example, traffic arriving at host port 8080 is published to port 80 in the container.
What Are Docker Volumes?
Containers are often treated as disposable application environments. This creates an important question: where should data that needs to survive container replacement be stored?
Docker volumes provide a mechanism for storing persistent data separately from the container's writable layer.
The container runs the application. The volume can keep important data outside the container lifecycle.
A volume can be created with:
docker volume create app-data
You can then attach it to a container when starting that container.
docker run -d -v app-data:/data nginx
The exact directory used for application data depends on the application running inside the container.
Useful Docker Commands
These commands are useful when learning Docker for beginners.
List running containers
docker ps
List all containers
docker ps -a
List local images
docker images
Stop a container
docker stop CONTAINER_ID
Start a stopped container
docker start CONTAINER_ID
View container logs
docker logs CONTAINER_ID
Inspect a container
docker inspect CONTAINER_ID
What Is a Dockerfile?
A Dockerfile is a text file containing instructions used to build a Docker image.
A very simple example could look like this:
FROM nginx:alpine COPY ./website /usr/share/nginx/html EXPOSE 80
Here:
- FROM specifies the starting image.
- COPY copies files into the image.
- EXPOSE documents the port the application is expected to use.
The image can then be built using:
docker build -t my-website:1.0 .
The dot at the end tells Docker to use the current directory as the build context.
What Is a Container Registry?
A container registry stores container images so that they can be distributed and retrieved by other systems.
A common workflow looks like:
↓
Build Image
↓
Push Image to Registry
↓
Server / Kubernetes
↓
Pull Image
↓
Run Container
Registries are an important part of modern CI/CD and container-based deployment workflows.
Why Docker Is Important for DevOps
Docker is closely associated with modern DevOps practices because containers can provide a consistent application packaging format across development and deployment environments.
A typical workflow might look like this:
- A developer changes application code.
- A CI system builds a Docker image.
- The image is tested.
- The image is pushed to a container registry.
- A deployment system retrieves the image.
- The application is started using that image.
Docker knowledge is therefore useful before moving into more advanced container orchestration technologies such as Kubernetes.
Docker Containers vs Virtual Machines
Containers and virtual machines solve related but different problems.
| Containers | Virtual Machines |
|---|---|
| Share the host kernel | Normally include a guest operating system |
| Usually lightweight | Generally require more resources |
| Fast application startup is common | Full OS boot is typically involved |
| Application-focused packaging | Complete machine environment |
Containers are not simply smaller virtual machines. They use operating-system isolation mechanisms differently from traditional virtual machines.
Practical Docker Exercise
If Docker is installed on your Linux system, try the following exercise.
Step 1: Run Nginx
docker run -d --name my-nginx -p 8080:80 nginx
Step 2: Check the container
docker ps
Step 3: Test the published port
Open the following address in a browser on the Docker host:
http://localhost:8080
Step 4: View the logs
docker logs my-nginx
Step 5: Stop the container
docker stop my-nginx
Step 6: Remove it
docker rm my-nginx
Docker Interview Questions for Beginners
1. What is Docker?
Docker is a platform and ecosystem for building, distributing and running containerized applications.
2. What is a Docker image?
A Docker image is a packaged template used to create containers.
3. What is a Docker container?
A container is a running instance created from a Docker image.
4. What is a Dockerfile?
A Dockerfile contains instructions used to build a Docker image.
5. What is a Docker volume?
A Docker volume provides persistent storage that can exist independently of an individual container's writable layer.
6. What does port mapping do?
Port mapping publishes a container port through a port on the Docker host, allowing traffic to reach the application inside the container.
7. Why are Docker images used in CI/CD?
Images provide a standardized artifact that can be built, tested, stored and deployed through an automated pipeline.
Frequently Asked Questions
Is Docker the same as a virtual machine?
No. Docker containers and virtual machines use different isolation approaches. Containers normally share the host kernel, while virtual machines provide a virtualized hardware environment for a guest operating system.
Can multiple containers use the same image?
Yes. Multiple containers can be created from the same image.
Why do containers need volumes?
Volumes can be used when application data needs to persist independently of the lifecycle of a particular container.
Do Docker containers always expose ports?
No. A container can run without publishing a port to the host. Whether a port needs to be published depends on how the application is accessed.
Key Takeaways
- Docker is widely used for containerized application workflows.
- A Docker image is a packaged template.
- A Docker container is a running instance of an image.
- A Dockerfile contains instructions for building an image.
- Port publishing allows host traffic to reach an application inside a container.
- Volumes can provide persistent data storage.
- Container registries store and distribute images.
- Docker is an important foundation for many DevOps and cloud workflows.
- Docker containers are different from traditional virtual machines.
Related Topics
- Kubernetes Explained: Pods, Deployments and Services
- Kubernetes Services Explained
- CI/CD Explained
- Linux Commands for Beginners
- Kubernetes Networking
- Container Networking
- Cloud Computing for Beginners
No comments:
Post a Comment