Docker's detailed basic usage

Source: Internet
Author: User
Tags docker ps docker cp docker run

Docker is a new way to achieve the ultra-lightweight virtual machine, in the implementation of the principle and application is still a huge difference with the VM, the professional term is the application container (application Container). (I personally prefer to call virtual machines) Docker application capacity

Dockeris a new way to achieve the ultra-lightweight virtual machine, in the implementation of the principle and application is still a huge difference with the VM, the professional term is the application container (appliCatIon Container). (I personally prefer to call it a virtual machine)

The Docker application container has several advantages over VMS:
Fast Start-up, containers typically start in less than a second, and VMS typically take longer
High utilization of resources, a common PC can run thousands of containers, you run thousands of VMs to try
With little performance overhead, VMS typically require additional CPU and memory to complete the functionality of the OS, which takes up additional resources

Because the VM's hypervisor need to implement the virtualization of the hardware, and also to carry their own operating system, naturally in the boot speed and resource utilization and performance of a relatively large cost.

Personal experience of the two deep advantages:
    1. Rapid deployment, the traditional deployment mode is: Installation (Package management tool or source package compilation), configuration---run, Docker deployment mode is: Copy, run.

    2. Ensure consistent online and test environments and plan to replicate the Docker containers used for testing directly on-line


What is Docker?
Http://oilbeater.com/docker/2014/06/29/what-is-docker.html

Why should you focus on Docker?
Http://oilbeater.com/docker/2014/06/13/why-you-should-care-about-docker.html

1. Docker installation
Debian7 Installing Docker
Reference Address: http://www.webmaster.me/server/installing-docker-on-debian-wheezy-in-60-seconds.html
Echo Deb Http://get.docker.io/ubuntu Docker main | sudo tee/etc/apt/sources.list.d/docker.list sudo apt-key adv--keyserver keyserver.ubuntu.com--recv-keys 36a1d7869245c8950f966e92d8576a8ba88d21e9 sudo apt-get update sudo apt-get install-y lxc-docker
#四行命令, Docker is ready to install. Create an Ubuntu virtual system below:
Docker pull Ubuntu #此处是从官网拉取名为ubuntu的image, or you can manually search for the desired image on Https://index.docker.io. Docker run-i-T Ubuntu/bin/bash #创建一个容器,-T is a temporary terminal.

ubuntu12.04, Windows, MacOS install Docker
Refer to Docker Chinese document http://www.widuu.com/docker/

2. Docker Use process Practice
2.1 Start the container in the test machine, install SSH
Docker run-i-T Ubuntu/bin/bash #此方式运行的容器 and the container shuts down after exiting. Apt-get Install openssh-server #安装ssh #需要修改/etc/sshd/sshd_config file contents permitrootlogin Yes usepam no
2.2 Start SSH, the container will run in the next stage
Docker run-d-P 50001:22 < container id>/usr/sbin/sshd-d #容器id可通过 docker ps-a View, the top is up to date.
2.3 Connecting to the container installation software via SSH
SSH [email protected] 50001 #连上后想装什么就装什么, you can use Exit to exit the container, but the background will also run.
2.4 After the service installation is complete, stop the container.
Docker Stop < container id> #停止运行的容器
2.5 Submit the container to generate the latest image
Docker Commit < container id> debian02 #把这个容器提交生成新的debian02镜像 (this image is the integration of the original image and the container)
2.6 Packaging Images
Docker Save Debian02 >/root/debian02.tar #debian02镜像打包
2.7 Import the image on another machine
Docker Load < Debian02.tar #导入镜像 Docker images #查看存在的镜像
2.8 Starting the Container
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 #此处是我测试机器启动命令, specify host name and port mappings.  #启动后, the program is installed in the back, the boot command can be placed in the/etc/rc.local file. Docker container migration is simple and convenient, can be arbitrary copy deployment, later no longer afraid of the new deployment environment, a bunch of dependent pretending to die have wood.

3. Port Mappings for Docker containers
Because the IP address of the Docker container is changed every time, it is not suitable for manually adding port mappings (does each reboot look at the container's IP?). ), so you need to automatically add NAT rules to the Docker program each time you start the container, and as far as possible, configure the ports that need to be mapped when creating the container, as follows:
Docker run-h= "ACTIVEMQ"--name activemq-d-P 51000:22-p 51001:3306-p 51003:6379-p 51004:6381-p 51005:80-p 51,006:8,000 -P 51007:8888 debian/base/etc/rc.local #此处我把mysql, redis,nginx,ssh are mapped.
Subsequent to the management of the Docker container, remember the name of the container, if the name above is ACTIVEMQ, use Docker Stop,start to control the container process.
Docker stop Activemq Docker start ACTIVEMQ
Of course, you can also not allow Docker to change the container's IP address each time it launches the container, as follows:
Docker Network configuration: http://www.open-open.com/lib/view/open1404896485747.html

4. Automatic operation of multi-program boot for Docker container
Each time the Docker container starts, the boot-up commands are specified before the container starts. such as the Docker run-i-T debian/bin/bash command, will only run/bin/bash programs, other programs will not run, for the container to run multiple programs particularly tangled.

Multi-Program Boot automatic operation method:

Can be said before the start of the command to Dockerrun-i-T debian/etc/rc.local, in the container all need to boot from the start command in the/etc/rc.local, you can reach the multi-program boot from the boot.

The background runs: Docker run-d-P 50001:22 debian/etc/rc.local. Note: The Run command creates a new container, and if you want to start a container that has ever been run, use the command Docker ps-a to find the corresponding container ID, and then use the Docker start < container id>.

5. The relationship between Docker containers and mirrors
No matter what operation the container does, write the file, delete the file. There is no change to the basic image of the container.

This is because Docker creates an incremental image from the parent image and stores only the changes for each container. So if you have a 300MB parent image, if you have 50MB of additional apps or services installed in the container, your container is only 50MB, and the parent image is 300MB.

However, you can use the dockfileor the commit command to create a new image with the incremental image and the parent image.

Commit using:
Docker Commit < Container id> < new image name >
Dockfile using:
[Email protected]:/data# cat Dockerfile fromubuntu/testa #这是基础镜像 cmd["/root/start.sh"] #这是启动命令 [email protected]:/data# Docker Build-t < new mirror name >./

More parameter reference addresses for 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, docker parameter detailed
docker  useage of docker  -d  default false  Allow debug mode (DebugMode)   -H   Default is unix:///var/run/docker.sock tcp://[host[:p ORT]] to bind   or Unix://[/path/to/socket] to use (the binary file), When host ip host=[0.0.0.0], (port) port=[4243]  or  path=[/var/run/docker.sock] is the default value to use as the default value   - api-enable-cors  default flase  Allow Cors header remote api  -b  is empty by default, attached to an existing bridge, if the ' none ' parameter is used, The network that is disabled for the container   -bip  is empty by default, using the provided CIDR (classlessinter-domain routing-non-type inter-domain routing) tag address to dynamically create the Network Bridge (DCOKER0), and-B parameter conflicts   -d  default false  Allow process mode (Daemonmode)   -dns  default is empty, enabling Docker to use the specified DNS server    -g  default is "/var/lib/docker": Root path used as Docker   -icc  default True, allow Inter-container to communicate    -ip  default "0.0.0.0": Default IP address of the bound container port   -iptables  default true  Disable Docker add Iptables rule   -mtu   Default 1500 :  sets the maximum unit (MTU) for container network transport   -p  The default is the file path used by the/VAR/RUN/DOCKER.PID process PID   - r  default is true  heavyThe container before the boot   -s  the default is empty   This is Docker run is using a specified storage drive   -v  default false  print version information and exit 

7. The Docker Run command is detailed
Usage: docker run [options] image[:tag] [command] [arg ...]   Run a command in a new container  -a=map[]:  additional standard input, Output or Error output   -c=0:  shared CPU format (relatively important)   -cidfile= "":  writes the ID identifier of the container to the file   -d=false:   Detach mode, run the container in the background, and print out the container id  -e=[]: Set environment variable   -h= "":  Container's host name   -i=false:  Keep the input stream open even if there is no additional input stream   -privileged=false:  permissions to the container extension   -m= "":  Memory Limit   (format: <number ><optional unit>, unit unit  = b, k, m or g)   -n=true:   Allow mirroring to use network   -p=[]:  match network port numbers within mirrors   -rm=false: Automatically delete containers when the container exits   (cannot be used with  -d)   -t=false:  assigning a forged terminal input   -u= "":  username or id  -dns=[]:  custom container DNS server   -v=[]:  Create a mount binding: [Host-dir]:[container-dir]:[rw|ro]. If the container directory is missing, Docker creates a new volume   - Volumes-from= "":  mount all volumes of the container   -entrypoint= "":  overwrite mirror set default entry point   -w= "":  container in working directory   -lxc-conf=[]:  add Custom-lxc-conf= " lxc.cgroup.cpuset.cpus = 0,1 " -sig-proxy=true:  agent receives all process signals (even in non-tty  mode)   -expose=[]:  let you host no open ports   -link= "":  connected to another container (Name:alias)   -name= "":  the name of the assigned container and randomly generates a   -p=false: publish all exposed ports to if not specified  thehost interfaces  publish all displayed port host interfaces

8, Docker Common Command summary
Docker Pull < mirror name:tag> #从官网拉取镜像 Docker Search < mirror name > #搜索在线可用镜像名
8.1 Querying containers, mirrors, logs
Docker top <container> #显示容器内运行的进程 Docker images #查询所有的镜像, the default is the most recently created row.  Docker PS #查看正在运行的容器 Docker ps-l #查看最后退出的容器的ID Docker ps-a #查看所有的容器, including exit.  Docker logs {container id| container name} #查询某个容器的所有操作记录. Docker logs-f {container id| container name} #实时查看容易的操作记录.
8.2 Deleting containers and mirrors
Docker rm$ (Docker ps-a-Q) #删除所有容器 Docker RM < container name or id> #删除单个容器 Docker rmi <ID> #删除单个镜像 Docker rmi$ (Docker Images | grep none | awk ' {print $} ' | SORT-R) #删除所有镜像
8.3 Start Stop container
Docker Stop < container name or id> #停止某个容器 Docker start < container name or id> #启动某个容器 Docker kill < container name or id> #杀掉某个容器
8.4 Container Moving Device
Docker export <container id> >/home/export.tar #导出 Cat/home/export.tar | sudo docker import-busybox-1-export:latest # import Export.tar file docker save debian>/home/save.tar #将debian容器打包 Docker load
Comparison of Save and Export reference address: http://www.fanli7.net/a/bianchengyuyan/C__/20140423/452256.html

8.5 Running a new container
#运行一个新容器, name it, Port map. Take DEBIAN02 Mirror as an example docker run-h= "Redis-test"--name redis-test-d-P 51000:22-p51001:3306-p 51003:6379-p 51004:6381-p 510  05:80-p 51006:8000-p 51007:8888 debian02/etc/rc.local #从container中拷贝文件, when the container is closed, the files inside can be copied. sudo docker CP 7bb0e258aefe:/etc/debian_version. #把容器中的/etc/debian_version Copy to the current directory.
8.6 Docker Dockfile Image maker
[Email protected]:/data# cat Dockerfile from Ubuntu/testa #这是基础镜像 CMD ["/root/start.sh"] #这是启动命令 [email protected]:/dat a# Docker build-t < new mirror name >./#生成新的镜像

Docker's detailed basic usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.