Docker Command Line Interface (CLI) Basics
Welcome to this comprehensive, student-friendly guide on Docker Command Line Interface (CLI) Basics! 🚀 Whether you’re just starting out or looking to solidify your understanding, this tutorial will walk you through the essentials of Docker CLI, helping you build a strong foundation. Don’t worry if this seems complex at first—by the end, you’ll be navigating Docker like a pro! Let’s dive in! 🏊♂️
What You’ll Learn 📚
- Introduction to Docker and its CLI
- Core Docker CLI commands
- Practical examples and exercises
- Common questions and troubleshooting tips
Introduction to Docker
Docker is a platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Think of a container as a small, self-contained environment that includes everything your application needs to run. This makes it super easy to move your applications between different environments without worrying about compatibility issues. 🐳
Why Use Docker?
- Consistency: Containers ensure that your application runs the same way, regardless of where it’s deployed.
- Efficiency: Containers are lightweight and use fewer resources than traditional virtual machines.
- Portability: Easily move containers between different environments.
Key Terminology
- Image: A read-only template used to create containers. Think of it as a blueprint for your application.
- Container: A runnable instance of an image. It’s where your application lives and runs.
- Dockerfile: A text file that contains instructions on how to build a Docker image.
- Registry: A storage and content delivery system, like Docker Hub, where Docker images are stored and shared.
Getting Started with Docker CLI
Setup Instructions
Before we begin, make sure you have Docker installed on your machine. You can download it from the Docker website. Once installed, open your terminal and run the following command to verify the installation:
docker --version
Your First Docker Command
Let’s start with the simplest Docker command: docker run hello-world
. This command runs a simple container that prints a ‘Hello, World!’ message.
docker run hello-world
Explanation: The docker run
command creates a new container from the hello-world
image and runs it. If the image isn’t available locally, Docker downloads it from Docker Hub. This command is a great way to test if Docker is installed correctly. 🎉
Progressively Complex Examples
Example 1: Running a Simple Web Server
Let’s run a basic web server using the official nginx
image.
docker run --name my-nginx -d -p 8080:80 nginx
Explanation:
--name my-nginx
: Assigns a name to the container for easier management.-d
: Runs the container in detached mode (in the background).-p 8080:80
: Maps port 8080 on your host to port 80 in the container, allowing you to access the web server viahttp://localhost:8080
.nginx
: The name of the image to use.
Example 2: Building a Custom Image
Create a simple Dockerfile
to build a custom image.
# Dockerfile
FROM python:3.8-slim
COPY app.py /app.py
CMD ['python', '/app.py']
Build and run the image:
docker build -t my-python-app .
docker run my-python-app
Explanation:
FROM python:3.8-slim
: Uses a lightweight Python image as the base.COPY app.py /app.py
: Copies your Python script into the image.CMD ['python', '/app.py']
: Specifies the command to run when the container starts.docker build -t my-python-app .
: Builds the image with the tagmy-python-app
.docker run my-python-app
: Runs a container from the custom image.
Example 3: Managing Containers
Learn how to list, stop, and remove containers.
# List running containers
docker ps
# Stop a container
docker stop my-nginx
# Remove a container
docker rm my-nginx
Explanation:
docker ps
: Lists all running containers.docker stop my-nginx
: Stops themy-nginx
container.docker rm my-nginx
: Removes the stoppedmy-nginx
container.
Common Questions and Answers
- What is Docker? Docker is a platform for developing, shipping, and running applications in containers.
- What is a Docker container? A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software.
- How do I install Docker? You can download Docker from the Docker website and follow the installation instructions for your operating system.
- What is a Docker image? An image is a read-only template used to create containers.
- How do I run a Docker container? Use the
docker run
command followed by the image name. - What is Docker Hub? Docker Hub is a cloud-based registry service for sharing Docker images.
- How do I stop a running container? Use the
docker stop
command followed by the container name or ID. - How do I remove a container? Use the
docker rm
command followed by the container name or ID. - What is a Dockerfile? A Dockerfile is a text file that contains instructions for building a Docker image.
- How do I build a Docker image? Use the
docker build
command followed by the path to the Dockerfile. - What is the difference between an image and a container? An image is a blueprint, while a container is a running instance of that image.
- Can I run multiple containers from the same image? Yes, you can run multiple containers from the same image.
- How do I list all Docker images? Use the
docker images
command. - How do I list all running containers? Use the
docker ps
command. - How do I remove a Docker image? Use the
docker rmi
command followed by the image name or ID. - What is a Docker registry? A registry is a storage and content delivery system for Docker images.
- How do I push an image to Docker Hub? Use the
docker push
command after logging in withdocker login
. - How do I pull an image from Docker Hub? Use the
docker pull
command followed by the image name. - What is the difference between
docker run
anddocker start
?docker run
creates and starts a new container, whiledocker start
restarts a stopped container. - How do I access a running container’s shell? Use the
docker exec -it
command followed by the container name and/bin/bash
or/bin/sh
.
Troubleshooting Common Issues
Issue: Docker command not found.
Solution: Ensure Docker is installed and added to your system’s PATH.
Issue: Cannot connect to the Docker daemon.
Solution: Make sure the Docker service is running. On Linux, usesudo systemctl start docker
.
Issue: Permission denied when running Docker commands.
Solution: Try running the command withsudo
or add your user to the Docker group.
Practice Exercises
- Create a Dockerfile for a simple Node.js application and build an image from it.
- Run a MySQL container and connect to it using a MySQL client.
- Experiment with Docker Compose to run a multi-container application.
Remember, practice makes perfect! Don’t hesitate to experiment and try new things. You’ve got this! 💪
For more information, check out the official Docker documentation.