background

Docker Basics: Essential Commands

Last updated June 15, 2023

DevOps

Docker Basics: Essential Commands

Now that you have Docker installed, let's explore the essential commands you'll need to manage containers and images effectively.

Running Your First Container

The simplest way to verify your Docker installation and run your first container is with the hello-world image:

docker run hello-world

This command:

  1. Checks for the hello-world image locally
  2. Downloads it from Docker Hub if not found
  3. Creates and runs a container from the image
  4. Displays a message confirming that Docker is working correctly

Basic Docker Commands

Container Management

CommandDescription
docker run [OPTIONS] IMAGE [COMMAND]Creates and starts a new container
docker psLists running containers
docker ps -aLists all containers (running and stopped)
docker start CONTAINERStarts a stopped container
docker stop CONTAINERGracefully stops a running container
docker kill CONTAINERForcefully stops a container
docker restart CONTAINERRestarts a container
docker rm CONTAINERRemoves a stopped container
docker rm -f CONTAINERRemoves a running container forcefully

Image Management

CommandDescription
docker imagesLists available images
docker pull IMAGE[:TAG]Downloads an image from Docker Hub
docker build -t NAME:TAG PATHBuilds an image from a Dockerfile
docker rmi IMAGERemoves an image
docker inspect IMAGE/CONTAINERShows detailed info about an image or container

Common Docker Run Options

The docker run command has many useful options:

# Run container in detached mode (background)
docker run -d nginx

# Map port 8080 on host to port 80 in container
docker run -p 8080:80 nginx

# Mount a volume
docker run -v /host/path:/container/path nginx

# Set environment variables
docker run -e VARIABLE=value nginx

# Name your container
docker run --name my-nginx nginx

# Run container with interactive terminal
docker run -it ubuntu bash

# Automatically remove container when it exits
docker run --rm nginx

Container Lifecycle Example

Let's work through a practical example using the official Nginx web server:

# Pull the Nginx image
docker pull nginx:latest

# Run Nginx container in detached mode and map port 8080 to 80
docker run -d -p 8080:80 --name my-nginx nginx

# Check that the container is running
docker ps

# View container logs
docker logs my-nginx

# Stop the container
docker stop my-nginx

# Start it again
docker start my-nginx

# Execute a command inside the running container
docker exec -it my-nginx bash

# Remove the container (must be stopped first)
docker stop my-nginx
docker rm my-nginx

After running these commands, you can access the Nginx welcome page at http://localhost:8080 in your web browser.

Docker Container States

A Docker container can be in one of these states:

  • Created: Container created but not started
  • Running: Container running with all processes active
  • Paused: Container processes paused
  • Stopped: Container stopped, but still exists (can be restarted)
  • Deleted: Container removed, no longer exists

In the next section, we'll learn how to create custom Docker images using Dockerfiles.

Enjoyed the read? Help us spread the word — say something nice!

Guide Parts

logo
© 2025 Guidely. All rights reserved.