Let's learn what commands can be used when object is container in the following Docker CLI syntax:
docker <object> <command> <options>
docker container
$ docker containerUsage: docker container COMMAND
Manage containers
Commands:
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 to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
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
Run 'docker container COMMAND --help' for more information on a command.
---
If we try to start some container which requires opening some port on the localhost while another running container has already opened it, we might get the following error:
$ docker-compose up --build
Starting my_app_1 ...
ERROR: for my-app_db_1 Cannot start service db: driver failed programming external connectivity on endpoint my-app_db_1 (a456b10867b734493c831aa99f227147110f61233652c4984415c12ecdf9a9b3): Bind for 0.0.0.0:5432 failed: port is Starting my-app_my-service_1 ... done
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint my-app_db_1 (a456b10867b734493c831aa99f227147110f61233652c4984415c12ecdf9a9b3): Bind for 0.0.0.0:5432 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
To resolve this, we first need to check which containers are running and then to stop them:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
499c43a9c28b postgres:latest "docker-entrypoint.s…" 3 days ago Up 3 days 0.0.0.0:5432->5432/tcp my-app-2_db_1
$ docker container stop my-app-2_db_1
my-app-2_db_1
Let's verify that no containers are running:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
---
docker container inspect
Useful command for debugging issues. It shows info about various container's properties e.g. network.
% docker container inspect --help
Usage: docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Display detailed information on one or more containers
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes
docker container kill
Stops container (non-gracefully). Killing the container does not give a chance to applications running inside it to shut down / terminate gracefully. This command sends a SIGKILL signal to the main process running inside container. This signal immediately terminates this process and this process cannot ignore it. SIGKILL also automatically terminates all child processes.
% docker container kill --help
Usage: docker container kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
-s, --signal string Signal to send to the container (default "KILL")
docker container ls
$ docker container ls --help
Usage: docker container ls [OPTIONS]
List containers
Aliases:
ls, ps, list
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
To list all containers:
$ docker container ls -a
To list only numeric IDs of all containers:
$ docker container ls -a -q
or
$ docker container ls -aq
docker container pause
This command transits specific container(s) into a Paused state. This feature is used to lower container's utilization of host's resources (CPU etc...so its battery) when running applications in container is not currently needed.
$ docker container pause --help
Usage: docker container pause CONTAINER [CONTAINER...]
Pause all processes within one or more containers
CONTAINER is a full container id or just first several characters that can uniquely identify it.
Example:
$ docker container pause 5a7
To resume the processes and transit the container back to Up state:
$ docker container unpause 5a7
$ docker container rm --help
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
To remove all containers:
$ docker container rm $(docker container ls -aq)
docker container rm
$ docker container rm --help
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
To remove all containers:
$ docker container rm $(docker container ls -aq)
To display detailed information on one or more containers:
$ docker container inspect <container id or name>
docker container run
Exploring Docker commands: docker [container] run | My Public Notepad
docker container stop
Stops the container (gracefully). Stopping the container in this way gives applications running inside it to stop/terminate gracefully. This can be verified by observing docker logs for a given container during the process of stopping it. Unix signal that gets sent to the main process running inside the container is SIGTERM. Process can handle this signal or can even ignore it. SIGTERM does not kill child processes so main process can choose to stop them gracefully.
% docker container stop --help
Usage: docker container stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
When the terminal is attached to a container output we can press CTRL+C in order to to shut down the container.
No comments:
Post a Comment