Tip1
Get the id of the most recently run container. This is an operation we often use. As the official example, you can do this (environment Ubuntu) :
This is useful when writing scripts where you want to batch fetch ids in the script and move on. However, this method requires you to assign a value to the ID, which is inconvenient if you type the command directly. In this case, you can take a different approach:
Docker ps -l -q command will return the id of the most recently run container, by setting the alias (alias), dl command is to get the ID of the most recently run container. This eliminates the need to type the lengthy Docker ps-l-q command. You get the value of the dl command, which is the ID of the most recently run container, with two oblique quotes “.
Tip2
Try to specify the software to be installed in the Dockerfile instead of using the shell of the Docker container to install the software directly. To be honest, I sometimes like to install software in a shell, and you probably like to install everything in a shell, too. However, it turns out that you still need to specify the installation file in the Doockerfile. To install software in a shell, you do this:
Then enter the following command to install the file:
Then call exit:
Exit the docker container and pass a complex JSON string to the Docker commit command to commit the new image:
It’s too much trouble, isn’t it? Instead, specify the installation file in the Dockerfile in two steps:
1. In a small Dockerfile, specify the image of the current operation as an argument to the FROM command
2. Then specify some Docker commands in the Dockerfile, such as CMD, ENTERPOINT, VOLUME, etc., to specify the installed software
Tip3
Super – super – super user
You may need to use Docker as a superuser all the time, as the earlier examples have always suggested:
Wow!!! Three sudos in a row! Three incarnations of “super user”, is really “super – super – super user” ah! Don’t worry, you’ll never have to hit so many sudos again!
Tip4
Clearing up the rubbish
If you want to delete all stopped containers, use this command:
By the way, docker ps command is very slow. I don’t know why it is so slow. Go is supposed to be fast. Docker ps -a -q lists the ids of all containers and then deletes containers based on their ids. Docker rm command will be invalid when it encounters running containers, so this command perfectly deletes all containers that are not running.
Tip5
Docker inspect output results of jQ to filter docker inspect output results, in general, with grep command, you need to operate like this:
Oh! Docker inspect looks very complex, with JQ bar, professional parsing docker inspect output results, with stronger readability, easy to use:
The first ‘. ‘represents all the results. ‘[0]’ represents the first element of the array. Just like JavaScript accessing a JSON object, it’s easy and convenient.
Tip6
What environment variables does a mirror have? Sometimes you need to know what environment variables the image you are creating has. Simple! As long as this:
The following output is displayed:
Calling env to look at environment variables is useful for “-links”, which are needed to connect two containers, as described in the last point, “links”.
Tip7
RUN command vs CMD command
New Docker users tend to confuse the RUN and CMD commands. RUN is executed when Docker is built, CMD is not executed. The CMD command is executed only when the RUN command is executed. To clear things up, assume the Dockerfile content is as follows:
We need to install some software into the system, so:
RUN is executed when Build, CMD is executed when RUN, that is, CMD is the final command executed by the image.
Tip8
CMD vs ENTRYPOINT
Two more confusing commands! For example, suppose a container Dockerfile specifies a CMD command as follows:
The Dockerfile of another container specifies the ENTRYPOINT command as follows:
Run the first container:
Results obtained:
Run the second container:
Results obtained:
See the difference? In fact, CMD commands are overwritable. If the command entered after docker run matches the command specified by CMD, the command specified by CMD will be replaced by the command contained in docker run. The command specified by ENTRYPOINT is just an “entry”, and the content after the Docker run is passed to this “entry” instead of command replacement, so the result is “echo Hello”.
Tip9
Does the Docker container have its own IP address?
Those who are new to Docker may wonder: Does Docker container have its own IP address? Docker container is a process? Or a virtual machine? B: well… Maybe both? In fact, Docker container does have its own IP, just like a process with IP. Just execute the command to view the IP on the host and in the Docker container.
Query the HOST IP address:
Results obtained:
Check Docker container IP:
Results obtained:
The difference between the two indicates that the Docker container has its own IP.
Tip10
Command line based thin client, using UNIX Socket and Docker backend service REST interface for communication. Docker uses UNIX sockets to communicate by default. Up to about version 0.5 or 0.6, Docker still uses ports to communicate, but now it has changed to UNIX sockets, so the internal details of Docker containers cannot be controlled from the outside. Now let’s do something interesting, from the host link to the Docker UNIX socket:
After the connection is successful, enter:
After entering, press enter twice. The second press enter indicates the end of entering. Then, the result should be:
One day, I accidentally mistyped the submitted name and it started with “-xxx” (I confused the order of commands and options), so when I deleted it there was a problem, docker rm -xxx, would take -xxx as a parameter instead of the name of the image. So I had to connect directly to the container through the socket to call REST Server to delete the error.
Tip11
Graph the dependencies of mirror images
The Docker images command has a cool option: -viz, which allows you to plot image dependencies and save them to an image file using pipe symbols:
Thus, a PNG image is generated under the current path of the host, and then a tiny HTTP server is opened in Python:
Then open it in a browser on another machine:
OK, dependencies are clear!
To use the dot command, the host needs to install the Graphviz package. Alternatively, if the host IP is not bound to a domain name, machinename is the host IP.
Tip12
Where does Docker keep all his stuff? Docker actually puts everything in /var/lib/docker. Switch to super and go to /var/lib/docker. You can learn a lot of interesting things. Execute the following command:
The containers directory is for containers, the graph directory is for images, the file system layer is for graph/imageid/layer, This allows you to see what’s in the file layer, and use this hierarchy to see exactly how the file layers are stacked on top of each other.
Tip13
Docker source code: Go, Go, Go, Golang! Docker source code is all written in Go language. Go is a really cool language. In fact, not only Docker, many excellent software is written with Go. For me, there are four Docker source files that I really like to read:
Commands. go Docker’s command line interface is a lightweight wrapper to the REST API. The Docker team doesn’t want logic in commands, so commands.go just sends instructions to the REST API to keep it small and granular.
API. Go REST API routing (accepts requests in commands.go and forwards to server.go)
Server. go most REST API implementations
Buildfile.go Parser for Dockerfile
Some guys exclaim, “Wow! How is Docker implemented? ! I don’t understand!” It doesn’t matter, Docker is open source software, just look at its source code. If you’re not sure what’s going on in Dockerfile, just look at buildfile.go.
Tip14
What happens when you run a few Docker daemons and exit the container? OK, second to last point. What happens if you run several background programs in a Docker and then exit the Docker container? The answer is: don’t! Because if you do that, you lose all the background programs.
Dockerfile uses the RUN command to start a daemon, such as:
In this case, the daemon started by the RUN command is lost. Calling the container’s bash connects to the container’s shell:
Then call ps aux to look at the process, and you’ll see that Postgres’s process is not running. The RUN command affects the file system. Therefore, instead of starting the daemon in Dockerfile, start the daemon as a foreground process. Or, as some gurus suggest, write a startup script that starts the daemon or process in a script.
Tip15
Friendly communication between containers: links
This is the coolest feature! I’m saving it for the end! This is the most important new feature in 0.6.5, and we’ve already mentioned it twice. Run a container and give it a name. In the following example, we give the container a name “loldb” with the -name argument:
Run another container, add the -link argument to connect to the first container (alias lolDB), and give the second container another alias (cheez) :
Get Cheez’s environment variable:
Thus, we create a network bridge between the two containers, from which we can build a Rails like program: one 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 (cheez in this case) and the port number to open. So the database container can also use the env command to check whether the port is open.