Operating Docker Containers

Running a container

Check whether /data/$app already exists or not and proceed,

docker ps -a | grep $app
docker run -d --name $app --hostname $app \
    -v /data/$app:/$app \
docker ps -a | grep $app
docker logs $app
docker exec -ti $app bash

Note. to map all ports randomely at once, use -P instead of -p host:containerport.

Note. to bind to localhost only (reverse-proxy),


Note. if you need to access another container’s service e.g. maria,

--link mariadbprod:mariadb \

Note. if you need to use the service or systemctl commands inside the container,

--cap-add=SYS_PTRACE \


To check the last error logs of a running container,

docker logs <containername>

To get into a container to quickly maintain it,

mkdir ~/bin/
vi shell

[[ -z $1 ]] && echo missing \$1 && exit 1
docker exec -ti $1 bash

shell <containername>

Note. -t for tty and -i for stdin.

Or try to get a grip an its shell or process already,

docker attach <containername>

Note. to get out of the container (does this work only from a shell?) and keep the container (namely its init 1 process) up,

Ctrl-P Ctrl-Q


As most of the containers are Ubuntu powered,

apt -y update
apt full-upgrade
apt autoremove
dpkg -l | grep ^rc | awk '{print $2}'
dpkg -l | grep ^rc | awk '{print $2}' | xargs dpkg --purge

Monitor & Audit

see what’s going on,

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

list the recorded containers,

docker ps -a

list the last created container details,

docker ps -l

show its output,

docker logs <containername>

ask what port corresponds on localhost to container’s port 8080,

docker port <containername> 8080

look for container’s processes,

docker top <containername>

Start / Stop

To stop it,

docker stop <containername>

to start it back,

docker start <containername>

If you used the guides on this website, remember, our images are originally powered by a debug/fake init, so you still need to exec or start the daemon manually,

docker exec -dti <containername> (start the daemon)


docker exec -dti <containername> bash
(start the daemon)

Backup & Change/Relaunch

If you want to make a backup of the container, well, first make sure that’s really what you want to do, as there are special tools to backup databases. And if you’re talking of the applications folders, why not use GIT or any versioning systems for deployments? Anyway if that’s what you want to do, look at the next point, it’s just like building an image based on the current status of the container.

If you even needs to change a runtime setting of the container (e.g. port & link), then you need to use the latest image you made or eventually rebuild one just in case,

apt -y update
apt -y full-upgrade
apt -y autoremove
(eventually clean up things, like log folders)

docker commit -p <containername> <containername>.`date +%s`.status.before.destroy

you can then re-launch using the docker run shown at the top of the document, with the image being <containername>.datetag.ready or <containername>.datetag.status.before.destroy instead of custom/ubuntu.

Shipping Images

Save an image,

#docker save -o $image.tar $image
docker save $image > $image.tar

then ship it to another docker host e.g.,

rsync -avz --delete images.archives/ remoteuser@otherdockerhost:~/images.archives/


scp -r ~/images.archives remoteuser@otherdockerhost:~/

Load it on the other host e.g.,

docker load < $image.tar

Export an image-archive from container and ship it

Export a container,

docker export containername > containername.export.tar

Import a container,

docker import - containername < containername.export.tar

Tips & Tricks

to remove ALL containers (even those which are up),

docker rm `docker ps --no-trunc -aq`

to remove all exited containers,

docker rm `docker ps -q -f status=exited`



latest container,

docker ps -lq

all exited ones,

docker rm `docker ps -qa --no-trunc --filter "status=exited"`
docker ps -a