Original article  Some Docker Tips and Tricks  by  Wouter  Danes

Docker  may seem daunting at first, but it is a really great tool. Working with Shells is annoying and has pitfalls. Since it took me a lot of time to figure it out, I wanted to write this article to save you guys from wasting your time. This post lists some quick tips, tricks, and one-liners to help you use Docker more efficiently.

Remove all containers and images (clean up)

Clean up with one line command:

  docker kill $(docker ps -q) ; docker rm $(docker ps -a -q) ; docker rmi $(docker images -q -a) 

Note: $() in the shell is similar to ``, it will execute the content first, the above script will appear as follows docker kill "pids" ; docker kill is used to stop the container in docker, docker rm deletes the container, docker rmi delete mirror

This will only prompt a warning message when there are no running containers or no containers at all. This is a great one-line command when you want to try it out. If you just want to delete all containers, you can run the following command:

docker kill $(docker ps -q) ; docker rm $(docker ps -a -q) 

remove container on exit

If you just want to quickly run a command in a container, then exit, and don't worry about the container state, add  --rm parameters to the  run command, which will end a lot of your saved containers and clean them up.

Example:docker run --rm -i -t busybox /bin/bash

Do not run commands on the shell

If you do something with a command that requires an extension to the shell  docker run , for example  docker run --rm busybox ls '/var/log/*', the command will fail. It took me a while to figure out the reason for this failure. Here's the catch: you don't have a shell, and ```* is a shell extension, so you need a shell that can be used. The correct way is:

docker run --rm busybox sh -c 'ls /var/log/*'

How Boot2Docker and LapTops handle DNS issues

For this reason, Boot2Docker will tie up the DNS server for a long time. When you try to create an image, you may get outrageous errors. If you see the following prompt on Ubuntu or CentOS:

cannot lookup archive.ubuntu.com

The smart thing to do is to stop and then start boot2docker.

boot2docker-cli down && boot2docker-cli up

So the problem is solved.

Volumes resolution  docker logs and  docker copy issues

If you want to monitor log files and file usage in another container in one container, you can look at  volumes  , for example, to check if tomcat is up:

tomcat_id=$(docker run -d -v /var/log/tomcat6 wouterd/tomcat6)
# Give Tomcat some time to wake up...
sleep 5
while ! docker run --rm --volumes-from ${tomcat_id} busybox /bin/sh -c "grep -i -q 'INFO: Server startup in' /var/log/tomcat6/catalina*.log" ; do
    echo -n "."
    sleep 5
done

你还可以在一个 Dockerfile中指定 volumes ,这个在我前面的博客文章中结合 Docker 连载了。

Docker Inspect 结合 Go Templates 的好处

命令 docker inspect 允许使用 Go Templates 来格式化inspect 命令的输出信息如果你擅长这个,你能获取很多 docker 容器命令行的脚本输出信息。这是一个获取正在运行的容器 IP 的示例:

container_ip=$(docker inspect --format '{{.NetworkSettings.IPAddress}}' ${container_id}) 

这里有一个笨的技巧,用于得到匹配所有暴露(exposed)的端口 host:port ,并且把他们输入一个 Java properties 文件:

sut_ip=${BOOT_2_DOCKER_HOST_IP}
template='{{ range $key, $value := .NetworkSettings.Ports }}{{ $key }}='"${BOOT_2_DOCKER_HOST_IP}:"'{{ (index $value 0).HostPort }} {{ end }}'
tomcat_host_port=$(docker inspect --format="${template}" ${container_id})
for line in ${tomcat_host_port} ; do
    echo "${line}" >> ${work_dir}/docker_container_hosts.properties
done

please read

My post on continuous integration using docker and maven

 

Original  http://blog.segmentfault.com/yexiaobai/119000000048222 9

 

http://blog.csdn.net/junjun16818/article/details/30696295