Docker

Managing Containers

Use the -d flag to start the container in the background and then access its output with docker logs.

docker run -d -p 80:9080 -p 443:9443 websphere-liberty:webProfile8

General Commands

Print the container IDs of all running containers that are based on a particular tag such as websphere-liberty:webProfile8:

docker ps -f "ancestor=websphere-liberty:webProfile8" --format "{% raw  %}{{.ID}}{% endraw %}"

The output of the above command may be sub-shelled into other docker commands. For example, to "log in" to a container based on a particular tag (if there's only one running):

docker exec -it $(docker ps -f "ancestor=websphere-liberty:webProfile8" --format "{% raw  %}{{.ID}}{% endraw %}") bash

Some images do not have sudo installed and you don't know the root password. If root is needed, log in as the root user:

docker exec -u root -it ${CONTAINERID} bash

Print messages.log:

docker exec -it $(docker ps -f "ancestor=websphere-liberty:webProfile8" --format "{% raw  %}{{.ID}}{% endraw %}") cat /logs/messages.log

Tail messages.log:

docker exec -it $(docker ps -f "ancestor=websphere-liberty:webProfile8" --format "{% raw  %}{{.ID}}{% endraw %}") tail -f /logs/messages.log

Seeing Host Processes

Start the container with --pid=host. For an example, see running htop in a container but looking at the host.

Seeing Another Container

Start the container with --pid=container:<name|id>.

Export Filesystem

Export filesystem of a container:

docker export $CONTAINER > export.tar

Image Size

Show the uncompressed image size of an image and all its parent images with docker images. For example:

$ docker images
REPOSITORY                     TAG           IMAGE ID       CREATED          SIZE
containerdiag                  latest        3c38d16cc5e5   45 seconds ago   9.17GB

Debugging

Rooting a Running Container

For debugging purposes, one can "root" into any Docker image which is useful for quickly installing something. Example:

docker run -d -p 80:9080 -p 443:9443 websphere-liberty:webProfile8
docker exec -u root -it ${CONTAINERID} sh
  apt-get update
  apt-get install vim
  vi /config/server.xml

Statistics

docker stats shows periodic information on each container:

CONTAINER ID   NAME            CPU %  MEM USAGE / LIMIT    MEM %   NET I/O      BLOCK I/O PIDS
fca32e320852   serene_hodgkin  3.84%  1.868GiB / 7.78GiB   24.01%  41MB / 629kB 0B / 0B   626