0
0
Dockerdevops~5 mins

Why containers matter in Docker - Why It Works

Choose your learning style9 modes available
Introduction
Containers help you run your apps in a small, isolated box that works the same everywhere. This solves the problem of apps breaking when moved between computers or servers.
When you want to run a web app on your laptop and then move it to a cloud server without changes.
When you need to run multiple apps on the same machine without them interfering with each other.
When you want to share your app with a teammate and be sure it runs exactly the same on their computer.
When you want to quickly start and stop apps without installing lots of software on your system.
When you want to keep your app and its tools separate from your main computer to avoid conflicts.
Commands
This command runs a small test container to check if Docker is working correctly on your machine.
Terminal
docker run --rm hello-world
Expected OutputExpected
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world Digest: sha256:... Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly.
--rm - Automatically removes the container after it exits to keep your system clean
This command lists all running containers so you can see what is currently active.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
This command runs an Nginx web server container in the background and maps port 8080 on your computer to port 80 in the container.
Terminal
docker run -d -p 8080:80 nginx
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
-d - Runs the container in detached mode (in the background)
-p 8080:80 - Maps port 8080 on your computer to port 80 inside the container
Check again to see the running Nginx container and confirm it is active.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx "nginx -g 'daemon off;'" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp loving_morse
This command fetches the default web page served by the Nginx container to show it is working.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: containers let you package apps so they run the same everywhere without conflicts.

Common Mistakes
Not mapping ports when running a web app container
Without port mapping, you cannot access the app from your computer's browser.
Always use the -p flag to map container ports to your computer's ports, like -p 8080:80.
Running containers without --rm during testing
Containers stay on your system after stopping, cluttering your environment.
Use --rm to automatically remove test containers when they exit.
Expecting apps to run the same without using containers
Apps may break due to different software versions or missing dependencies on different machines.
Use containers to package apps with all needed software to ensure consistency.
Summary
Run a test container with 'docker run --rm hello-world' to check Docker setup.
Use 'docker run -d -p 8080:80 nginx' to start a web server container accessible on your computer.
Check running containers with 'docker ps' to monitor active apps.
Access container apps via mapped ports to interact with them from your machine.