Lesson 1 of 9 ↓
- 1.Docker Comprehensive Guide: From Zero to Hero
- 2.Docker Environment Variables: the Ultimate Guide
- 3.Docker Images: the Ultimate Comprehensive Guide
- 4.Docker: Cmd vs. Entrypoint - the Comprehensive Guide
- 5.Docker Networking: the Ultimate Deep Dive
- 6.Docker Storage & Filesystem: the Complete Reference
- 7.Docker Compose: the Ultimate Master Guide
- 8.Docker Registry: the Ultimate Master Guide
- 9.Docker Engine: the Ultimate Deep Dive
8 min read
Docker Comprehensive Guide: From Zero to Hero
This guide covers everything you need to know to get started with Docker. It explains commands in detail, breaks down arguments (flags), and provides real-world examples including web servers (Nginx) and databases (MySQL).
Installation & Setup
Before using Docker, you need to install and enable it on your system.
Install Docker
Command:
sudo yum install docker-
Explanation: This command uses
yum, the package manager for RHEL/CentOS/Amazon Linux distributions, to download and install the Docker engine. -
Note: If you are on Ubuntu/Debian, you would typically use
apt-get install docker.io.
Start Docker Service
Command:
sudo systemctl start docker- Explanation: Installing Docker doesn't start it automatically. This command starts the Docker daemon (background process) so it can receive commands.
Enable Docker (auto-start)
Command:
sudo systemctl enable docker- Explanation: This ensures that Docker starts automatically whenever you reboot your server or computer.
Check Docker Version
Command:
docker --version- Explanation: Verifies that Docker is installed correctly and shows you which version you are running.
The Golden Command: docker Run
The docker run command is the most frequently used command. It effectively performs three steps in one:
-
Pull: Downloads the image from Docker Hub (if not found locally).
-
Create: Creates the container from that image.
-
Start: Starts the container.
Basic Run
Command:
docker run hello-world- What it does: Downloads a tiny test image, runs it to print a "Hello" message, and then exits. Used to verify your installation works.
Running a Web Server (nginx)
Command:
docker run nginx- Behavior: This runs Nginx in the foreground. Your terminal will be stuck showing Nginx logs, and you cannot type new commands until you stop it (Ctrl+C).
Running in Background (detached Mode)
Command:
docker run -d nginx-
Arguments:
-d(Detached): Crucial. It tells Docker to run the container in the background. The terminal remains free for you to use, and the container keeps running silently.
Running with a Specific Version (tags)
Command:
docker run -d nginx:1.19- Explanation: By default, Docker looks for the
latesttag. You can specify a version (e.g.,1.19,alpine) by adding a colon:.
Running with Port Mapping
Command:
docker run -d -p 8080:80 nginx-
Arguments:
-
-p(Publish/Port): Very Important. The format isHOST_PORT:CONTAINER_PORT. -
Example:
-p 8080:80means "Forward traffic from port 8080 on my laptop (Host) to port 80 inside the container." -
Result: You can access the website at
http://localhost:8080.
-
Overriding the Default Command
Command:
docker run -d ubuntu sleep 5-
Explanation: Docker images have a default startup command. You can override it by typing a command after the image name.
-
Example: This forces the Ubuntu container to simply
sleepfor 5 seconds and then exit (stop).
Managing Containers (lifecycle)
List Running Containers
Command:
docker ps-
Explanation: Shows only the containers that are currently UP and running.
-
Columns: Shows Container ID, Image, Command, Created Time, Status, Ports, and Names.
List All Containers (running & Stopped)
Command:
docker ps -a-
Arguments:
-a(All): Shows active containers AND containers that have stopped, exited, or crashed.
List Only Container Ids
Command:
docker ps -aq-
Arguments:
-q(Quiet): Only displays the numeric IDs. Useful for scripts (see "Bulk Deletion" below).
Stop a Container
Command:
docker stop <container_id>
# OR
docker stop <container_name>- Explanation: Gracefully stops a running container (sends a SIGTERM signal). It gives the application a moment to save data before shutting down.
Start a Stopped Container
Command:
docker start <container_id>- Explanation: Starts a container that already exists but was stopped. (Note:
docker runcreates a new one;docker startresumes an existing one).
Restart a Container
Command:
docker restart <container_id>- Explanation: Stops and immediately starts the container again. Useful if the app crashed or configs changed.
Removing & Cleanup
Remove a Stopped Container
Command:
docker rm <container_id>- Explanation: Deletes the container (the instance), but not the image. The container must be stopped first.
Force Remove a Running Container
Command:
docker rm -f <container_id>-
Arguments:
-f(Force): Deletes the container immediately, even if it is currently running (sends SIGKILL). Use with caution.
Bulk Delete (delete All Containers)
Command:
docker rm -f $(docker ps -aq)-
How it works:
-
$(docker ps -aq)runs first and lists ALL container IDs. -
docker rm -fthen receives that list and deletes them all at once. -
Warning: This deletes everything. Be careful!
-
Working with Images
Pull an Image (download Only)
Command:
docker pull nginx- Explanation: Downloads the image from Docker Hub to your local machine but does not run it.
List Local Images
Command:
docker images- Explanation: Shows all images currently downloaded on your machine (Repository, Tag, Image ID, Size).
Create a Container (without Starting)
Command:
docker create nginx-
Explanation: Creates the container layer ready to use but status remains "Created", not "Up". You must use
docker startafterwards. -
Logic:
docker run=docker pull+docker create+docker start.
Delete an Image
Command:
docker rmi <image_name>
# OR
docker image rm <image_name>- Explanation: Deletes the actual image file from your disk to save space. You cannot delete an image if a container is using it (you must delete the container first).
Advanced Interactions & Inspection
Inspecting Details
Command:
docker inspect <container_id>- Explanation: Returns a huge JSON file with every low-level detail about the container: IP address, environment variables, mounted volumes, paths, etc.
Viewing Logs
Command:
docker logs <container_id>-
Explanation: Shows the output (stdout/stderr) of the container. Essential for debugging why an app failed.
-
Useful Flag:
docker logs -f <id>(Follow mode - streams logs live liketail -f).
Executing Commands Inside a Container
Command:
docker exec -it <container_id> bash-
Arguments:
-
-i(Interactive): Keeps STDIN open even if not attached. -
-t(TTY): Allocates a pseudo-terminal (makes it look like a real shell). -
bash: The command to run (opens the Bash shell).
-
-
Use Case: "Logging into" the container to fix files or check configurations manually.
Persistent Storage (volumes)
Command:
docker run -d -p 8090:80 -v /data:/tmp nginx-
Arguments:
-
-v(Volume): Crucial for Databases. Format isHOST_PATH:CONTAINER_PATH. -
Explanation: Maps the folder
/dataon your computer to/tmpinside the container. -
Why? If the container is deleted, files in
/tmpare gone. But with this mapping, the files are safely stored in/dataon your host machine.
-
Real World Scenario: Mysql (from Uploaded Images)
This section explains the complex commands found in the screenshots you provided.
Step 1: Running the Mysql Container
Command:
docker run -d \
--name mysql-server \
-e MYSQL_ROOT_PASSWORD=your_password \
-e MYSQL_DATABASE=mydb \
-e MYSQL_USER=myuser \
-e MYSQL_PASSWORD=mypassword \
-p 3306:3306 \
-v mysql_data:/var/lib/mysql \
mysql:latestDetailed Breakdown of Arguments:
-
docker run -d: Run container in detached mode (background). -
--name mysql-server: Naming. Instead of a random name (likesilly_sammet), we name itmysql-server. This makes it easier to reference later. -
-e(Environment Variable): Configuration. This is how we pass settings to the container.-
MYSQL_ROOT_PASSWORD: Sets the root password (Mandatory for MySQL image). -
MYSQL_DATABASE: Creates a default database namedmydb. -
MYSQL_USER: Creates a generic usermyuser. -
MYSQL_PASSWORD: Sets the password formyuser.
-
-
-p 3306:3306: Port Mapping. Allows external tools (like Workbench) on your laptop to connect to the DB. -
-v mysql_data:/var/lib/mysql: Volume. Maps a named volumemysql_datato the container's data folder. Even if you delete this container, your database data remains safe inmysql_data. -
mysql:latest: The image name and tag.
Step 2: Connecting to Mysql (interactive Mode)
Command:
docker exec -it mysql-server mysql -u root -pDetailed Breakdown:
-
docker exec: Execute a command inside an existing running container. -
-it: Interactive terminal mode (allows you to type the password). -
mysql-server: The name of the container we created in Step 1. -
mysql -u root -p: The actual Linux command running inside the container. It launches the MySQL CLI client logging in as root. -
Result: You will see the
mysql>prompt where you can run SQL queries directly.
Summary Cheatsheet
| Command | Description | Key Flag/Arg |
|---|---|---|
docker run <image> |
Create and start a container | -d (background), -p (port) |
docker ps |
List running containers | -a (show all), -q (IDs only) |
docker stop <id> |
Stop a container | |
docker rm <id> |
Delete a container | -f (force delete) |
docker images |
List downloaded images | |
docker rmi <image> |
Delete an image | |
docker pull <image> |
Download image without running | |
docker exec -it <id> bash |
Enter container shell | -it (interactive tty) |
docker logs <id> |
View container output | -f (follow live) |
docker inspect <id> |
View low-level config (JSON) |
Comments
Loading the discussion…