Docker Basics: Essential Commands
Last updated June 15, 2023
DevOps

Guide Parts
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:
- Checks for the
hello-world
image locally - Downloads it from Docker Hub if not found
- Creates and runs a container from the image
- Displays a message confirming that Docker is working correctly
Basic Docker Commands
Container Management
Command | Description |
---|---|
docker run [OPTIONS] IMAGE [COMMAND] | Creates and starts a new container |
docker ps | Lists running containers |
docker ps -a | Lists all containers (running and stopped) |
docker start CONTAINER | Starts a stopped container |
docker stop CONTAINER | Gracefully stops a running container |
docker kill CONTAINER | Forcefully stops a container |
docker restart CONTAINER | Restarts a container |
docker rm CONTAINER | Removes a stopped container |
docker rm -f CONTAINER | Removes a running container forcefully |
Image Management
Command | Description |
---|---|
docker images | Lists available images |
docker pull IMAGE[:TAG] | Downloads an image from Docker Hub |
docker build -t NAME:TAG PATH | Builds an image from a Dockerfile |
docker rmi IMAGE | Removes an image |
docker inspect IMAGE/CONTAINER | Shows 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.
Read previous part
What is Docker?
Read next part
Docker Compose: Managing Multi-Container Applications
Enjoyed the read? Help us spread the word — say something nice!
Guide Parts
Enjoyed the read? Help us spread the word — say something nice!
Guide Parts