Explore Docker together

 

Docker is an ultra-lightweight virtual machine implemented in a novel way . It is still very different from VM in terms of implementation principle and application. The professional name is Application Container .

Docker application containers have the following advantages over  VMs : 

1. Fast startup, containers usually start within a second, while  VMs usually take longer 

2. High resource utilization. An ordinary PC can run thousands of containers. Try running thousands of VMs .   

3. The performance overhead is small. The  VM usually requires additional CPU and memory to complete the functions of the OS , which occupies additional resources.     

Because the hypervisor of the VM needs to virtualize the hardware and carry its own operating system, it naturally has a relatively large overhead in terms of startup speed, resource utilization and performance. There are two advantages of deep personal experience:   

1.  Rapid deployment, the traditional deployment mode is: install ( package management tool or source package compilation ) -> configure -> run; Docker 's deployment mode is: copy -> run.

2. It can be ensured that the online and test environments are consistent, and the docker container used for testing will be copied directly after planning to go online) 

what is docker?

http://oilbeater.com/docker/2014/06/29/what-is-docker.html

Why should you follow docker?

http://oilbeater.com/docker/2014/06/13/why-you-should-care-about-docker.html

1. docker installation _

debian7 install docker

Reference address: http://www.webmaster.me/server/installing-docker-on-debian-wheezy-in-60-seconds.html

  1. echo deb http://get.docker.io/ubuntu docker main | sudo tee/etc/apt/sources.list.d/docker.list  
  2. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9  
  3. sudo apt-get update  
  4. sudo apt-get install -y lxc-docker 

#Four lines of commands, Docker is installed. Let's create an ubuntu virtual system:

 

  1. docker pull ubuntu #Here is to pull the image named ubuntu from the official website, or you can manually search for the desired image on https://index.docker.io.  
  2. docker run -i -t ubuntu /bin/bash #Create a container, -t is a temporary terminal. 

ubuntu12.04 , windows , macOS install docker _

Refer to docker Chinese documentation http://www.widuu.com/docker/

2 , docker use process practice

2.1  Start the container on the test machine and install ssh

  1. docker run -i -t ubuntu /bin/bash #The container running in this way will be closed after exiting.  
  2. apt-get install openssh-server #安装ssh  
  3. #Need to modify the contents of the /etc/sshd/sshd_config file  
  4. PermitRootLogin yes  
  5. UsePAM no 

2.2  Start ssh , the container runs in the background

 

  1. docker run -d -p 50001:22 <容器id> /usr/sbin/sshd-D  
  2. #Container id can be viewed through docker ps-a, the top one is the latest. 

2.3  Connect to the container via ssh to install the software

 

  1. ssh [email protected] 50001  
  2. #After connecting, you can install whatever you want. You can use exit to exit the container, but the background will still run. 

2.4  After the service installation is complete, stop the container.

  1. docker stop <container id> #stop the running container 

2.5  Submit the container to generate the latest image

  1. docker commit <container id> debian02 #Submit this container to generate a new debian02 image (this image is the integration of the original image and the container) 

2.6  Package image

  1. docker save debian02 >/root/debian02.tar #debian02 image packaging 

2.7  Import the image on another machine

 

  1. docker load < debian02.tar #import image  
  2. docker images #View existing images 

2.8  Start the container

 

  1. docker run -h="redis-test" --name redis-test -d -p 51000:22 -p51001:3306 -p 51003:6379 -p 51004:6381 -p 51005:80 -p 51006:8000 -p 51007:8888 debian02 /etc/rc.local  
  2. #Here is my test machine startup command, specifying the hostname and port mapping.  
  3. #After startup, the program is installed later, and the boot self-start command can be placed in the /etc/rc.local file.  
  4. Docker container migration is simple and convenient, and can be copied and deployed arbitrarily. In the future, you will no longer be afraid of a new deployment environment. There are a bunch of dependencies. 

3. Port mapping for docker containers

Since the IP address of the docker container changes every time it is started, it is not suitable to manually add port mapping ( do you want to check the IP of the container every time it restarts ? ) , so it needs to be automatically added by the docker program every time the container is started. NAT rules, In the early stage, configure the ports that need to be mapped when creating the container as much as possible, as follows:

 

  1. docker run -h="activemq" --name activemq -d -p 51000:22 -p 51001:3306-p 51003:6379 -p 51004:6381 -p 51005:80-p 51006:8000 -p 51007:8888 debian/base/etc/rc.local  
  2. #Here I have mapped mysql, redis, nginx, and ssh. 

For the subsequent management of the docker container, remember the name of the container. If the above name is activemq , use docker stop and start to control the container process.

  1. docker stop activemq  
  2. docker start activemq 

Of course, you can also prevent docker from modifying the container's IP address every time it starts the container, refer to the following:

docker network configuration: http://www.open-open.com/lib/view/open1404896485747.html

4. About the automatic operation of multi-program startup of docker container

Every time a docker container is started, the command for self-starting at boot must be specified before starting the container. For example  , the docker run -I -t debian /bin/bash command will only run the /bin/bash program, and other programs will not run. It is especially tangled for containers that need to run multiple programs.

Multi-program start-up automatic operation method :

You can replace the startup command mentioned above with dockerrun -I -t debian /etc/rc.local , and put all the startup commands that need to be booted in the container in /etc/rc.local , you can achieve multi-program booting Self-started.

The background run is: docker run -d -p 50001:22 debian /etc/rc.local . Note: The run command is to create a new container. If you want to start a container that has been running before, use the command docker ps -a to find the corresponding container ID , and then use docker start < container ID> .

5. About the relationship between docker containers and images

No matter what operations are done in the container, write files, delete files. Nothing will change in the base image of the container.

This is because Docker builds incremental images from the parent image, storing only per-container changes. So if you have a 300MB parent image, if you install 50MB of additional apps or services in the container, your container is only 50MB , and the parent image is still 300MB .

But you can use the Dockfile or commit command to generate a new image with the incremental image and the parent image together.

commit use:

  1. docker commit <container id> <new image name> 

Dockfile uses:

  1. root@yangrong:/data# cat Dockerfile  
  2. FROMubuntu/testa #This is the base image  
  3. CMD["/root/start.sh"] #This is the startup command  
  4. root@yangrong:/data# docker build -t <new image name> ./ 

For more parameter reference address of Dockfile :

http://www.tuicool.com/articles/FRvAbe

http://www.colorscode.net/2014/01/04/howto-build-image-with-automatic-startup-ssh-service-from-dockerfile/

6. Detailed explanation of docker parameters

 

  1. docker  
  2. useage of docker  
  3. -D default false to allow debug mode (debugmode)  
  4. -H The default is unix:///var/run/docker.sock tcp://[host[:port]] to bind or unix://[/path/to/socket] to use (when binary files are used ), when the host ip host=[0.0.0.0], (port) port=[4243] or path=[/var/run/docker.sock] is the default value, use it as the default value  
  5. -api-enable-cors default flase allow CORS headers for remote api  
  6. -b is empty by default, attached to an existing bridge, if the 'none' parameter is used, the container's network is disabled  
  7. -bip is empty by default, use the provided CIDR (ClasslessInter-Domain Routing-Untyped Inter-Domain Routing) marked address to dynamically create a bridge (dcoker0), which conflicts with the -b parameter  
  8. -d default false to allow process mode (daemonmode)  
  9. -dns is empty by default, make docker use the specified DNS server  
  10. -g default is "/var/lib/docker": as the root path used by docker  
  11. -icc defaults to true, allowing inter-container to communicate  
  12. -ip default "0.0.0.0": the default IP address to bind the container port  
  13. -iptables default true to disable docker from adding iptables rules  
  14. -mtu default 1500 : set the maximum unit (mtu) of container network transmission  
  15. -p defaults to the file path used by the /var/run/docker.pid process pid  
  16. -r defaults to true restart the container that was running before  
  17. -s is empty by default, this is docker running using a specified storage drive  
  18. -v default false print version information and exit 

7. Detailed explanation of docker run command

  1. Usage: docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]  
  2. Run a command in a new container  
  3. -a=map[]: Append standard input, output or error output  
  4. -c=0: shared CPU format (relatively important)  
  5. -cidfile="": Write the container ID to a file  
  6. -d=false: detached mode, run the container in the background, and print the container ID  
  7. -e=[]: set environment variables  
  8. -h="": the hostname of the container  
  9. -i=false: keep input stream open even if no input stream is attached  
  10. -privileged=false: extended permissions to the container  
  11. -m="": memory limit (format: <number><optional unit>, unit unit = b, k, m or g)  
  12. -n=true: allow the mirror to use the network  
  13. -p=[]: match the network port number in the mirror  
  14. -rm=false: automatically delete the container when the container exits (cannot be used with -d)  
  15. -t=false: assign a fake terminal input  
  16. -u="": Username or ID  
  17. -dns=[]: custom container DNS server  
  18. -v=[]: Create a mount binding: [host-dir]:[container-dir]:[rw|ro]. If the container directory is missing, docker will create a new volume  
  19. -volumes-from="": mount all volumes of the container  
  20. -entrypoint="": Override the default entry point for mirror settings  
  21. -w="": container inside working directory  
  22. -lxc-conf=[]: add custom -lxc-conf="lxc.cgroup.cpuset.cpus=0,1" 
  23. -sig-proxy=true: proxy to receive all process signals (even in non-tty mode)  
  24. -expose=[]: make your host have no open ports  
  25. -link="": connect to another container (name:alias)  
  26. -name="": Assign the name of the container, if not specified a random one will be generated  
  27. -P=false: Publish all exposed ports to the host interfaces 

8. Summary of common docker commands

 

  1. docker pull <image name:tag> #Pull the image from the official website  
  2. docker search <image name> #Search online available image name 

8.1 Querying Containers, Images, and Logs

 

  1. docker top <container> #Display the processes running in the container  
  2. docker images #Query all images, the default is the most recently created images.  
  3. docker ps #View running containers  
  4. docker ps -l #View the ID of the last exited container  
  5. docker ps -a #View all containers, including exited ones.  
  6. docker logs {container ID|container name} #Query all operation records of a container.  
  7. docker logs -f {container ID|container name} #View easy operation records in real time. 

8.2 Deleting containers and images

 

  1. docker rm$(docker ps -a -q) #delete all containers  
  2. docker rm <container name or ID> #delete a single container  
  3. docker rmi <ID> #delete a single image  
  4. docker rmi$(docker images | grep none | awk '{print $3}' | sort -r) #delete all images 

8.3 Start and stop containers

 

  1. docker stop <container name or ID> #Stop a container  
  2. docker start <container name or ID> #Start a container  
  3. docker kill <container name or ID> #kill a container 

8.4 Container Migration

 

  1. docker export <CONTAINER ID> > /home/export.tar #Export  
  2. cat /home/export.tar | sudo docker import - busybox-1-export:latest # Import export.tar file  
  3. docker save debian> /home/save.tar #Package the debian container  
  4. docker load< /home/save.tar #Load package files on another server 

The comparison reference address of save and export :

http://www.fanli7.net/a/bianchengyuyan/C__/20140423/452256.html

8.5 Running a new container

 

  1. #Run a new container, giving it a name and port mapping. Take the debian02 image as an example  
  2. docker run -h="redis-test" --name redis-test -d -p 51000:22 -p51001:3306 -p 51003:6379 -p 51004:6381 -p 51005:80 -p 51006:8000 -p 51007:8888 debian02 /etc/rc.local  
  3.  
  4. #Copy files from the container, when the container has been closed, the files in it can also be copied out.  
  5. sudo docker cp 7bb0e258aefe:/etc/debian_version . #Copy /etc/debian_version in the container to the current directory. 

8.6 Docker Dockfile image creation

 

  1. root@yangrong:/data# cat Dockerfile  
  2. FROM ubuntu/testa #This is the base image  
  3. CMD ["/root/start.sh"] #This is the startup command  
  4. root@yangrong:/data# docker build -t <new image name> ./ #Generate a new image 

Dockfile more parameter reference:

http://www.tuicool.com/articles/FRvAbe

http://www.colorscode.net/2014/01/04/howto-build-image-with-automatic-startup-ssh-service-from-dockerfile/

< end >

Original link: http://yangrong.blog.51cto.com/6945369/1551327

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326428728&siteId=291194637