Getting Started with Docker CLI

Tutorial 5 of 5

Getting Started with Docker CLI

1. Introduction

In this tutorial, we'll be getting acquainted with Docker, a popular platform used to develop, ship, and run applications inside containers. Particularly, we'll focus on Docker's Command Line Interface (CLI) and its basic commands.

You'll learn how to manage and interact with Docker containers right from your terminal using Docker CLI.

Prerequisites:

  • Basic knowledge of command-line interfaces
  • Docker installed on your computer. If you haven't installed Docker yet, you can follow the instructions here.

2. Step-by-Step Guide

Concepts:

Docker CLI is a command line interface client that lets you interact with Docker. You can use Docker CLI to create and manage Docker objects, such as images, containers, networks, and volumes.

Best practices and tips:

  • Always use the latest version of Docker CLI for the best features and security fixes.
  • Follow the naming conventions for Docker containers and images for better organization.

3. Code Examples

Let's dive into some practical examples:

Example 1: Pulling a Docker Image

# Pull an image from Docker Hub
docker pull ubuntu:latest

In this example, docker pull ubuntu:latest is used to pull the latest version of the Ubuntu image from Docker Hub.

Example 2: Running a Docker Container

# Run a Docker container
docker run -it ubuntu:latest

Here, docker run -it ubuntu:latest is used to run the Ubuntu container. The -it flag attaches an interactive tty in the new container.

Example 3: Listing Docker Containers

# List Docker containers
docker ps -a

The docker ps -a command is used to list all Docker containers, whether running or stopped.

4. Summary

In this tutorial, we've covered the basics of Docker CLI, including pulling images, running containers, and listing containers. For further learning, you could explore more advanced Docker CLI commands and concepts like Dockerfile, Docker Compose, and Docker Networking.

5. Practice Exercises

Exercise 1:
Pull the nginx image from Docker Hub and list all images to verify it's there.

Solution:

# Pull the nginx image
docker pull nginx:latest

# List Docker images
docker images

Exercise 2:
Run a Docker container using the nginx image, and list all running containers.

Solution:

# Run the nginx container
docker run -d -p 8080:80 nginx:latest

# List Docker containers
docker ps

In this exercise, the -d flag runs the container in detached mode (in the background), and -p 8080:80 maps port 8080 of the host to port 80 in the container.

Keep practicing with different Docker images and commands to get more comfortable with Docker CLI.