Most of tasks that are related to the management of containers are performed using the command: docker container. However, some commands process a short notation.
A container or an image can be mentioned by its name (see option --name) or by its id. The container id can be shortened to the few first unique characters.
The container is created but not started. This helps speedup the starting and running of containers.
$ docker container create [options] <image> [command]
Options:
-e : Set environment variables
-i : Run container in interactive mode
--name : Assign a name to the container
-p : publish container's ports to the host (-p 8080:80 host:container)
--rm : Remove the container after it exits
-t : Allocate a terminal to the container
-v : Bind a volume to the container
Command: The command with its arguments to be executed in the container. For instance, /bin/sh -c "echo hello"
$ docker ps
$ docker ps -a
$ docker container ls -a
Options:
-a : Show all containers including the stopped ones
-q : Show container numeric ids
$ docker container start [options] <container>
$ docker start <container>
Options:
-a : Attach stdout and stderr to the container
-i : Attach stdin to the container
$ docker container stop <container>
$ docker stop <container>
$ docker container restart <container>
The streams to be attached are stdin, stdout, and stderr. The container should be running to attach to it.
$ docker container attach <container>
$ docker attach <container>
CTRL-c: Stop the container
CTRL-p CTRL-q: Detach from the container while leaving it running
This command lists the files and directory that were added (A), deleted (D) or changed (C) in the container.
$ docker diff <container>
The container should be running to execute commands.
$ docker run --name malpine -it --rm alpine sh
$ docker exec [options] malpine touch demo.txt
$ docker container inspect <container>
$ docker container logs [options] <container>
$ docker logs [options] <container>
Options:
-f : Follow log output
-t : Show timestamps
$ docker container prune [options]
Options:
-f : Delete without confirmation
$ docker container rm [options] <container>
Options:
-f : Remove container without confirmation
-v : Remove the associated volumes with a container
The run command is the combination of the create and start commands.
$ docker container run [options] <image> [command]
$ docker run [options] <image> [command]
Options:
-a : Attach container to stdin, stdout and stderr
-d : Run container in detach mode (in background)
-e : Set environment variables for container
-i : Run container in interactive mode (open stdin)
--name : Assign a name to the container
-p : Assign a host port to a container port (-p 8080:80)
--rm : Remove a container after it exits
-t : Assign a terminal to a container (for container input and output)
-v : Bind mount a volume to container
Command: The command to be executed in the container after it starts. The command may have arguments.
Examples:
$ docker run -it --rm --name myname <image> /bin/bash
$ docker run -d -p 8080:80 <image>
docker run -a stdin -a stdout -it <image> /bin/bash