0
0
Dockerdevops~30 mins

Containers vs virtual machines in Docker - Hands-On Comparison

Choose your learning style9 modes available
Containers vs Virtual Machines: Basic Docker Setup
📖 Scenario: You are learning how containers work compared to virtual machines. Containers are lightweight and share the host system's kernel, while virtual machines run full operating systems. To see this in action, you will create a simple Docker container that runs a small web server.
🎯 Goal: Build a basic Docker container that runs an Nginx web server. You will create a Dockerfile, set a configuration variable, build the container, and then run it to see the web server in action.
📋 What You'll Learn
Create a Dockerfile with the base image nginx:alpine
Add a configuration variable to set the container's exposed port to 80
Build the Docker image with the tag mynginx
Run the Docker container mapping port 8080 to the container's port 80
Print the command to check running containers
💡 Why This Matters
🌍 Real World
Containers let developers package applications with all needed parts to run anywhere, making deployment fast and consistent. Virtual machines provide full OS isolation but use more resources.
💼 Career
Understanding containers and how to build and run them is essential for modern DevOps roles, enabling efficient application deployment and scaling.
Progress0 / 4 steps
1
Create the Dockerfile with base image
Create a file named Dockerfile with the exact line FROM nginx:alpine to use the lightweight Nginx image.
Docker
Need a hint?

This line tells Docker to use the official lightweight Nginx image as the base for your container.

2
Add port configuration variable
Add a line to the Dockerfile to expose port 80 inside the container. Use the exact line EXPOSE 80.
Docker
Need a hint?

This tells Docker that the container listens on port 80 for web traffic.

3
Build the Docker image
Run the exact command docker build -t mynginx . in your terminal to build the Docker image with the tag mynginx.
Docker
Need a hint?

This command builds your Docker image using the Dockerfile in the current folder and tags it as 'mynginx'.

4
Run the Docker container and check running containers
Run the exact command docker run -d -p 8080:80 mynginx to start the container mapping port 8080 on your machine to port 80 in the container. Then run docker ps to list running containers.
Docker
Need a hint?

This runs your container in the background and maps port 8080 on your computer to the container's port 80. The docker ps command shows running containers.