Container Commands
Running
# interactive shell inside a container
docker run -it ubuntu /bin/bash
docker run -it ubuntu:16.04 /bin/bash # default user is root
# detached, named, with a published port
docker run --name mywildfly -d -p 8080:8080 jboss/wildfly
# remove the container automatically when it exits
docker run --rm busybox:latest /bin/echo "Hello world"
# attach to a user-defined network
docker network create mynetwork
docker run --name mywildfly-net -d --net mynetwork -p 8080:8080 jboss/wildfly
# mount a host directory into the container
docker run --name mywildfly-volume -d \
-v myfolder/:/opt/jboss/wildfly/standalone/deployments/ \
-p 8080:8080 jboss/wildfly
# create without starting
docker create --name hello-world-cont helloworld
The -p host:container flag publishes a port. In -p 8080:8080 the first number is the host
port and the second is the port inside the container; they need not match.
Listing and lifecycle
docker ps # running containers only
docker ps -a # all containers, including stopped
docker ps -a -q # all container IDs only
docker start <name|id>
docker stop <name|id> # SIGTERM, then SIGKILL after a grace period
docker stop -t 1 <name|id> # one-second timeout
docker restart <name|id>
docker pause <name|id>
docker unpause <name|id>
docker kill <name|id> # SIGKILL immediately
docker rename <old-name> <new-name>
A container is always in one of these states: created, restarting, running, removing,
paused, exited, or dead. Filter on them:
docker ps --filter status=running
docker ps --filter status=paused
docker ps -a --filter 'exited=0'
Removing
docker rm <name|id> # a stopped container
docker rm -f <name|id> # force-stop, then remove
docker rm -f $(docker ps -aq) # every container
docker rm $(docker ps -q -f "status=exited") # stopped containers only
docker container prune # all stopped containers
Inspecting and interacting
docker logs <name|id>
docker logs -f <name|id> # follow
docker logs --tail 100 <name|id> # last 100 lines
docker exec -it <name|id> /bin/bash # new interactive process in a running container
docker exec <name|id> ls
docker exec <name|id> hostname
docker attach <name|id> # attach to the main process's stdio
docker top <name|id> # processes running inside
docker diff <name|id> # filesystem changes since the image
docker inspect <name|id> # full low-level JSON
docker inspect --format '{{ .State.Status }}' <id>
docker inspect --format '{{ .State.Pid }}' <id>
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <id>
docker cp <name|id>:/path/in/container ./local/path
docker cp ./local/file <name|id>:/path/in/container
docker exec fails if the container is paused — unpause it first. docker diff marks each change
as A (added), D (deleted), or C (changed).
Resource limits and monitoring
docker stats # live resource usage, all containers
docker stats <name|id> # one container; Ctrl+C to exit
docker system df # disk used by images, containers, volumes
docker events # real-time event stream from the daemon
docker info
# cap memory and CPU
docker run -m 512m --cpu-quota 50000 <image>
--cpu-quota 50000 against the default 100000-microsecond period allows roughly half a CPU core.
Setting limits matters: by default a container may consume all available host memory.