WordPress docker deployment using – link can directly connect to the database without configuring MySQL database connection.
Here I want to discuss what link has done
First of all, we need to clarify two concepts: MySQL is the source container and the connected container, WordPress is the target container and the receiving container
For the next test, we start two busybox containers and connect them together
For WordPress containers, WP_ MySQL and MySQL are a container with the same containerid
–link source:mysource
For the target container, source and mysource are one container
2. DNS
3. environment variable
The reason why WordPress can directly connect to the database above is that it actually uses environment variables
The image of WordPress will read the database configuration from the alias MySQL by default, so if it is written as –link wp_mysql:test_mysql and WordPress can’t recognize the database, which can be understood as a convention
The target container gets the following environment variables
4. If the IP of the source container changes
After deleting the source container, I create an nginx container, which takes up all the resources of the original source container
172.17.0.2 IP you can see that the new IP is 172.17.0.4
At this point, no content is updated in the target container
Start a target2 container, and you can see that DNS and env are replaced with new ones
Generally, service processes such as apache2, Tomcat and MySQL will not stop directly, so docker run and docker start will not exit directly in this case
docker run -i -t –name nginx nginx
The -i -t parameter is fundamental to providing an interactive shell
-i ensures that stdin in the container is open and can be attached with docker attach
-t assigns a pseudo TTY terminal to the created container so that the newly created container can provide an interactive shell
If you want to interfere with the process in Linux system, you need to send some specific signals
There are dozens of these signals, so we won’t list them one by one
There are three common ways to stop or terminate a process,
CTRL + C sends SIGINT signal, which will and will only send to the process in the currently active window and all the subprocesses of the process (process tree)
Kill PID sends SIGTERM, which can be blocked, processed, and ignored
Kill – 9 PID sends sigkill. Sigkill signal cannot be blocked, processed, or ignored. It stops the specified process directly and does not affect other child processes and parent processes. However, the parent process of the child process will be changed from the process that was killed to the init process
SIGTERM and SIGINT signals will be processed according to the logic defined by the program, such as the following Java program
import sun.misc.Signal;
import sun.misc.SignalHandler;
@SuppressWarnings("restriction")
public class Test {
public static void main(String[] args) {
System.out.println("Signal handling example.");
SignalHandler handler = new MySignalHandler();
// kill命令
Signal termSignal = new Signal("TERM");
Signal.handle(termSignal, handler);
// ctrl+c命令
Signal intSignal = new Signal("INT");
Signal.handle(intSignal, handler);
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
System.out.println("Interrupted: " + e.getMessage());
}
}
}
@SuppressWarnings("restriction")
class MySignalHandler implements SignalHandler {
@Override
public void handle(Signal signal) {
System.out.println("Signal handler called for signal " + signal);
try {
System.out.println("Handling " + signal.getName());
} catch (Exception e) {
System.out.println("handle|Signal handler" + "failed, reason "
+ e.getMessage());
e.printStackTrace();
}
}
}
When using Ctrl + C to send SIGINT signal to it, it just outputs information and does not stop itself
After using kill PID to send SIGTERM signal, it only outputs information and does not stop itself
Finally, the sigkill signal of kill – 9 PID directly kills the system
To stop a container, the most common method is docker stop
The stop command essentially sends SIGTERM signal to the process (such as nginx process). Most applications can correctly respond to this signal and start to stop themselves
If there is a special case, you need to use docker kill container_ Name to send sigkill
Alpine Linux is a Linux distribution based on busybox and Musl Libc. Its biggest advantage is its small size. When I write this article, the latest version is 3.12.4, with a size of only 2.67M
Alpine has developed a lot of community versions, basically aiming at small
docker pull alpine/git 11.39M
docker pull alpine/socat 3.22M
docker pull python:rc-alpine 16.54M
In fact, alpine is not the smallest image. There is also an empty image called scratch
Scratch doesn’t even provide the most basic / bin, / usr, / lib, / dev
Through the go language to write a HelloWorld and the code compiled into binary format, directly use the
CMD “./hello”
The following error will be reported because the scratch image does not even provide the most basic sh
In this case, some changes to the format of the CMD are required
FROM library/golang:alpine as base
COPY hello.go /hello.go
WORKDIR /
RUN go build /hello.go
FROM scratch
COPY --from=base /hello /hello
CMD [ "/hello" ]