About the relationship between Docker containers and images
No matter what operations are performed in the container, such as writing files or deleting files, the base image of the container will not change at all. This is because Docker builds an incremental image from the parent image and only stores the changes for each container. Therefore, if you have a 300MB parent image and you install an additional 50MB of applications or services in the container, your container is only 50MB, and the parent image remains 300MB. However, you can use the Dockerfile or the commit command to combine the incremental image and the parent image to generate a new image.
dock top -- 显示容器中运行的进程
View the container's root username and password
docker logs <容器名orID> 2>&1 | grep '^User: ' | tail -n1
Because the root user's password is randomly assigned when the Docker container starts, in this way you can get the root user's password of the redmine container.
View container logs in real time
docker logs -f <容器名orID>
Delete all containers
docker rm $(docker ps -a -q)
Stop, start, or kill a container
docker stop <容器名orID> docker start <容器名orID> docker kill <容器名orID>
View all images
docker images
Delete all images
docker rmi $(docker images | grep none | awk '{print $3}' | sort -r)Remove all containers and images (thorough cleanup)
Clean up with a 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 command substitution; it executes the content inside first. The above script will appear as follows: docker kill "pids"; docker kill is used to stop containers in Docker, docker rm deletes containers, and docker rmi deletes images.
When there are no running containers, or no containers at all, this will only display a warning message. This is a great one-liner when you want to try it out. If you simply want to delete all containers, you can run the following command:
docker kill $(docker ps -q) ; docker rm $(docker ps -a -q)
No Shell to Run Commands
If you use the docker run command to handle certain things that require Shell expansion, such as docker run --rm busybox ls '/var/log/', this command will fail. It took me a while to figure out the reason for this failure. The pitfall is here: you don't have a Shell originally, and ``` (backticks) are Shell expansion items; therefore you need a usable Shell. The correct method is:
docker run --rm busybox sh -c 'ls /var/log/*'
Method for automatically running multiple programs at startup:
Every time a Docker container starts, the commands to auto-start must be specified before starting the container. For example, the command docker run -I -t debian /bin/bash will only run the /bin/bash program; no other programs will run, which is especially troublesome for containers that need to run multiple programs. You can replace the startup command mentioned earlier with:
dockerrun -I -t debian /etc/rc.local
In the container, put all commands that need to auto-start at boot into /etc/rc.local, and you can achieve auto-starting multiple programs at boot.
For running in the background:
docker run -d -p 50001:22 debian /etc/rc.local
Delete the container on exit
If you just want to quickly run a command in a container and then exit, without worrying about the container's state, add the --rm parameter after the run command. This will end many containers you have saved and clean them up.
docker run --rm -i -t busybox /bin/bash
Run a new container, while naming it, mapping ports, and mapping folders. Take the redmine image as an example.
docker run --name redmine -p 9003:80 -p 9023:22 -d -v /var/redmine/files:/redmine/files -v /var/redmine/mysql:/var/lib/mysql sameersbn/redmine
Connect one container to another container
docker run -i -t --name sonar -d -link mmysql:db tpires/sonar-server sonar
Connect the container to the mmysql container, and rename the mmysql container to db. In this way, the sonar container can use db's related environment variables.
Saving and importing images
When you need to migrate images from one machine to another, you need to save and load images. Machine a:
docker save busybox-1 > /home/save.tar
Use scp to copy save.tar to machine b, then:
docker load < /home/save.tar
Build your own image
docker build -t <镜像名> <Dockerfile路径>
For example, if the Dockerfile is in the current path:
docker build -t xx/gitlab .
Re-view the container's stdout
# 启动top命令,后台运行 $ ID=$(sudo docker run -d ubuntu /usr/bin/top -b) # 获取正在running的container的输出 $ sudo docker attach $ID top - 02:05:52 up 3:05, 0 users, load average: 0.01, 0.02, 0.05Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 373572k total, 355560k used, 18012k free, 27872k buffers Swap: 786428k total, 0k used, 786428k free, 221740k cached ^C$ $ sudo docker stop $ID
Run in the background (-d) and expose the port (-p)
docker run -d -p 127.0.0.1:33301:22 centos6-ssh
Copy files out of the container
sudo docker cp 7bb0e258aefe:/etc/debian_version .
Copy /etc/debian_version from 7bb0e258aefe to the current directory.
Note: As long as 7bb0e258aefe has not been deleted, the file namespace still exists, and you can safely copy files from a container in the exit state.
Source: http://89ao.gitcafe.io/docker-tricks/