2. Installation & Your First Container | The Complete Docker Handbook.
Welcome to Article 2 of The Complete Docker Handbook.
In Article 1, we covered the theory: what Docker is, why we use it, and how it differs from Virtual Machines. Now, it's time to stop talking and start doing.
In this guide, you will install Docker on your operating system, verify the installation, and run your first few containers. By the end of this article, you'll be comfortable with the basic lifecycle commands that you will use every day.
Step 1: Installing Docker
The installation process differs slightly depending on your operating system. Choose your path below.
π macOS & πͺ Windows
For Mac and Windows, the easiest way to get started is Docker Desktop. It includes the Docker Engine, a CLI client, and a nice visual dashboard.
- Download: Visit docker.com/products/docker-desktop and download the version for your system.
- Install: Run the installer and follow the wizard.
- Windows Users: Ensure you have WSL 2 (Windows Subsystem for Linux) enabled. Docker Desktop will usually prompt you to enable this if it's missing. This is crucial for performance.
- Mac Users: If you are on Apple Silicon (M1/M2/M3), Docker Desktop runs natively.
- Start: Open Docker Desktop. Wait for the engine to start (the bottom left corner should turn green).
π§ Linux (Ubuntu/Debian)
On Linux, you typically install the Docker Engine directly. While you can use Docker Desktop for Linux, the Engine is the standard for servers.
The Quick Install Script (Recommended for Dev): Docker provides a convenience script for development environments. Open your terminal and run:
Bash1curl -fsSL https://get.docker.com -o get-docker.sh 2sudo sh get-docker.sh
Post-Installation (Linux Only):
By default, Docker commands require sudo. To run Docker as a non-root user (recommended):
- Create the docker group:
Plain Text
1bash 2sudo groupadd docker - Add your user to the group:
Plain Text
1bash 2sudo usermod -aG docker $USER - Log out and log back in (or run
newgrp docker) to apply changes.
Step 2: Verifying the Installation
Once installed, open your terminal (Command Prompt, PowerShell, Terminal, or Bash) and verify everything is working.# Installation & Your First Container
Welcome to Article 2 of The Complete Docker Handbook.
In Article 1, we covered the theory: what Docker is, why we use it, and how it differs from Virtual Machines. Now, it's time to stop talking and start doing.
In this guide, you will install Docker on your operating system, verify the installation, and run your first few containers. By the end of this article, you'll be comfortable with the basic lifecycle commands that you will use every day.
Step 1: Installing Docker
The installation process differs slightly depending on your operating system. Choose your path below.
π macOS & πͺ Windows
For Mac and Windows, the easiest way to get started is Docker Desktop. It includes the Docker Engine, a CLI client, and a nice visual dashboard.
- Download: Visit docker.com/products/docker-desktop and download the version for your system.
- Install: Run the installer and follow the wizard.
- Windows Users: Ensure you have WSL 2 (Windows Subsystem for Linux) enabled. Docker Desktop will usually prompt you to enable this if it's missing. This is crucial for performance.
- Mac Users: If you are on Apple Silicon (M1/M2/M3), Docker Desktop runs natively.
- Start: Open Docker Desktop. Wait for the engine to start (the bottom left corner should turn green).
π§ Linux (Ubuntu/Debian)
On Linux, you typically install the Docker Engine directly. While you can use Docker Desktop for Linux, the Engine is the standard for servers.
The Quick Install Script (Recommended for Dev): Docker provides a convenience script for development environments. Open your terminal and run:
Bash1curl -fsSL https://get.docker.com -o get-docker.sh 2sudo sh get-docker.sh
Post-Installation (Linux Only):
By default, Docker commands require sudo. To run Docker as a non-root user (recommended):
- Create the docker group:
Plain Text
1bash 2sudo groupadd docker - Add your user to the group:
Plain Text
1bash 2sudo usermod -aG docker $USER - Log out and log back in (or run
newgrp docker) to apply changes.
Step 2: Verifying the Installation
Once installed, open your terminal (Command Prompt, PowerShell, Terminal, or Bash) and verify everything is working.
1. Check the version:
Bashdocker --version
Output: Docker version 24.0.7, build afdd53b (Your version number may vary).
2. Check the system info:
Bashdocker info
This command displays detailed information about the Docker Engine, including the number of containers and images currently on your system.
Step 3: Your First Container (hello-world)
Let's run the traditional "Hello World" of the Docker ecosystem.
Run the command:
Bashdocker run hello-world
What just happened?
You might see a message saying Unable to find image 'hello-world:latest' locally. Here is the step-by-step process Docker just performed:
- Client: The Docker Client contacted the Docker Daemon.
- Search: The Daemon looked for the
hello-worldimage locally. - Pull: Since it wasn't found, the Daemon pulled it from Docker Hub (the public registry).
- Create: The Daemon created a new container from that image.
- Run: The Daemon streamed the output back to your terminal.
Output:
Plain Text1Hello from Docker! 2This message shows that your installation appears to be working correctly. 3...
Congratulations! You have successfully pulled an image and run a container.
Step 4: Running an Interactive Container
The hello-world container is staticβit prints a message and exits immediately. To really see Docker in action, let's run a Linux distribution inside a container and interact with it.
We will use the Ubuntu image.
Run the command:
Bashdocker run -it ubuntu bash
Breaking down the flags:
docker run: The command to create and start a container.-i(Interactive): Keeps STDIN open even if not attached.-t(TTY): Allocates a pseudo-terminal (gives you a command prompt).ubuntu: The image name (defaults to:latesttag).bash: The command to run inside the container (start the bash shell).
You are now inside the container! Your terminal prompt will change. Try running some Linux commands:
Bash1root@f1234567890:/# cat /etc/os-release
2root@f1234567890:/# ls
3root@f1234567890:/# exit
Type exit or press Ctrl+D to leave the container and return to your host machine's terminal.
> β οΈ Important Note:
> Notice that when you ran ls, you only saw files inside the container, not files on your computer. The container is isolated from your host file system.
Step 5: Managing Containers
Now that you've run containers, you need to know how to manage them.
1. List Running Containers
Bashdocker ps
If you ran the Ubuntu container and typed exit, it is no longer running, so docker ps will show nothing.
2. List All Containers (Including Stopped)
Bashdocker ps -a
You should see your hello-world and ubuntu containers listed here. Note their STATUS (e.g., Exited (0) 2 minutes ago).
3. Stop a Running Container
If you have a container running in the background, you can stop it using its Container ID or Name.
Bashdocker stop
Example: docker stop f1234567890
4. Remove a Container
Stopped containers still take up disk space. You can remove them:
Bashdocker rm
Tip: You can combine stop and remove: docker rm -f (force remove).
Step 6: Managing Images
Containers are created from Images. Let's look at what images you have downloaded.
1. List Images:
Bashdocker images
You should see hello-world and ubuntu in the list.
2. Remove an Image: You cannot remove an image if a container created from it still exists. You must remove the container first.
Bashdocker rmi
Step 7: Cleanup (Pruning)
As you learn, you will accumulate many stopped containers and unused images. Docker has a built-in cleanup command.
Remove all stopped containers, unused networks, and dangling images:
Bashdocker system prune
Add -a to remove all unused images (not just dangling ones), but be careful with this flag as it deletes any image not currently used by a running container.
Troubleshooting Common Issues
1. "Permission denied" (Linux)
- Error:
Got permission denied while trying to connect to the Docker daemon socket... - Fix: You need to add your user to the docker group (see Step 1 Linux section) or run the command with
sudo.
2. "Error during connect" (Windows/Mac)
- Error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. - Fix: Ensure Docker Desktop is actually running. Look for the whale icon in your system tray. If it's spinning, wait for it to turn green.
3. Virtualization Not Enabled
- Error: Docker Desktop fails to start.
- Fix: You need to enable Virtualization (VT-x/AMD-V) in your computer's BIOS/UEFI settings.
Summary Checklist
By completing this article, you have:
- Installed Docker Desktop or Engine.
- Verified installation with
docker --version. - Ran the
hello-worldcontainer. - Ran an interactive Ubuntu container.
- Learned
docker ps,stop,rm, andimages. - Cleaned up your system with
docker system prune.
What's Next?
You now know how to run existing images. But the real power of Docker comes from creating your own images tailored to your applications.
In Article 3, we will dive deep into Docker Images:
- Understanding Image Layers.
- How caching works.
- Writing your first Dockerfile.
Link: Read Article 3: Deep Dive into Docker Images
Stuck? If you encountered an error during installation, drop a comment below with your OS and the error message. Let's solve it together!