docker Command: Tutorial & Examples

Manage docker containers

Docker is an open-source platform that automates the deployment, scaling, and management of applications inside lightweight, portable, self-sufficient containers. The docker command is the main command used to interact with Docker. It is used to create, manage, and delete Docker containers, images, networks, and volumes.

What Does the docker Command Do?

The docker command provides a command line interface (CLI) to interact with the Docker daemon, which is the background service running on the host that manages building, running and distributing Docker containers. The command allows you to build, run, manage, and distribute Docker containers.

How Does the docker Command Work?

The docker command sends commands to the Docker daemon, which then performs the requested action. For example, if you run the command docker run ubuntu:18.04, the Docker CLI sends a request to the Docker daemon to run a Docker container with the Ubuntu 18.04 image.

docker run ubuntu:18.04

How to Use the docker Command

To use the docker command, you first need to have Docker installed on your system. Once Docker is installed, you can use the docker command followed by a command like run, pull, push, build, etc.

Here are some basic examples of how to use the docker command:

To pull (download) an image from Docker Hub:

docker pull ubuntu:18.04

To run a Docker container:

docker run ubuntu:18.04

To list all running Docker containers:

docker ps

To stop a running Docker container:

docker stop container_id

Why is the docker Command Important?

The docker command is important because it is the primary way of interacting with Docker. Without the docker command, you would have to interact with the Docker API directly, which would be much more complex and time-consuming.

Common docker Command Parameters

There are many parameters that can be used with the docker command. Here are a few of the most common ones:

  • -i, --interactive: Keeps STDIN open, even if not attached. Useful for interactive sessions.
  • -t, --tty: Allocates a pseudo-TTY. Useful for attaching an interactive shell.
  • -p, --publish: Publishes a container’s port to the host. Format: <hostport>:<containerport>
  • -d, --detach: Runs the container in the background.
  • --name: Assigns a name to the container.

To run a Docker container in detached mode, with an interactive shell, and with a specific name, you would use the following command:

docker run -dit --name my_container ubuntu:18.04

Potential Problems and Pitfalls

While the docker command is powerful and flexible, there are a few potential issues to be aware of:

  • Docker Daemon Not Running: If the Docker daemon is not running, you will get an error message when you try to run any docker command. To fix this, you need to start the Docker daemon.
  • Insufficient Resources: If your system does not have enough resources (CPU, memory, disk space) to run a Docker container, the docker run command will fail. You may need to free up resources or add more resources to your system.
  • Networking Issues: Docker containers can sometimes have issues connecting to the network. This could be due to a network issue on your system, or a problem with Docker's networking configuration.
  • Image Not Found: If you try to run a Docker container with an image that does not exist on your local system or on Docker Hub, you will get an error message. You need to pull the image first with the docker pull command.
Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA