How to easily play with Docker? With these 15 tips, it's OK.

Source: Internet
Author: User
Tags docker ps docker run

Tip1

Get the ID of the most recently run container this is an operation that we will often use, according to the official example, you can do this (environment Ubuntu):

This is useful when scripting, such as when you want to get IDs in bulk in a script, and then proceed further. But this approach requires you to assign a value to the ID, which is inconvenient if you are directly knocking at the command. At this point, you can change the way:

The Docker ps-l-q command returns the ID of the most recently run container, and by setting the alias, the DL command gets the ID of the most recent 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 run container.

Tip2

Try to specify the software that you want to install in Dockerfile instead of installing the software directly from the shell of the Docker container. To tell you the truth, I sometimes like to install software in the shell, and maybe you like to have all the software installed in the shell. However, in the end, we found that you still need to specify the installation files in the Doockerfile. To install the software in the shell, you do this:

Then enter the following command to install the file:

And then call exit:

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

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

1. In a small dockerfile, specify the image of the current operation as a parameter of the FROM command

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

Tip3

Super-super-super User

You may need to always use the superuser to operate Docker, as you have always indicated in the early examples:

wow! Three consecutive sudo! Three times Avatar "Super User", it is "super-super-Super user" Ah! Don't worry, Setup is over, you'll never have to hit that much sudo again!

Tip4

Cleaning up Trash

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

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

Tip5

Docker inspect output results: 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 analysis Docker inspect output results, with greater 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.

Tip6

What are the environment variables for mirroring? Sometimes you need to know what environment variables are in the image you are creating. Simple! As long as this:

The output results are as follows:

Calling env to view environment variables is useful for the following "links" (-link), which need to be used when connecting two containers, see the last point "link".

Tip7

Run command vs cmd command

The novice user of Docker is more likely to confuse the two commands of run and CMD. The Run command executes at build Docker when 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:

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

Build executes CMD when executing run,run, that is, CMD is the final command that the mirror executes.

Tip8

cmd command vs entrypoint command

It's another two confusing orders! We won't talk about specifics, for example, suppose a container's dockerfile specifies a cmd command, as follows:

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

Run the first container:

The results obtained:

Run a second container:

The results obtained:

You see the difference? In fact, the cmd command is overwritten, and when the command entered after the Docker run matches the command specified by cmd, the command specified by CMD is replaced with the command in the Docker run. The command specified by entrypoint is just a "portal", and the content behind the Docker run is passed to the "portal" instead of the command, so the result is "echo Hello".

Tip9

Does the Docker container have its own IP address?

People who have just contacted Docker may have this question: Does the Docker container have its own IP address? is a docker container a process? Or a virtual machine? Well... Maybe both? Haha, actually, the Docker container does have its own IP, just like a process with IP. As long as the command to view the IP is performed separately in the host and Docker containers.

To view the IP of the host:

Get results:

To view the IP of the Docker container:

Get results:

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

Tip10

A thin client based on the command line that communicates using the rest interface of the UNIX socket and the Docker background service. Docker communicates with a UNIX socket by default, up to about 0.5, 0.6, or port, but now it's changed to a UNIX socket, so the inside details of the Docker container cannot be controlled externally. Let's do something interesting, from a host to a UNIX socket that's linked to Docker:

After the connection is successful, enter:

After entering the two return, the second return indicates the end of the input. Then, the result should be:

One day, I accidentally misspelled the name of the file, starting with the name "-xxx" (I confused the order and the options), so when I deleted the problem, Docker rm-xxx,-xxx as a parameter instead of the name of the mirror. So I had to just connect to the container through the socket to call rest server and delete the wrong thing.

Tip11

Mapping the dependency of a mirror into a diagram

The Docker images command has a very windy option:-viz, you can map the image dependencies and save them to the picture file by the pipe symbol:

In this way, a PNG graph is generated under the current path of the host, and then a mini HTTP server is opened with Python:

Then open it with a browser on another machine:

OK, the dependency relationship at a glance!

(Translator Note: To use the dot command, the host will install the Graphviz package.) Also, if the host IP does not have a domain name, MachineName is replaced by the host's IP. )

Tip12

Where did Docker save everything? Docker actually puts everything under the/var/lib/docker path. Switch to super users, and look at/var/lib/docker, you can learn a lot of interesting things. Execute the following command:

Can see a lot of directories, containers directory is of course the storage container (container), the graph directory storage image, the file layer (Files system layer) stored in the Graph/imageid/layer path, This way you can see exactly what is in the file layer and use this hierarchy to clearly see how the layers of the file layer stack up.

Tip13

Docker source code: Go, go, go, golang! The source code for Docker 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 the Docker source files that I really like to read:

Commands.go Docker's command-line interface is a lightweight package for rest APIs. The Docker team does not want logic in the command, so commands.go just sends instructions to the rest API to ensure its smaller granularity.

Api.go REST API Routing (accepts requests in commands.go, forwards to Server.go)

Server.go the implementation of most rest APIs

Parser for Buildfile.go Dockerfile

Some guys Marvel "wow! How is Docker implemented?! I can't understand! "It doesn't matter, Docker is open source software, to see its source code is OK." If you're not sure what's going on in the Dockerfile, go straight to the buildfile.go and see.

Tip14

What happens when you run several Docker daemons and then exit the container? OK, the second point to the bottom. What happens if you run several background programs in Docker and then exit the Docker container? The answer is: don't do this! Because the background program will be lost all.

Use the Run command in Dockerfile to open a background program such as:

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

Then call PS aux to view the process and you will find that the Postgres process does not run up. The Run command affects the file system. Therefore, do not dockerfile with the start daemon, to the background program to start the foreground process. Or, as some experts suggest, write a startup script that starts these daemons or processes in the script.

Tip15

Friendly communication between containers: links

This is the feature of the most pull-out wind! I'll leave it to the end of 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 give the container the name "Loldb" by the-name parameter:

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

By the way, get Cheez environment variables:

In this way, we set up a network channel (bridge) between two containers, so that we can build a rails-like program: A container can access the database container without exposing other interfaces to the outside. 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 view the ENV command to see if the port is open.

It is recommended that you join the Java Architecture Advanced Development Group:619841960  Group in Java Engineering, high-performance and distributed, high-performance, in layman's order. High architecture. Performance tuning, Spring,mybatis,netty source analysis and big data, and many other knowledge points free live sharing     You can participate in   and are targeted at the work of the open development crowd

Related Article

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.