The Docker we use is all used in the machine, that is, the Docker server and the client have the same machine, but sometimes, it is necessary to connect the docker of the remote server in the machine, this paper studies this situation.
Basic knowledge: Docker run
When we install docker with apt, we will automatically configure a lot of things, but these operations are hidden, let’s use an apt-get install to complete the operation. Docker is divided into server and client, but cannot be distinguished in command form. After we ran service docker start docker service was launched, can use ps aux | grep docker to query, as follows:
Root 11602 0.5 2.3 570236 47856? Ssl 11:57 0:00 /usr/bin/dockerd -H fd://Copy the code
As you can see, there is a process called Dockerd, which is a daemon (d is the first letter of a daemon), so it is the docker server. The server must be running. If it is not running, most docker commands cannot be used properly (but docker -v is not affected). Such as:
# docker images
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Copy the code
The error message is obvious: The server is not running. But usually we use Docker on the same machine, do not pay attention to these, think there is only one Docker program.
Since there is a server, docker service can be arranged to other machines, or docker service on other machines can be used.
questions
Consider the following situations: Jenkins is deployed in Docker, but CICD needs to build a Docker image in Jenkins. Methods are:
- Docker in Docker, install docker service in docker container, need to add privilege option when docker run.
- Docker in Docker, to mount the host docker service, Jenkins only needs the Docker client.
- Jekins goes remote to another host and builds on that host.
This article uses a third-party Docker server.
Implementation scheme
First stop the Docker service:
service docker stop
Copy the code
Modify/lib/systemd/system/docker. Service file, the original:
ExecStart=/usr/bin/dockerd -H fd://
Copy the code
Is amended as:
ExecStart = / usr/bin/dockerd -h TCP: / / 192.168.1.78:2376Copy the code
Note that TCP ://192.168.1.78 is the IP address of the test machine, which needs to be changed according to the actual situation. Restart:
systemctl daemon-reload
service docker start
Copy the code
Example:
Docker-h TCP ://192.168.1.78:2376 build-t busybox_tCopy the code
The generated image is at 192.168.1.78. Cannot connect to the Docker daemon at TCP ://192.168.1.78:2376. Is the Docker daemon running? . This is similar to the previous error message.
Docker server to view:
Docker -h TCP ://192.168.1.78:2376 images REPOSITORY TAG IMAGE ID CREATED SIZE busybox_t latest ad27937bd9f8 About a Minute line 4.39 MBCopy the code
summary
1. Docker server can only be used solely for this occasion. In command operation, you must add -h TCP ://192.168.1.78:2376 after docker. Other options and parameters remain unchanged. It will be stuck if not added because no Docker server is specified. 2. This paper only tries, and there is not much understanding and practical guidance about this method.