About Docker's 15 tips (Tips) _docker

Source: Internet
Author: User
Tags commit json docker ps docker run in python

1. Get the ID of the most recently run container

This is an operation that we often use, as the official example allows you to do this (environment Ubuntu):

$ id=$ (Docker run Ubuntu echo Hello World)

Hello World

$ docker commit $ID HelloWorld

fd08a884dc79

This is useful when writing scripts, such as you want to bulk get IDs in a script, and then do it further. But this way requires that you have to assign a value to the ID, and if it's a direct knock on the command, it's not very convenient. At this point, you can change one way:

$ alias dl= ' Docker ps-l-Q '

$ docker run Ubuntu echo Hello World

Hello World

$ dl

1904cf045887

$ doc Ker commit ' dl ' HelloWorld

fd08a884dc79

The Docker ps-l-q command returns the ID of the most recently running container, by setting the alias (alias), the DL command is the ID of the nearest container. This eliminates the need to enter the lengthy Docker ps-l-q command. With two diagonal quotes, you can get the value of the DL command, which is the ID of the most recently running container.

2. As far as possible in the Dockerfile to specify the software to install, without the Docker container shell directly install the software

To be honest, I also sometimes like to install software in the shell, and maybe you like to have all the software installed in the shell. But, in the end, it turns out that you still need to specify the installation file in Doockerfile. To install the software in the shell, you do this:

$ docker run-i-t ubuntu bash #登陆到docker容器

root@db0c3967abf8:/#

Then enter the following command to install the file:

 
 

And then call exit:

root@db0c3978abf8:/# exit

Exit the Docker container, and then pass a complex JSON string to the Docker commit command to commit the new mirror:

$ docker commit-run= "{CMD": ["Postgres", "-too-many-opts"]} "' DL ' Postgres

It's too much trouble, isn't it? or specify the installation file in Dockerfile, just two steps:

1. In a small dockerfile, specify the parameters of the current operation mirroring as the FROM command

2. Then specify some Docker commands in the dockerfile, such as CMD, enterpoint, volume, etc. to specify the installed software

3. Super-super-super User

You may need to always use Superuser to manipulate Docker, as you have been prompted in earlier examples:

# Add Docker user group

$ sudo groupadd docker

# Add yourself to Docker user group

$ sudo gpasswd-a myusername

# docker # restart Docker background service

$ sudo service docker Restart

# Log off and then log on

$ exit

wow! Three consecutive sudo! Three times incarnation "Super User", really is "super-super-Super user" Ah! Don't worry, set it up, and you'll never have to play that much sudo again!

4. Cleaning up rubbish

If you want to delete all containers that stop running, use this command:

$ docker RM $ (Docker ps-a-Q)

By the way, Docker PS command is very slow, do not know why so slow, logically speaking go language is very fast ah. The Docker ps-a-q command lists the IDs of all containers, and then deletes the containers by ID. The Docker RM command will fail when it encounters a running container, so this command perfectly removes all containers that are not running.

5. Docker inspect output results of the analytical tool: JQ

To filter the output of the Docker inspect, in general, with the grep command, you need to do this:

 
 

Oh! Looks very complex, with JQ bar, professional analytical docker inspect output results, with more readability, easy to use:

 
 

One of the first '. ' Represents all the results. ' [0] ' represents the first element of the array. Just as JavaScript accesses a JSON object, it's simple and convenient.

6. What are the environment variables for mirroring?

Sometimes, you need to know what environment variables you have created in your mirror. Simple! Just like this:

 
 

The output results are as follows:

home=/

path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

container=lxc

HOSTNAME= 5e1560b7f757

Call env to view environment variables, which are useful for the "links" that follow, and use these environment variables when connecting two containers, see the last key point, "link".-link

7.RUN Command vs cmd command

Docker novice users are more likely to confuse run and cmd two commands.

The Run command executes when building (build) Docker, at which point the cmd command is not executed. The cmd command executes when the Run command executes. Let's clarify the relationship, assuming the dockerfile content is as follows:

From Thelanddownunder

Maintainer Crocdundee

We want to install some software to the system, then:

# Docker build will execute the following command:

run apt-get update

run apt-get install softwares

# Dokcer run defaults to the following command:

CMD [" Softwares "] 

Executes CMD when executing run,run on build, that is, CMD is the last command to mirror the final execution.

8.CMD Command vs entrypoint command

Another two easy to confuse command! We will not say the details, for example, assume that the dockerfile of a container specifies the cmd command, as follows:

From Ubuntu

CMD ["echo"] 

The dockerfile of another container specifies the entrypoint command, as follows:

From Ubuntu

entrypoint ["echo"] 

To run the first container:

Docker run Image1 Echo Hello

The results obtained:

Hello

To run a second container:

Docker Run Image2 echo Hello

The results obtained:

echo Hello

Do you see the difference? In fact, the cmd command is covered, and the command that is entered after Docker run is matched with the command specified by CMD, replacing the command specified by CMD with the command in Docker run. The command specified by entrypoint is only a "portal", and the content behind Docker run is passed to the "entry" instead of the command replacement, so the result is "echo Hello".

Does the 9.Docker container have its own IP address?

People who have just come in contact with Docker may have the question: Does the Docker container have its own IP address? Docker container is a process? Or is it a virtual machine? Well... Maybe both? Haha, in fact, the Docker container does have its own IP, just like a process with IP. Just do the command to view the IP in both the host and the Docker container separately.

To view the IP of a host:

 
 

Get the result:

2:eth0 inet 162.243.139.222/24

To view the IP of the Docker container:

$ docker run Ubuntu ip-r-O addr Show eth0

Get the result:

149:eth0 inet 172.17.0.43/16

The two are not the same, indicating that the Docker container has its own IP.

10. Based on the command line thin client, communication using the rest interface of the UNIX socket and Docker backend service

Docker the default is to communicate with UNIX sockets, until about 0.5, 0.6 of the version or the port to communicate, but now changed to a UNIX socket, so from the outside can not control the internal details of the Docker container. So here's the fun thing to link to the Docker UNIX socket from the host:

# Connect to UNIX sockets as HTTP clients

$ nc-u//var/run/docker.sock 

When the connection is successful, enter:

 
 

When you enter, you knock two carriage returns, and the second carriage return indicates that the input is finished. Then, the result should be:

http/1.1 OK

Content-type:application/json

date:tue, Nov 2013 23:18:09 GMT transfer-encoding

: chunked

16aa

[{"Repository": "Postgres", "Tag": "...

One day, I accidentally put the name of the submission is wrong, the beginning of the name of "-xxx" (I have mixed the order and options), so when I deleted the problem, Docker rm-xxx, will be-xxx as a parameter rather than the name of the mirror. So I have to connect to the container directly through the socket to call rest server to erase the wrong things.

11. Mapping the dependencies of mirrors into graphs

Docker Images command has a very windy option:-viz, you can map the dependency of the mirror graph and save it to the picture file through the pipe symbol:

# Generate a graph of dependencies

$ docker Images-viz | dot-t png-o docker.png

In this way, a PNG map is generated under the host's current path, and then a miniature HTTP server is opened in Python:

Python-m Simplehttpserver

Then open it in a browser on another machine: Yun_qi_img/docker.png

OK, the dependency relationship at a glance!

(Translator Note: To use the dot command, the host will install the Graphviz package.) In addition, if the host IP does not bind the domain name, MachineName replaced by the host IP. )

Where did 12.Docker keep everything?

Docker actually put everything under the/var/lib/docker path. Switch to Super User, look under the/var/lib/docker, you can learn a lot of interesting things. Execute the following command:

$ sudo su

# cd/var/lib/docker

# ls-f

containers/graph/repositories volumes/

You can see a lot of directories, the containers directory is of course the storage container (container), the graph directory to store mirrors, the file layer (Files system layer) stored in the Graph/imageid/layer path, So you can see what's in the file layer, and using this hierarchy makes it clear how the layers of the file stack up on top of each other.

13.Docker source code: Go, go, go, golang!

Docker's source code is all written in the go language. Go is a very cool language. In fact, not only Docker, a lot of good software is written with go. For me, there are 4 of Docker source files that I like to read very much:

Commands.go

The Docker command-line interface is a lightweight encapsulation of the rest API. The Docker team does not want logic in the command, so Commands.go simply sends instructions to the rest API to ensure its smaller granularity.

Api.go

The REST API routing (accepts requests in commands.go, forwarding to Server.go)

Server.go

Implementation of most rest APIs

Buildfile.go

Dockerfile Parser

Some guys Marvel "wow! How did the Docker come true?! I can't understand! "Never mind, Docker is open source software, to see its source code on it." If you're not sure what's going on in the Dockerfile, go straight to buildfile.go and see.

14. Run several Docker background programs, and then exit the container, what will happen?

OK, the penultimate point. What happens if you run several background programs in Docker and then exit the Docker container? The answer is: don't do that! Because the background program is all lost.

In Dockerfile, use the Run command to open a background program, such as:

RUN Pg_ctl Start

In this case, the background program that the Run command opens will be lost. Call the container's bash to the container's shell:

$ docker run-i-T Postgresimage bash

Then call PS aux to view the process and you will find that the Postgres process does not run. The Run command affects the file system. Therefore, do not use Dockerfile to start the background program, the background program to start into the foreground process. Or, as some gurus suggest, write a startup script that launches these daemon programs or processes in a script.

15. Friendly communication between containers: links

This is the most pull the wind function! I'll leave it to the finale! This is the most important new feature in 0.6.5, which we have mentioned twice before. Run a container, give it a name, in the example below, we specify the name "Loldb" to the container through the-name parameter:

$ docker run-d-name loldb loldbimage

Run another container, plus the-link parameter to connect to the first container (alias Loldb) and assign an alias to the second container (here is Cheez):

$ docker Run-link/loldb:cheez Otherimage env

By the way, get the CHEEZ environment variable:

cheez_port=tcp://172.17.0.8:6379

cheez_port_1337_tcp=tcp://172.17.0.8.6379

cheez_port_1337_tcp_addr= tcp://172.17.0.12

cheez_port_1337_tcp_port=6379

cheez_port_1337_tcp_proto=tcp

So we built a network channel (bridge) between two containers, so we could build a rails-like program: A container can access the database container without exposing other interfaces. Very cool! The database container only needs to know the alias of the first container (in this case, Cheez) and the port number to open. So the database container can also have the ENV command to see if the port is open.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.