Docker Special (2): Docker common management Commands (top)

Source: Internet
Author: User
Tags docker ps docker hub docker run docker registry

http://segmentfault.com/a/1190000000751601

This article only records the use of Docker commands in most situations, and if you want to know the details of each option, please refer to the official documentation, which is recorded only as a later memo.

According to their own understanding, generally divided into the following types:

    • Container Life cycle Management-docker [run|start|stop|restart|kill|rm|pause|unpause]
    • Operation and maintenance of containers-docker [ps|inspect|top|attach|events|logs|wait|export|port]
    • Container ROOTFS Command-docker [commit|cp|diff]
    • Mirrored Warehouse-docker [login|pull|push|search]
    • Local image Management-docker [images|rmi|tag|build|history|save|import]
    • Other Commands-docker [info|version]

Look at a change chart

1. List the mirrors on the machine (images)
# docker images REPOSITORY               TAG             IMAGE ID        CREATED         VIRTUAL SIZEubuntu                   14.10           2185fd50e2ca    13 days ago 236.9 MB…

Where we can determine which server this image is from, and if not/it represents an official image, similar to the username/repos_name personal Public Library of GitHub, similar to Repository regsistory.example.com:5000/repos_name .
The IMAGE ID column is actually an abbreviation, with the option to display the complete --no-trunc

2. Search for image in Docker index

Usage: docker search TERM

# docker search seanloNAME                DESCRIPTION           STARS     OFFICIAL   AUTOMATEDseanloook/centos6   sean‘s docker repos         0

The scope of the search is the official image and all personal public images. The name column is followed by the warehouse.

3. Drop-down image or Repository (pull) from Docker Registry server

Usage: docker pull [OPTIONS] NAME[:TAG]

# docker pull centos

The above commands need to be noted that all images from the official image of the CentOS repository are downloaded before the Docker v1.2 version, and the instructions in the official document from V.13 are changed: will pull the centos:latest image Intermediate layers and any aliases of the same ID, that is, only the image tagged as latest (and other tags of the same images ID) will be downloaded.
You can also explicitly specify a specific image:

# docker pull centos:centos6

Of course, it can also be pulled from a person's public warehouse (including a private warehouse), such as docker pull username/repository<:tag_name> :

# docker pull seanlook/centos:centos6

If you do not have a network, or get a mirror from anotherdocker pull registry.domain.com:5000/repos:<tag_name>

# docker pull dl.dockerpool.com:5000/mongo:latest
4. Push an image or repository to registry (push)

Corresponding to the pull above, it can be pushed to the public, private, and public of the Docker hub, but cannot be pushed to top level Repository.

# docker push seanlook/mongo# docker push registry.tp-link.net:5000/mongo:2014-10-27

Registry.tp-link.net can also be written as ip,172.29.88.222.
In the case where the repository does not exist, the push on the command line will be created as a private library, but the default is public library created by the browser.

5. Starting a container (run) from image

docker runThe command first creates a writable container from a specific image creation, and then starts it with the start command. The stopped container can be restarted and the original modifications are preserved. There are a number of startup parameters for the Run command, here are some general instructions for use, see http://www.cnphp6.com/archives/24899 for more information
When you use Docker run to create a container, the standard operations that Docker runs in the background include:

    • Checks if there is a specified image locally and does not exist to download from the public repository
    • Create and start a container with mirroring
    • Assign a file system and mount a layer of read-write layers outside the mirrored layer
    • Bridging a virtual interface into a container from the bridge interface configured by the host host
    • Configure an IP address from the address pool to the container
    • Executing user-specified applications
    • Container is terminated when execution is complete

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

5.1 Use image to create a container and execute the appropriate command, and then stop
# docker run ubuntu echo "hello world"hello word

This is the simplest way to almost feel no difference with local direct execution echo ‘hello world‘ , and actually it will boot from the local ubuntu:latest image into a container and exit (viewable) after executing the print command docker ps -l . It is important to note that there is a default --rm=true parameter that stops the container and removes it from the file system after the operation is complete. Because Docker containers are too lightweight, many times users are deleting and creating new containers at any time.
Once the container is started, it automatically generates a random CONTAINER ID ID, which can be changed after the commit command.IMAGE ID

Using image to create container and enter interactive mode, login shell is/bin/bash
# docker run -i -t --name mytest centos:centos6 /bin/bashbash-4.1#

The above --name parameter can specify the container name after launch, and if not specified, Docker will take a name for us. Mirroring centos:centos6 can also be used IMAGE ID (68edf809afe7), and a pseudo terminal will be started, but with the PS or top command we can only see one or two processes, because the core of the container is the application being executed, and the resources required are required for the application to run, in addition to There are no other resources, so Docker is highly utilized for resources. When you exit with Exit or CTRL+D, the container disappears (the missing container is not completely deleted?). )
(so the number of different tags and the image ID of the same image will run with which tag to start it?)

5.2 Run out a container and put it in the background to run
do echo hello world; sleep 2; done"ae60c4b642058fefcc61ada85a610914bed9f5df0e2aa147100eab85cea785dc

It will directly suspend the boot container in the background (this is called SaaS), and will output one CONTAINER ID , through the docker ps information can be seen in this container, you can view its output outside of container docker logs ae60c4b64205 , or through docker attach ae60c4b64205 Connect to this running terminal, at this point in the Ctrl+C exit container disappeared, press Ctrl-p Ctrl-q can exit to the host, and keep container still running
Also, if-D starts but the subsequent command finishes executing, such as, /bin/bash the echo test container will still terminate when done. And-D cannot be used in conjunction with--RM
You can run memcached, Apache, and so on in this way.

5.3 Mapping the host to container ports and directories

It is useful to map a host to a container's port, such as running memcached in container, Port 11211, and the host that is running the container to connect to container internel_ip:11211 access. If you have access to memcached requirements from other hosts then you can use the-P option, which is like -P , which has the following kinds of wording:

  -p 11211:11211 this is the default, bind the host all network cards (0 .0.0.0) 11211 ports to 11211 ports on the container -p 127.0.0.1:11211: 11,211 bind only localhost 11211 ports of this interface -p 127.0.0.1::5000-p 127.0.0< Span class= "Hljs-class" >.1:80:8080     

Directory mapping is actually the "bind mount" host path to the container directory, which is convenient for internal and external transfer of files, in the construction of the section, in order to avoid the container stop after the save images is not deleted, It is necessary to save the submitted images to the mounted host directory. It's easier to use, and -v add more when you bind multiple directories -v .

-v /tmp/docker:/tmp/docker

There is also a connection between two container available --link , see the Advanced section or official documentation.
Here is an example:

# docker run --name nginx_test > -v /tmp/docker:/usr/share/nginx/html:ro > -p 80:80 -d > nginx:1.7.6

Index.html is established under the/tmp/docker of the host, it can be passed http://localhost:80/ or http://host-ip:80 accessed.

6. Solid a container into a new image (commit)

When we make our own image, we will install some tools in the container, modify the configuration, and if we do not save the commit, then container stop and then start again, the changes will disappear.
Repo:tag Optional
after Docker commit <container> [Repo:tag]

can only commit container that are running, through the Docker PS visible container,

  view just-running containers # Docker ps-lcontainer ID IMAGE COMMAND creat   ED STATUS PORTS namesc9fdf26326c9 nginx:1 nginx-g.     3 hours ago Exited (0). Nginx_test start an existing container (run is started after creating a new container from image), the following can also use Docker start Nginx_test replace [[email protected] Docker]# Docker start C9fdf26326c9c9fdf26326c9docker run-i-T--sig-proxy=false  21ffe545748baf/bin/bashnginx service does not start # Docker commit-m "Some tools installed" Fcbd0a5348ca seanlook/ubuntu:14.10_tutorialfe022762070b09866eaab47bc943ccb796e53f3f416abf3f2327481b446a9503   

-a "[email protected]"
Please note that when you repeatedly go to commit a container, each time you will get a new one IMAGE ID , if the latter is repository:tag not changed, through docker images can see, the previously submitted copy of the image repository:tag will become <none>:<none> , so try to avoid repeated submissions.
In addition, observe the following points:

    • Commit container only pause the container, which is to ensure consistency of the container file system, but does not stop. If you want to continue making other changes to this container:
      • You can resubmit to get the new image2, delete the new image1
      • You can also close the container to start with a new image1, continue to modify, submit Image2 Delete Image1
      • Of course this will be very painful, so it is generally used Dockerfile to build get the final image, reference []
    • Although a new image is generated and you can see 100MB in size, it is very quick to know from the commit process that it does not take up 100MB of hard disk space independently, but only modifies it on the basis of the old mirror, which shares most of the common "slices".

Continue below: Docker Special (2): Docker Common management commands (bottom)

Reference
      • Official Command Line Reference
      • Docker Chinese guide Cli-widuu translation
      • docker--from getting started to practicing
      • Docker Foundation and advanced

Docker Special (2): Docker common management Commands (top)

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.