Introduction to Docker and Installation
🎯 Introduction
By the end of this article, you will understand what Docker is and the problems containers solve. You will also install Docker Desktop and run an Nginx container that serves a page you can open in your browser. The MySQL and MongoDB courses use Docker to install databases, so this is your starting point.
🧱 What Is Docker?
Think about installing software such as MySQL: you download an installer, configure environment variables, and start a service. On another computer, you may have to repeat everything, with no guarantee that it will work. Docker is a tool for solving these environment setup and configuration problems.
A central concept in Docker is the container. Think of a shipping container:
- A shipping container packages cargo into a standard unit. Cranes and ships can handle it in the same way regardless of its contents.
- A Docker container packages software and its runtime environment into a standard unit. You can run it in the same way on macOS or Windows.
For example, instead of installing MySQL directly on your computer, you ask Docker to run a MySQL container. One command gives you a running MySQL instance, and removing the container removes its files from your computer.
✨ Why Use Docker?
For frontend developers, Docker offers three practical benefits:
- Start software with one command: Server software such as MySQL, MongoDB, and Nginx can run with a single
docker runcommand, without working through installers and configuration steps. - Consistent environments: The same container behaves consistently on your computer, a colleague's computer, and a server, reducing “it works on my machine” problems.
- Clean isolation: Software runs inside a container, keeping it separate from your system. When you no longer need it, remove the container.
🛠 Install Docker Desktop
For learning, use Docker Desktop, Docker's official desktop application for macOS and Windows. This article was written and tested with Docker Desktop 4.x.
- Visit the Docker website (docker.com) and download the Docker Desktop installer for your operating system.
- Open the installer and follow the instructions. On macOS, drag the Docker icon into Applications.
- Start Docker Desktop and wait for the whale icon in the status bar to settle, indicating that Docker is ready.
After installation, open a terminal to check:
docker --version
Output such as Docker version 27.x.x confirms that the installation succeeded.
🛠 Run Your First Container
With Docker ready, try running an Nginx container. Nginx is a widely used web server. Installing it manually can involve downloading, compiling, and configuring it; with Docker, you need one command:
docker run -d --name web -p 8080:80 nginx
Here is what each part means:
-d: Run the container in the background.--name web: Name the containerwebso it is easy to reference later.-p 8080:80: Map port 80 in the container to port 8080 on your computer. Nginx listens on port 80 by default inside the container.nginx: Use the Nginx image, a packaged template for the software.
The first run downloads the image automatically. Wait for the download to finish, then open http://localhost:8080 in your browser. The “Welcome to nginx!” page confirms that your first container is working.
When you are done, clean it up:
docker stop web
docker rm web
Stop it first, then remove it. Removing the container's files demonstrates Docker's clean isolation.
❓ Questions and Answers
Q1: Is Docker a virtual machine?
No. A virtual machine runs an entire operating system, which takes longer to start and uses more resources. Containers share an operating system kernel, so they start quickly and use fewer resources. Think of them as a lighter form of isolation.
Q2: What is an image?
An image is a packaged template for software, and a container is an instance created from that image. Think of an image as a class and a container as an object instantiated from it. The next article explores this relationship.
Q3: Is installation different on Intel and Apple silicon Macs?
Choose the installer for your chip when downloading it; the website can usually detect this automatically. Once installed, Docker works the same way on both.
Q4: After removing a container, does another docker run download the image again?
No. Downloaded images are cached locally. Subsequent runs use the cached image and can start within seconds, unless you also remove the image with docker rmi.
Q5: What if I cannot access localhost:8080?
Run docker ps to check whether the web container is running. If the list is empty, the container did not start successfully. Run docker run again and check the terminal for errors.
🧪 Practice
- Run Nginx again, mapping it to port 3000 on your computer. Visit
http://localhost:3000to verify it, then clean up the container:
# Hint: change the -p argument and use a different container name
# Write your commands here
- Run
docker run hello-worldand read the terminal output to understand the complete process: pulling an image, creating a container, and executing a program.
🎉 You now understand Docker's basic concepts and have run your first container! Next, we will explore images and containers in more detail and learn the commands used to manage them.
RESTful API Practice
Combine routes, controllers, services, validation, and a consistent response format to build a complete RESTful user management API.
Images and Containers
Understand how images and containers relate, and learn common Docker commands for listing, starting, stopping, and removing them.
