Home > Operation and Maintenance > Nginx > How to solve the problem when docker fails to run nginx binding configuration file

How to solve the problem when docker fails to run nginx binding configuration file

WBOY
Release: 2023-05-17 09:34:05
forward
978 people have browsed it

Direct execution of the startup command will fail

pull image:

docker pull nginx
Copy after login

Then execute the startup command:

docker run -d -p 80:80 -p 443:443 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/cache:/var/cache/nginx \
--restart=always nginx
Copy after login
Copy after login

If /mydata/nginx/conf/nginx. The conf file does not exist, and a docker error will appear here because docker does not allow binding files that do not exist.

And directly create an empty /mydata/nginx/conf/nginx.confAlthough docker will not report an error, nginx cannot start normally in the container, through docker ps -aCommand view, nginx will be in exit or always restart state, because the operation of nginx depends on the relevant configuration in the nginx.conf configuration file .

Solution ideas and methods

First run a container without using -v binding, and then copy the relevant files in the container directly to the specified location. After that, you can delete the container and run it directly. the startup command.

The specific operations are as follows:

First create the relevant folders:

mkdir -p \
/mydata/nginx/html \
/mydata/nginx/conf \
/mydata/nginx/logs \
/mydata/nginx/cache
Copy after login

Run an nginx container:

docker run -d --name nginx nginx
Copy after login

Copy the configuration file and folder to Host specified directory:

docker cp nginx:/etc/nginx/nginx.conf /mydata/nginx/conf/
docker cp nginx:/etc/nginx/conf.d /mydata/nginx/conf.d
Copy after login

Delete the original container:

docker rm -f nginx
Copy after login

Run the startup command, -v binds the relevant volume:

docker run -d -p 80:80 -p 443:443 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/cache:/var/cache/nginx \
--restart=always nginx
Copy after login
Copy after login

Move the copyconf.dThe files in the directory to the correct location:

 mv /mydata/nginx/conf.d/* /mydata/nginx/conf/conf.d/
 rm -rf /mydata/nginx/conf.d
Copy after login

So that the nginx container can run normally, and we will use the above operation to move the nginx.conf file and conf.d The directories are all mapped to the host machine. If you need to modify the nginx.conf file or add the .conf file to conf.d in the future, you only need to modify it on the host machine. Just operate the corresponding position.

The above is the detailed content of How to solve the problem when docker fails to run nginx binding configuration file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template