Some Docker Tips and Tricks
Category Programming Technology
About the Relationship Between Docker Containers and Images
Regardless of what operations are performed inside a container, such as writing or deleting files, the base image of the container remains unchanged. This is because Docker creates incremental images from the parent image, storing only 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, the container itself will be only 50MB, while the parent image remains 300MB. However, you can use a Dockerfile or the commit command to generate a new image that includes both the incremental image and the parent image.
docker top -- Display running processes in a container
View the root username and password of a container
docker logs <container name or ID> 2>&1 | grep '^User: ' | tail -n1
Since the root user's password in a Docker container is randomly assigned at startup, this method can be used to obtain the root password of a Redmine container.
View container logs in real-time
docker logs -f <container name or ID>
Delete all containers
docker rm $(docker ps -a -q)
Stop, start, or kill a container
docker stop <container name or ID>
docker start <container name or ID>
docker kill <container name or ID>
View all images
docker images
Delete all images
docker rmi $(docker images | grep none | awk '{print $3}' | sort -r)
Remove all containers and images (cleanup)
Use a single command for a cleanup:
docker kill $(docker ps -q) ; docker rm $(docker ps -a -q) ; docker rmi $(docker images -q -a)
Note: In shell, $() is similar to command, which executes the content inside first. The script above will result in docker kill "pids"; docker kill is used to stop containers in Docker, docker rm deletes containers, and docker rmi deletes images.
This will only show a warning message when there are no running containers or no containers at all. This is a great one-liner to try when you want to clean up. If you only want to remove all containers, you can run the following command:
docker kill $(docker ps -q) ; docker rm $(docker ps -a -q)
Not running commands on Shell
If you need to use Docker run commands that require Shell expansion, such as docker run --rm busybox ls '/var/log/', this command will fail. The reason for this failure took me some effort to understand. The trap is here: you originally did not have a Shell, and ``` is a Shell expansion, so you need a Shell to use. The correct method is:
docker run --rm busybox sh -c 'ls /var/log/*'
Method for auto-running multiple programs at startup:
Each time a Docker container starts, the commands to be auto-started 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, and no other programs will run, which is particularly troublesome for containers that need to run multiple programs. You can replace the previous startup command with:
docker run -I -t debian /etc/rc.local
Place all commands that need to be auto-started in the container in /etc/rc.local to achieve auto-start of multiple programs.
Running in the background is:
docker run -d -p 50001:22 debian /etc/rc.local
Delete container upon exit
If you want to quickly run a command in a container and then exit without worrying about the container state, add the --rm parameter to the run command. This will end many saved containers and clean them up.
docker run --rm -i -t busybox /bin/bash
Run a new container, name it, map ports, and map folders. Taking 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 a container to another container
docker run -i -t --name sonar -d --link mmysql:db tpires/sonar-server
sonar
The sonar container connects to the mmysql container and renames the mmysql container to db. This way, the sonar container can use the related environment variables of db.
Saving and Importing Images
When you need to migrate images from one machine to another, you need to save and load the 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 <image name> <Dockerfile path>
If the Dockerfile is in the current path:
docker build -t xx/gitlab .
Re-view the stdout of a container
# Start the top command, running in the background
$ ID=$(sudo docker run -d ubuntu /usr/bin/top -b)
# Get the output of the container that is running
$ 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 ports (-p)
docker run -d -p 127.0.0.1:33301:22 centos6-ssh
Copy files from a container
sudo docker cp 7bb0e258aefe:/etc/debian_version .
Copy /etc/debian_version from 7bb0e258aefe to the current directory.
Note: As long as 7bb0e258aefe is not deleted, the file namespace will still exist, so you can safely copy files from a container that is in the exit state.
>
Article source: http://89ao.gitcafe.io/docker-tricks/
** Click to Share Notes
-
-
-