Home >Operation and Maintenance >Docker >How to solve the error when the container uses the host docker

How to solve the error when the container uses the host docker

PHPz
PHPzOriginal
2023-04-17 16:42:43872browse

In recent years, Docker has become increasingly popular as an efficient containerization solution. However, there are also some problems encountered when using Docker for containerized deployment. For example, when accessing the host inside the container, the container will report an error using the host Docker. This article explains the cause of this problem and how to fix it.

Problem description

When using Docker to deploy applications, sometimes it is necessary to access the host file system or the host's Docker daemon from within the container. Typically, this is done by adding the -v /:/host and -v /var/run/docker.sock:/var/run/docker.sock parameters when starting the container. implement this function. However, in some cases, when the container tries to access the host Docker daemon, an error message similar to the following will appear:

FATA[0000] Post http:///var/run/docker.sock/v1.18/containers/create: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?

This is because the process in the container will first try to find the Docker daemon process. Search in the /var/run/docker.sock directory inside the container, but this directory does not exist inside the container, so this error will occur.

Solution

Method 1: Use the --privileged parameter

When starting the container, use the --privileged parameter The container can be given all the permissions of the host, including access to the Docker daemon. However, using this method will allow the container to obtain higher permissions and pose greater security risks, so it is recommended to use it with caution.

docker run --privileged -v /:/host -v /var/run/docker.sock:/var/run/docker.sock <image>

Method 2: Use environment variables

When starting the container, you can use environment variables to specify the address of the Docker daemon to avoid the container being in /var/run/docker.sock Find the process of Docker daemon.

docker run -v /:/host -e DOCKER_HOST=unix:///host/var/run/docker.sock <image>

Method 3: Switch to TCP connection method

Using TCP connection instead of Unix Socket can also solve this problem. You need to add the -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock parameter when starting the Docker daemon, and then use DOCKER_HOST# in the container ## Environment variables to specify the use of TCP to connect to the Docker daemon.

docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
docker run -e DOCKER_HOST=172.xx.xx.xx:2375 <image>
Conclusion

The problem of errors reported when the container uses the host Docker is caused by the path problem when the process in the container is looking for the Docker daemon process. This article introduces three solutions: using the

--privileged parameter, using environment variables, and changing the connection method of the Docker daemon. This problem can be easily solved by choosing the appropriate method according to the actual situation.

The above is the detailed content of How to solve the error when the container uses the host docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn