1. Get the ID of the most recently run container
This is an operation that we often use, according to the official example, you can do this (environment Ubuntu):
$ id=$ (Docker run Ubuntu echo Hello World)
Hello World
$ docker Commit $ID HelloWorld
Fd08a884dc79
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:
$ alias dl= ' Docker ps-l-Q '
$ docker run Ubuntu echo Hello World
Hello World
$ DL
1904cf045887
$ docker commit ' DL ' HelloWorld
Fd08a884dc79
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.
2. Try to specify the software to install in the Dockerfile, without 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:
$ docker run-i-T Ubuntu bash #登陆到docker容器
[Email protected]:/#
Then enter the following command to install the file:
Apt-get Install PostgreSQL
And then call exit:
[Email protected]:/# exit
Exit the Docker container, and then pass a complex JSON string to the Docker commit command to commit the new image:
$ 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, 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
3. Super-super-super User
You may need to always use the superuser to operate Docker, as you have always indicated in the early examples:
# Add a Docker user group
$ sudo groupadd Docker
# Add yourself to the Docker user group
$ sudo gpasswd-a myusername Docker
# Restart Docker Backend Service
$ sudo service docker restart
# Log out and then log in
$ exit
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!
4. Cleaning up rubbish
If you want to delete all the 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, 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.
5. Docker inspect output analysis weapon: JQ
To filter the output of the Docker inspect, in general, with the grep command, you need to do the following:
$docker inspect ' dl ' | grep IPAddress | Cut-d ' "'-F 4 172.17.0.52
Oh! Looks very complex, with JQ bar, professional analysis Docker inspect output results, with greater readability, easy to use:
$docker inspect ' dl ' | Jq-r '. [0]. Networksettings.ipaddress ' 172.17.0.52
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 are in the image you are creating. Simple! As long as this:
$ Docker Run Ubuntu env
The output results are as follows:
home=/
Path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Container=lxc
hostname=5e1560b7f757
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".
7.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:
From Thelanddownunder
Maintainer Crocdundee
We want to install some software into the system, then:
# Docker build will execute the following command:
RUN apt-get Update
RUN Apt-get Install Softwares
# Dokcer Run executes the following command by default:
CMD ["Softwares"]
Build executes CMD when executing run,run, that is, CMD is the final command that the mirror executes.
8.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:
From Ubuntu
CMD ["echo"]
The dockerfile of the other container specifies the entrypoint command, as follows:
From Ubuntu
entrypoint ["echo"]
Run the first container:
Docker run Image1 Echo Hello
The results obtained:
Hello
Run a second container:
Docker Run Image2 echo Hello
The results obtained:
echo Hello
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".
Does the 9.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:
$ ip-4-O addr Show eth0
Get results:
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 results:
149:eth0 inet 172.17.0.43/16
The two are not the same, indicating that the Docker container has its own IP.
10. Command line-based thin client that communicates using the rest interface of the UNIX socket and the Docker backend 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:
# Connect to a UNIX socket like an HTTP client
$ nc-u//var/run/docker.sock
After the connection is successful, enter:
Get/images/json http/1.1
After entering the two return, the second return indicates the end of the input. Then, the result should be:
http/1.1 OK
Content-type:application/json
Date:tue, 23:18:09 GMT
Transfer-encoding:chunked
16aa
[{"Repository": "Postgres", "Tag": "...
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.
11. Map the image dependencies
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:
# Generate a graph of dependencies
$ Docker Images-viz | Dot-t Png-o Docker.png
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:
Python-m Simplehttpserver
Then open it with a browser on another machine:
Http://machinename:8000/docker.png
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. )
Where did 12.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:
$ sudo su
# Cd/var/lib/docker
# ls-f
Containers/graph/repositories volumes/
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.
13.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
Implementation of most rest APIs
Buildfile.go
Parser for 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.
14. 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:
RUN Pg_ctl Start
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:
$ 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 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.
15. Friendly communication between the 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:
$ docker run-d-name loldb loldbimage
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):
$ docker Run-link/loldb:cheez Otherimage env
By the way, get Cheez environment variables:
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
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.
15 Small tip for Docker