LinuxCommandLibrary

docker-container

Manage (create, start, stop) Docker containers

TLDR

List currently running Docker containers

$ docker container ls
copy

Start one or more stopped containers
$ docker container start [container1_name] [container2_name]
copy

Kill one or more running containers
$ docker container kill [container_name]
copy

Stop one or more running containers
$ docker container stop [container_name]
copy

Pause all processes within one or more containers
$ docker container pause [container_name]
copy

Display detailed information on one or more containers
$ docker container inspect [container_name]
copy

Export a container's filesystem as a tar archive
$ docker container export [container_name]
copy

Create a new image from a container's changes
$ docker container commit [container_name]
copy

SYNOPSIS

docker container [OPTIONS] COMMAND [ARG...]

Common COMMANDs include:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes on files or directories in a container's filesystem
  exec        Run a command in a running container
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls           List containers
  pause      Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune      Remove all stopped containers
  rename     Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start      Start one or more stopped containers
  stats      Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause    Unpause all processes within one or more containers
  update     Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

PARAMETERS

attach
    Attach to a running container's standard input, output, and error streams.

commit [REPOSITORY[:TAG]]
    Create a new image from a container's changes.

cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- OR SRC_PATH CONTAINER:DEST_PATH
    Copy files/folders between a container and the local filesystem.

create [OPTIONS] IMAGE [COMMAND] [ARG...]
    Create a new container, but do not start it immediately.

diff
    Inspect changes on files or directories in a container's filesystem.

exec [OPTIONS] [ARG...]
    Run a command in a running container.

inspect [OPTIONS] [CONTAINER...]
    Display detailed information on one or more containers.

kill [OPTIONS] [CONTAINER...]
    Kill one or more running containers (sends SIGKILL by default).

logs [OPTIONS]
    Fetch the logs of a container.

ls [OPTIONS]
    List containers. This is an alias for docker ps.

pause [CONTAINER...]
    Pause all processes within one or more running containers.

port [PRIVATE_PORT[/PROTOCOL]]
    List port mappings or a specific mapping for the container.

prune [OPTIONS]
    Remove all stopped containers.

rename
    Rename an existing container.

restart [OPTIONS] [CONTAINER...]
    Restart one or more containers.

rm [OPTIONS] [CONTAINER...]
    Remove one or more containers.

run [OPTIONS] [COMMAND] [ARG...]
    Run a command in a new container.

start [OPTIONS] [CONTAINER...]
    Start one or more stopped containers.

stats [OPTIONS] [CONTAINER...]
    Display a live stream of container(s) resource usage statistics.

stop [OPTIONS] [CONTAINER...]
    Stop one or more running containers.

top
    Display the running processes of a container.

unpause [CONTAINER...]
    Unpause all processes within one or more containers.

update [OPTIONS] [CONTAINER...]
    Update configuration of one or more containers.

wait [CONTAINER...]
    Block until one or more containers stop, then print their exit codes.

--help
    Print usage statement for docker container or its specific subcommands.

DESCRIPTION

The "docker-container" (more accurately, docker container) command group is a fundamental component of the Docker command-line interface (CLI) used for managing Docker containers. It provides a comprehensive set of tools to control the lifecycle of containers, from creation and execution to stopping, restarting, and removal. This subcommand consolidates operations that were previously available directly under the main docker command (e.g., docker ps became docker container ls). By organizing container-related functionalities into a dedicated group, Docker improved the CLI's discoverability and consistency. Users leverage docker container to interact with the Docker daemon, enabling them to inspect running containers, view logs, copy files, execute commands inside containers, and perform various maintenance tasks.

CAVEATS

The docker container command group is a subcommand of the main docker CLI, not a standalone executable. It requires the Docker daemon to be running on the host system. Users typically need appropriate permissions (e.g., being part of the docker Unix group or running with sudo) to interact with the Docker daemon. The exact behavior and available options for each subcommand can vary slightly between Docker Engine versions. While older top-level commands like docker ps or docker run often still work for backward compatibility, using the namespaced docker container equivalents is the recommended modern approach.

COMMON WORKFLOWS

Typical workflows involving the docker container command include:
1. Pull an image: docker pull <image_name>
2. Run a new container: docker container run -d -p 80:80 --name mynginx nginx
3. List running containers: docker container ls
4. Execute a command inside a running container: docker container exec -it mynginx bash
5. Inspect a container: docker container inspect mynginx
6. Stop a container: docker container stop mynginx
7. Remove a stopped container: docker container rm mynginx
8. Clean up all stopped containers: docker container prune

CONTAINER STATES

Docker containers can exist in several states, which are reflected by the docker container ls (or docker ps) command:

  • Created: The container has been created but not started.
  • Running: The container is actively executing its primary process.
  • Paused: The container's processes have been suspended.
  • Restarting: The container is in the process of restarting.
  • Exited: The container's primary process has finished or been terminated.
  • Dead: The container should not be running and Docker has attempted to stop it.

HISTORY

Before Docker Engine version 1.13 (released in January 2017), many common Docker commands like docker ps, docker run, and docker stop were top-level commands directly under the docker CLI. As Docker's functionality expanded and its command-line interface grew, it became increasingly complex. To address this, Docker introduced a modular subcommand structure. Commands were logically grouped into namespaces such as docker container, docker image, docker network, and docker volume. This change aimed to improve discoverability, organization, and consistency of the CLI, making it easier for users to find and use related commands. While the old top-level commands are often aliased or still functional for backward compatibility, the docker container group represents the modern, preferred way to manage containers.

SEE ALSO

Copied to clipboard