Docker - Container usage
Manage containers
Listing containers
docker container ls
To list all containers:
docker container ls -a
To list all containers and size (
-s
option):docker container ls -sa
Run container
To start new container interactively from an image with its Tag Name or Image ID:
docker container run <TagName|ImageID>
This command always starts a new container, use
docker container start <TagName|ImageID>
to start an exixting stopped one.Example of start and attach to an Alpin linux operating system:
docker run -it --rm alpine /bin/ash
-i, --interactive
: Remain theSTDIN
accessible.-t, --tty
: Allocate a pseudo-tty, bind it on this container.--rm
: Automatically delete the container when it exits./bin/ash
: To run the application/bin/ash
inside the container, without this option, Docker will run/bin/sh
by default.
Execute a command: To run an additional command in existing container.
docker container exec <TagName|ImageID> <command>
Publish a container’s port(s) to the host (
-p
,--publish
)docker run -p 127.0.0.1:80:8080/tcp <TagName|ImageID>
This binds the TCP port
80
on127.0.0.1
from the host to the port ` 8080` from the container.Expose a port or a range of ports
docker run --expose 80 my_ubuntu /bin/bash
This exposes port
80
of the conatiner without publishing the port to the host system’s interfaces.Set evnironment variable (
-e
,--env
,--env-file
)docker run -e FOO=bar --env-file ./env.list <TagName|ImageID> /bin/bash
This connect port
3000
between container and host and set host’s environment variablehttp_proxy
to container.docker container run -e http_proxy -e https_proxy -p 3000:3000 <TagName|ImageID>
Start container
Wake up an exited container:
docker container start <TagName|ImageID>
Instead of using a full container ID, we can use just the first few characters, as long as they are uniquely identifying a container.
Attach to a stoped container:
docker container start -ai CONTAINER
-a
,--attach
: Attach STDOUT/STDERR and forward signals.-i
,--interactive
: Attach to container’s STDIN.
Stop containers:
dokcer stop CONTAINER
# Stop All containers
docker stop $(docker ps -aq)
# Stop specific container
docker stop $(docker ps -aq --filter name="my_container")
docker ps -aq
will listing all the containers with container ID.
Keep a container busy living
Option 1.
Use docker run
with -t
option:
docker run -td IMAGE
-d
: Run container in background and print container ID.-t
,--tty
: Allocate a pseudo-TTY.
Option 2.
A Docker container is supposed to be built with only one process running.
As the container will exit when the process itself exits, there’s a way to keep a ubuntu container busy by continuously printing nothing to the system blackhole /dev/null
:
docker run -d ubuntu tail -f /dev/null
Pause & Unpause
To suspends all processes in the specified containers.
docker pause CONTAINER [CONTAINER...]
Attach to a running container
Examples:
$ docker attach CONTAINER
$ docker exec -it CONTAINER /bin/bash
Remove containers
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Options:
-f
,--force
: Force remove a container even it’s running.-v
,--volumes
: Remove anonymous volumes associated with the container.
Monitoring a container:
Display the running processes of a container
To list processes that running inside of the specific container.
docker top CONTAINER
Display detailed information from containers
docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
To shows the IP address of the container:
docker container inspect --format '{{ .NetworkSettings.IPAddress }}' CONTAINER
-f
,--format
: Format the output using the given Go template.
Display a live stream of container(s) resource usage statistics
docker stats
Quick port check
docker container port CONTAINER
Fetch the logs of a container
docker logs [OPTIONS] CONTAINER
Note: This command is only functional for containers that are started with the
json-file
orjournald
loggin driver.
Options:
--follow
: To continue streaming the new output from the container’sSTDOUT
andSTDERR
.--timestamps
: To add an RFC3339Nano timestamp, for example2021-02-12T08:32:48.001607450Z
to each log entry.