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
WBOYforward
2023-05-17 09:34:05947browse

Direct execution of the startup command will fail

pull image:

docker pull nginx

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

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

Run an nginx container:

docker run -d --name nginx nginx

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

Delete the original container:

docker rm -f nginx

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

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

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete