1. Logs and monitoring
You can view logs of a container by
docker logs [container_name]
And you can monitor output from a container by
docker logs -f [container_name]
2. Open a shell in the container
If you want to quickly dive into the container, and see what’s inside a particular file, you can do so by:
docker exec -it [container_name] /bin/bash
# or use "sh" if "bash" is not available
docker exec -it [container_name] /bin/sh
However, the container must be already running, if the container failed to start, you have to find another way, e.g. overriding the entry point of the container.
3. Find out useless (not referenced) images
After trying lots of times perfecting a Docker build, you may run into a problem which there are many garbage images.
Output the list of images that is not used by any created containers. (“dangling” as in “dangling pointers”, if you know C)
docker images -f dangling=true
Just output the image IDs
docker images -qf dangling=true
4. Remove all useless images
docker rmi $(docker images -qf dangling=true)
5. Use docker-compose
With Docker compose, you can deploy one or more container simply by docker-compose up -d , and undeploy by docker-compose down .
Docker compose can be regard as a way to specify container config in a config file, but it also do more than that, it helps you to link 2 or more containers together nicely (“orchestration”).
What Docker does is it automatically add the IP of “mariadb” to the “hosts” file of “app”, so you can reference the database by “db”.
More tips and tricks later!