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
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
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
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
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.
Practice
(1/5)
1. Which statement best describes the main difference between containers and virtual machines?
easy
A. Containers require more disk space than virtual machines.
B. Virtual machines are faster to start than containers.
C. Virtual machines share the host OS kernel, containers do not.
D. Containers share the host OS, while virtual machines run separate full OS instances.
Solution
Step 1: Understand container architecture
Containers share the host operating system kernel, making them lightweight and fast.
Step 2: Understand virtual machine architecture
Virtual machines run a full guest OS on top of a hypervisor, making them heavier and fully isolated.
Final Answer:
Containers share the host OS, while virtual machines run separate full OS instances. -> Option D
Quick Check:
Containers share OS, VMs run full OS [OK]
Hint: Containers share OS; VMs run full OS instances [OK]
Common Mistakes:
Thinking containers run full OS instances
Believing VMs share the host OS kernel
Assuming containers use more disk space
2. Which of the following is the correct command to list running Docker containers?
easy
A. docker ps
B. docker images
C. docker run
D. docker start
Solution
Step 1: Identify command to list running containers
The command docker ps shows all running containers.
Step 2: Differentiate from other commands
docker images lists images, docker run starts a container, and docker start restarts stopped containers.
Final Answer:
docker ps -> Option A
Quick Check:
List running containers = docker ps [OK]
Hint: Use 'docker ps' to see running containers [OK]
Common Mistakes:
Using 'docker images' to list containers
Confusing 'docker run' with listing
Using 'docker start' instead of listing
3. Given the following scenario: You start a Docker container and run uname -a inside it. What will this command output compared to running uname -a on a virtual machine?
medium
A. Both outputs will be identical because containers and VMs have the same kernel.
B. Both outputs will be completely unrelated to the host system.
C. The container's output shows the host kernel, VM shows its own kernel version.
D. The container shows a different kernel version than the host, VM shows host kernel.
Solution
Step 1: Understand container kernel sharing
Containers share the host OS kernel, so uname -a inside a container shows the host kernel version.
Step 2: Understand VM kernel isolation
Virtual machines run their own OS kernel, so uname -a inside a VM shows the VM's kernel version, which can differ from the host.
Final Answer:
The container's output shows the host kernel, VM shows its own kernel version. -> Option C
Quick Check:
Container shares kernel, VM has own kernel [OK]
Hint: Containers share kernel; VMs have separate kernels [OK]
Common Mistakes:
Assuming containers have separate kernels
Thinking VM shares host kernel
Believing outputs are identical always
4. You tried to run a Docker container but got an error saying the container cannot start due to kernel incompatibility. What is the most likely cause?
medium
A. The Docker daemon is not installed.
B. The container image requires a different kernel feature not supported by the host OS.
C. The container image is corrupted and cannot be loaded.
D. The container is trying to run a full OS which is not allowed.
Solution
Step 1: Analyze kernel incompatibility error
Containers rely on the host kernel. If the container needs kernel features missing on the host, it cannot start.
Step 2: Rule out other causes
Docker daemon missing causes different errors; corrupted images cause load errors; containers can run full OS but share kernel.
Final Answer:
The container image requires a different kernel feature not supported by the host OS. -> Option B
Quick Check:
Kernel features mismatch causes container start failure [OK]
Hint: Kernel mismatch blocks container start, check host features [OK]
5. You want to deploy an application that requires full OS isolation for security but also needs to start quickly and use minimal resources. Which approach is best?
hard
A. Use containers with additional security layers like SELinux or AppArmor.
B. Use containers for speed and efficiency but accept less isolation.
C. Use virtual machines for full isolation despite slower startup and higher resource use.
D. Use virtual machines with lightweight OS images to speed up startup.
Solution
Step 1: Understand isolation vs speed tradeoff
Virtual machines provide full OS isolation but are slower and heavier; containers are fast but share kernel.
Step 2: Consider enhanced container security
Adding security layers like SELinux/AppArmor to containers improves isolation while keeping speed and resource benefits.
Step 3: Evaluate other options
Using VMs sacrifices speed; lightweight VMs help but still slower than containers; plain containers lack full isolation.
Final Answer:
Use containers with additional security layers like SELinux or AppArmor. -> Option A
Quick Check:
Secure containers balance isolation and speed [OK]
Hint: Secure containers with extra layers for isolation and speed [OK]
Common Mistakes:
Choosing VMs ignoring startup speed
Using plain containers without security layers
Assuming lightweight VMs match container speed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.