Docker Usage Basics Summary

Source: Internet
Author: User
Tags docker hub docker run

Docker usage Notes

Authorization: Hu Jinlin

Email: [Email protected]

1. Installing Docker

CentOS6

For CentOS6, you can use the EPEL library to install Docker with the following command

Yum Install Http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpmyum Install Docker-io


CentOS7

The CENTOS7 system has Docker in the Centos-extras library and can be installed directly:


Yum Install Docker

Start the Docker service after installation and let it load automatically with the system boot.


Service Docker Startchkconfig Docker on

2. Get the image

You can use the Docker pull command to get the required mirrors from the warehouse.


The following example downloads an Ubuntu 12.04 operating system image from the Docker Hub repository.

$ sudo docker pull ubuntu:12.04pulling repository ubuntuab8e2728644c:pulling dependent layers511136ea3c5a:download Comp Lete5f0ffaa9455e:download completea300658979be:download complete904483ae0c30:download Completeffdaafd1ca50: Download Completed047ae21eeaf:download Complete

During the download process, each layer of information that gets the image is output.

The command is actually equivalent to the sudo docker pull registry.hub.docker.com/ubuntu:12.04 command, which is downloaded from the Ubuntu repository in the registry server registry.hub.docker.com to 1 2.04 of the image.

Sometimes the official warehouse registration server download is slow and can be downloaded from other warehouses. You need to specify the full warehouse registration server address when downloading from another warehouse. For example

$ sudo docker pull dl.dockerpool.com:5000/ubuntu:12.04pulling dl.dockerpool.com:5000/ubuntuab8e2728644c:pulling Dependent Layers511136ea3c5a:download complete5f0ffaa9455e:download Completea300658979be:download Complete904483ae0c30:download completeffdaafd1ca50:download Completed047ae21eeaf:download Complete

Once you're done, you can use the image at any time, such as creating a container to run the Bash app.


3. List of Mirrors

Use Docker images to display a local image.

$ sudo docker imagesrepository       tag       IMAGE ID      CREATED       virtual sizeubuntu           12.04     74fe38d11401  4 weeks ago  209.6 MBubuntu            precise  74fe38d11401  4 weeks  ago  209.6 mbubuntu           14.04     99ec81b80c55  4 weeks ago  266 MBubuntu            latest   99ec81b80c55  4  weeks ago  266 mbubuntu            Trusty   99ec81B80C55  4 WEEKS AGO  266 MB ... 

In the listing information, you can see several field information

from which warehouse, such as Ubuntu

Mirrored tags, such as 14.04

Its ID number (unique)

Creation time

Mirror size

Where the image ID uniquely identifies the mirror, notice that ubuntu:14.04 and ubuntu:trusty have the same mirror ID, which means they are actually the same mirror.

Tag information is used to mark different images from the same warehouse. For example, there are multiple images in the Ubuntu repository, which can be differentiated by TAG information, such as 10.04, 12.04, 12.10, 13.04, 14.04, and so on.


4. Start the container

Docker Run-t-I Centos:latest/bin/bash

If you do not specify a specific tag, the latest tag information is used by default.

5, the container basic use

Container life cycle Management-docker [Run|start|stop|restart|kill|rm|pause|unpause] container operation operations-docker [ps|inspect|top|attach|events|logs| Wait|export|port] Container rootfs command-docker [commit|cp|diff] Mirror warehouse-docker [Login|pull|push|search] local image management-docker [images|rmi| Tag|build|history|save|import] Other commands-docker [info|version]

container life cycle management

#  Run "echo" command in container, output "Hello word" $docker  run image_name echo  "Hello word" #   Interactively enter the container $docker run -i -t image_name /bin/bash#  install new programs in the container $docker run  image_name yum install -y app_name# enables the container to run docker run -d -i -t in the daemon state   image_name /bin/bash# enables a container to run some network applications in a specific port container at run time, allowing external access to these applications, either through  -P  or  -p   parameters to specify port mappings. When using the  -P  tag,,docker  randomly maps a  49000~49900  port to an internal container open network port. Docker run -d -p training/webapp python app.pydocker ps -lcontainer  ID  IMAGE                    COMMAND       CREATED         STATUS        PORTS           &nbsP;         namesbc533791f3f5  training/webapp:latest   python app.py 5 seconds ago  Up 2 seconds   0.0.0.0:49155->5000/tcp  nostalgic_morse-p (lowercase) specifies the port to be mapped, and only one container can be bound on a specified port. The supported formats are  ip:hostport:containerport | ip::containerport | hostport:containerport. Map all interface addresses using the  hostPort:containerPort  format local  5000  port mapped to the  5000  port of the container, you can perform docker  run -d -p 5000:5000 training/webapp python app.py The default is to bind all addresses on all interfaces at this time. The specified port mapped to the specified address can use the  ip:hostPort:containerPort  format to specify the mapping using a specific address, such as  localhost  address   127.0.0.1docker run -d -p 127.0.0.1:5000:5000 training/webapp python  app.py map to any port of the specified address use  ip::containerPort  bind  localhost  any port to the container's  5000  port, The local host is automatically assigned a port. docker run -d -p 127.0.0.1::5000 training/wEbapp python app.py can also use the  udp  tag to specify the  udp  port docker run -d -p  127.0.0.1:5000:5000/udp training/webapp python app.py view Map port configuration using  docker port  To view the currently mapped port configuration, you can also view the bound address docker port nostalgic_morse 5000127.0.0.1:49155. Note: The container has its own internal network and  IP   Address (use  docker inspect  to get all the variables,docker  can also have a variable network configuration.) -p  tags can be used multiple times to bind multiple ports such as docker run -d -p 5000:5000 -p 3000:80 training/ webapp python app.py# Start/Restart container and close container start a closed container: docker start container_id Restart a container:docker  restart container_id close a container: docker stop container_id or Docker kill container_ ID force close a container to delete a container: docker rm -f container_id Remove all containers:docker rm  ' docker ps -a  -q ' pause/Resume all processes for a container: docker pause/unpause

Operation and maintenance of containers

#查看容器运行状态docker  ps -a  #查看所有容器的状态docker  ps -a -q  #列出所有容器列表docker   inspect container_id  #检查镜像或者容器的参数, return  JSON  format by default. docker top container_id  #查询某个容器的进程运行情况docker  attach container_id # Access to a running Docker container (focus: Do not use ctrl+d or exit when exiting, this will cause the container to stop running, use ctrl+p when exiting, and then use Ctrl+q) docker events  --since=20151118  #查看直到指定日期容器的实时系统事件docker  logs CONTAINER_ID  #批量打印出容器中进程的运行日志docker  wait CONTAINER_ID  #阻塞一个容器 until the container stops (PS: I don't know how to use it at the moment) docker export container_id >  /directory/container.tar # (saves state information for a current container), import can use cat /directory/container.tar |docker  import - container_name:tagdocker save image_name >/directory/image_name.tar  # (Export an image), the difference between importing and using Docker load < /directory/image_name.tar is as follows: Export and import (exported-imported) The image will lose all history, and the image saved after reloading (saveed-loaded) does not have a layer of history lost. This means that you will not be able to roll back to the previous layer using the Export and import method (layER), the layer rollback can be done by persisting the entire mirror using the Save and reload mode (can execute docker tag <layer id> <image name> Roll back to the previous layer). docker port container_id  #列出某个容器和宿主机之间的端口映射关系

Container rootfs command

Docker commit [options] container [repository[:tag]]$ sudo docker psid                    image               command              CREATED              STATUS               portsc3f279d17e0a        ubuntu:12.04         /bin/bash            7 days ago          up 25  hours197387f1b436        ubuntu:12.04         /bin/bash           7 days ago           Up 25 hours$ docker commit c3f279d17e0a   SvenDowideit/testimage:version3f5283438590d$ docker images | headREPOSITORY                          TAG                  ID                   CREATED              VIRTUAL SIZESvenDowideit/testimage             version3            f5283438590d         16 seconds ago      335.7  MB Usage Note: The purpose of this command is to submit the modified container to the new image, and then export this imange to be distributed to other scenarios in debug use. The official Docker recommendation is that when you have finished debugging the image, you should write a new Dockerfile file to maintain the image. The commit command is only a secondary command that temporarily creates an image. Docker cp container:path hostpath Instructions: Use CP to copy the files from the container to the host. This command, in the case of developer application development, will require the need to copy the results of the running program, and this CP command can be used in this situation. Docker diff container Example: $ sudo docker diff 7bb0e258aefec /deva /dev/ Kmsgc /etca /etc/mtaba /goa /go/srca /go/src/github.coma /go/src/github.com/dotcloud .... Instructions for use: Diff lists List of file status changes (a - add, d - delete, c - change ) within 3 containers. The debug instructions needed to build the image.

Mirrored Warehouse

Docker [login|pull|push|search] First needs a place where the shared image is stored, and the private mirror warehouse can be used in the enterprise environment, but for convenience we use Docker's public repository directly. First you need to register a user in Docker hub and then use the  docker login  command to log in to the warehouse server. docker loginusername: xxxxxxpassword:email: [email protected]******.comlogin  Succeeded then we need to generate a local mirror using the  docker commit  command of the locally modified container. Note that since the mirror needs to be submitted to Docker hub, the name of the image must be prefixed with its own Docker hub user name, or it will be encountered in the  push  of the following  403  " Access denied: not allowed to create repo at given location "   error. For example, the name is  linfan/employees. docker commit node-app linfan/ employeesa4281aa8baf9aee1173509b30b26b17fd1bb2de62d4d90fa31b86779dd15109bdocker imagesrepository   TAG  IMAGE ID  CREATED  VIRTUAL SIZElinfan/employees   LATEST&NBSP;&NBSP;A4281AA8BAF9&NBSP;&NBSP;14&NBSP;SECONDS&NBSP;AGO&NBSP;&NBSP;696.2&NBSP;MB Finally, use  docker  push  command to submit this prepared image to docker hIn the UB warehouse. docker push linfan/employeesthe push refers to a repository [linfan/ employees]  (len: 1) sending image list ... pushing tag for rev [5577d6743652] on {https://cdn-registry-1.docker.io/v1/ Repositories/linfan/employees/tags/latest} After the commit is completed, the image can be obtained by using the  docker pull  command on the other nodes. Note: Strictly speaking, the method of exposing the database service container through Docker link to the Application service container does not conform to the 12 guidelines for distributed applications, because two containers connected by Docker link must be running on the same physical host. Data and applications cannot be deployed individually or horizontally in a cluster. How to use: Docker search term: Search for shared image by keyword.

Local image Management

Docker images #列出所有的镜像docker rmi Image [Image ...] #删除镜像 docker tag [OPTIONS] Image[:tag] [registryhost/][username/]name[: TAG]
Instructions for use: Combine user names, image names, tag names to organize and manage the image. Docker build-t edwardsbean/centos6-jdk1.7. Dockerfile is used to create a custom image that contains user-specified software dependencies, and so on. The current directory contains Dockerfile, use command build to create a new image, and name it edwardsbean/centos6-jdk1.7: (Dockerfile Writing is important, you need to focus on learning) Docker history Image #列出某个镜像之前做过的操作docker Save Image_name >/directory/image_name.tar # (Export an image), import can use Docker load </directory/ Image_name.tardocker export container_id >/directory/container.tar # (Save status information for a current container), import to use cat/directory/ Container.tar |docker Import-container_name:tag

Other commands

Docker [info|version]docker Info #结合docker vesion, you can use this command at any time to provide local configuration information, convenient for Docker developers to quickly locate the problem. Docker version #显示Docker的版本号, API release number, Git commit, the go version number of the Docker client and background process.


This article is from the "Lemon" blog, be sure to keep this source http://xianglinhu.blog.51cto.com/5787032/1717512

Docker Usage Basics Summary

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.