Docker Quick Reference

Table of Contents

Docker Compose Force Rebuild

Also see here.

docker-compose rm -v -f
docker-compose build --no-cache
docker-compose up --force-recreate

For compose files where you have trouble removing referenced images, try:

docker-compose down --rmi all

See Docker Compose reference.

Remove a Volume

docker volume ls
docker volume rm -f some_grafana_data

You might then see:

Error response from daemon: unable to remove volume: remove some_grafana_data: volume is in use - [4633083c16d8cab09af2163a2e76105ce1a7e2a5e2d23289f7b67125c607c9a6]

In which case:

docker rm -f 4633083c16d8cab09af2163
docker volume rm -f some_grafana_data

Working With Docker for Mac

Aliasing 0.0.0.0 to work around dynamic IPs

To solve this problem:

I want to connect from a container to a service on the host. The
mac has a changing IP address or no address if you have no network access

The current recommendation is to attach an unused IP to the lo0 interface on the Mac e.g.

sudo ifconfig lo0 alias 10.200.10.1/24

Make sure your service is listening on this address or 0.0.0.0
(i.e. not 127.0.0.1). Then any containers which need to access the
service can use the 10.200.10.1 address.

Accessing The Xyve Virtual Machine

You might need to do this after restarting/upgrading docker. To get into the xyve vm use screen:

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty 

The login is ‘root’, no password.

Add an Internal Company Registry or Repo

vi /etc/hosts 

and add the address e.g.:

10.10.10.100   my-registry.mycompany.com

Exit screen with Ctrl-a then k.

Here is a screen reference: http://ss64.com/osx/screen.html.

Get Into a Docker Container without docker-enter

docker exec -it 963ffd75c1b5 /bin/bash

Get Into a Docker Image which Fails to Start

docker run -it applications_mycontainer /bin/bash 

Use docker images to find the correct image name

Free up Disk Space Used by Docker

See this reference.

For issues related to the size of the Docker.qcow2 file, see this link.

Temporarily Install a Package for Troubleshooting

If you need to, you can probably simply install packages in any transient container, e.g.:

apt-get update
apt-get install netcat
nc -v -w3 10.10.10.100 5432

Run A Container With Env Variables for Testing

docker run -it -e  MY_ENV_VAR=somevalue myrepo/myapp:latest /bin/bash

Increase Terminal Width to View Truncated Lines

E.g. if output of ps is truncated at 80 characters by default. This happens because ps is using the width from the $COLUMNS variable. Specify a large value for $COLUMNS when using ps, e.g.:

root@ff41cab9438f:/# COLUMNS=1000 ps -aux

Remove Containers Created and Exited 2 Weeks ago

docker ps -a | grep "2 weeks" | grep "Exited" | awk '{print $1}' | sudo xargs docker rm

Remove All Exited Containers

docker ps -a | grep Exit | awk '{print $1}' |  xargs docker rm

Grep Docker stdout and stderr

E.g. if you have an application that’s writing to both streams and you’re not sure where logs are going:

docker logs 4017b8a68f98 2>&1 | grep

Keep a Container Running

E.g. if it starts and immediately exits:

sudo docker run -t -i my-registry/mycontainer /bin/bash

This will attach you directly to the container so you can check the logs.

sudo docker run -d my-registry/my-container /bin/bash

This will start it detached.

Run A Container With a Link to Another

docker run --rm -t -i --name mycontainer --link docker_consul_1 my-registry/mycontainer

Logs

Get the last 20 lines of logs:

docker logs --tail 20 fc805ce579d3

Other References

Cheat sheet.

View and edit this post on GitHub