Docker Commands


While learning about how to run Docker container, we ran few commands. Lets learn more about Docker commands in detail.

What are commands in Docker?

We already know that Docker works in client-server architecture. There are different ways to interact with server(docker daemon), Client CLI one of them and mostly used Docker client. It’s like Linux CLI(Command Line Interface). Based on the command run, it makes api calls to perform corresponding activity in server.

What is basic structure of a Docker command?

docker COMMAND [OPTIONS] [ARGUMENTS]

We have to use docker keyword for running any command related to Docker. COMMAND – Command we want to run. Below are some common commands

Common Commands:
  run         // Create and run a new container from an image
  exec        // Execute a command in a running container
  ps          // List containers
  build       // Build an image from a Dockerfile
  bake        // Build from a file
  pull        // Download an image from a registry
  push        // Upload an image to a registry
  images      // List images
  login       // Authenticate to a registry
  logout      // Log out from a registry
  search      // Search Docker Hub for images
  version     // Show the Docker version information
  info        // Display system-wide information

[OPTIONS][ARGUMENTS] – These are additional values we can pass along with the command

What different types of commands we have?

GENERAL COMMANDS :

// Display installed Docker version and other information
% docker info

// To start the docker daemon in the server
% docker -d

// Get help with Docker
% docker --help

IMAGES COMMAND :

// List all images from local repositories
% docker images

// Build an Image from a Dockerfile
% docker build -t [image_name]:[tag] .   // -t for image_name along with optional tag

// Delete an Image
% docker rmi [image_name]

CONTAINERS COMMAND :

// List all running containers
% docker ps

// List all(running/stopped) containers
% docker ps -a // -a for all

// Create or run a container. If the image is availble in local, it will run otherwise it will pull the image from Dcoker Hub, then run it
% docker run [image_name]

// Run a container in the background
% docker run -d [image_name]  // -d for dettached

// Create and run a container from an image, with a custom name
% docker run --name [container_name] [image_name]

// Run a container and map a container’s port(s) to the host
% docker run -p [host_port]:[container_port] [image_name]  // -p for port

// Start or stop an existing container
% docker start|stop [container_name] (or [container-id])

// Open a shell inside a running container
% docker exec -it <container_name> sh // -it for interactive

// Fetch and follow the logs of a container
% docker logs -f <container_name>  // -f for follow

// Remove a stopped container
% docker rm [container_name]

// Inspect a running container
% docker inspect [container_name] (or [container_id])

// Resource usage stats
% docker container stats

DOCKER HUB COMMANDS :

// Login into Docker Hub
% docker login -u [username]

// Publish an image to Docker Hub
% docker push [username]/[image_name]

// Pull an image from a Docker Hub
% docker pull [image_name]

// Search Docker Hub for an image
% docker search [image_name]

We will use these commands extensively and learn more about the commands and related options / arguements in future.