Operate & Maintain Docker Instances

Basic operations

app=APP-NAME
image=IMAGE-NAME

start

docker run -d --name $app --hostname $app --workdir /root $image
docker ps -a | grep $app
docker logs $app

get a shell within

docker exec -ti $app /bin/bash

get a grip on the process within

docker attach $app
^P ^Q

start while keeping the grip – useful for e.g. Devuan SVR4 init

docker run -dti --name $app --hostname $app --workdir /root $image
^P ^Q

start with alternate ENTRYPOINT and CMD while keeping the grip

docker run -dti --name $app --hostname $app --workdir /root \
    --entrypoint ALTERNATE-ENTRYPOINT $image ALTERNATE-CMD
^P ^Q

status

docker ps
docker ps -a
docker ps -l

maintain – get into an instance’s pants really quick – note -t for tty and -i for stdin

docker exec -ti $app bash

stop/start (entrypoint gets re-executed at startup)

docker stop $app
docker start $app

clean-up

docker rm $app

Volumes

check whether /data/$app/ already exists or not

and proceed

docker ps -a | grep $app
docker run -d --name $app --hostname $app --workdir /root \
    -v /data/$app:/$app \
    $image

Network

bind a host port to an port within and check

docker run -p HOSTPORT:INSTANCEPORT ...
docker port $app INSTANCEPORT

bind to localhost only (reverse-proxy)

-p 127.0.0.1:HOSTPORT:INSTANCEPORT

map all ports randomely at once

-P

Access another container’s service e.g. MariaDB

--link mariadbprod:mariadb

Backup / import / export

see Shipping instances and images across continents

Monitoring & auditing

docker top $app
docker stats
docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}"

Wipe-out all instances

grab the ID of the latest created (not restarted?) container

docker ps --latest --quiet

get rid of all instances that are down

docker rm `docker ps --quiet --no-trunc`
#docker ps | sed 1d | awk '{print $1}' | xargs docker rm
# --filter status=exited

wipe-out all instances incl. those which are up

docker rm `docker ps -a --quiet --no-trunc` --force
#docker ps -a --quiet --no-trunc | xargs docker rm --force

Additional notes

enable systemctl command within

--cap-add=SYS_PTRACE

start with an init in case it does not already have one

docker run --init -d $image

start with an init

docker run --init -d $image

TODO

Resources

Docker Overview https://docs.docker.com/engine/understanding-docker/

Getting started with Docker https://coreos.com/os/docs/latest/getting-started-with-docker.html

Get started with images https://docs.docker.com/v1.8/userguide/dockerimages/

Comment installer et exploiter Docker sur Red Hat ou CentOS http://blog.deliverous.com/2014-07-10.docker-tomcat-centos.html

docker https://doc.ubuntu-fr.org/docker

clean-up

https://coderwall.com/p/ewk0mq/stop-remove-all-docker-containers

https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430

https://docs.docker.com/config/pruning/

docker rm https://docs.docker.com/engine/reference/commandline/rm/

How To Remove Docker Containers, Images, Volumes, and Networks https://linuxize.com/post/how-to-remove-docker-images-containers-volumes-and-networks/

fixup

How to start a stopped Docker container with a different command? https://stackoverflow.com/questions/32353055/how-to-start-a-stopped-docker-container-with-a-different-command

Execute the same entrypoint script on docker container restart https://stackoverflow.com/questions/49052278/execute-the-same-entrypoint-script-on-docker-container-restart

How to Override Entrypoint Using Docker Run https://phoenixnap.com/kb/docker-run-override-entrypoint

Dockerfile: ENTRYPOINT vs CMD https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/

run vs. create

docker run vs create+start: why are created containers different? https://stackoverflow.com/questions/45771746/docker-run-vs-createstart-why-are-created-containers-different

Docker run vs create https://stackoverflow.com/questions/37744961/docker-run-vs-create/37745900


GUIDES | LECTURES | BENCHMARKS | SMTP HEALTH