官术网_书友最值得收藏!

  • Deployment with Docker
  • Srdjan Grubor
  • 555字
  • 2021-07-02 23:22:12

Applying our custom NGINX configuration

Our directive after the system update (RUN rm /etc/nginx/conf.d/default.conf) is one that removes the default web server configuration from the container. You can find out more about the NGINX configuration with the link from our last tip, but for now, it will suffice to say that by default, all the inpidual site configuration files are stored in /etc/nginx/conf.d and NGINX Docker image comes out of the box with a simple example file called default.conf , which we absolutely do not want to use.

While we could overwrite the mentioned file, we would be stuck with the name default, which isn't very descriptive, so for our configuration, we will delete this one and add ours with a better filename.

Next, we need to make sure that the folder we will be serving files from is available and readable by the web server process. The first command using mkdir -p creates all the relevant directories, but since NGINX doesn't run as the root, we need to know what user the process will be reading the files we want to serve up or otherwise our server will not be able to display anything. We can find what the original configuration has there as the default user by showing the first few lines of the system-wide NGINX configuration included in the image at /etc/nginx/nginx.conf:

$ # Print top 2 lines of main config file in NGINX image
$ docker run --rm \
nginx /bin/head -2 /etc/nginx/nginx.conf


user nginx;

Perfect! Well, now that the user that needs to be able to read this directory is nginx , we will change the owner of our target folder with chown nginx:nginx /srv/www/html , but what is going on with that new style of run Docker command we just used when trying to find this out? If you include a command after specifying the image name instead of the CMD directive in the image, Docker will substitute it with this new command. In the preceding command, we are running the /bin/head executable, passing in arguments to tell it that we only want the top two lines from the /etc/nginx/nginx.conf file. Since this command exits as soon as it is done, the container stops and is fully removed because we used the --rm flag.

With the default configuration gone and our directories created, we can now copy our main configuration for NGINX in place with COPY nginx_main_site.conf /etc/nginx/conf.d/. The COPY argument does pretty much the obvious thing of copying a file from the current build directory into the image at a specified location.

Be very careful with how you end the COPY directive argument, as leaving the slash off will put the source into a file at the destination even if the destination is a directory. To ensure that this doesn't happen, always end your target directory paths with a slash.

Adding our main test.txt file that we want hosted is the last part, and it follows along the same lines as the other COPY directive, but we will make sure that we put this one in the folder that our NGINX configuration is referencing. Since we turned on the autoindex flag for this endpoint, there are no additional steps to be taken as the folder itself will be browsable.

主站蜘蛛池模板: 崇阳县| 普洱| 台北县| 锡林郭勒盟| 大宁县| 封丘县| 天镇县| 汝州市| 马公市| 凌源市| 巢湖市| 云阳县| 澄江县| 长子县| 大邑县| 玛多县| 阜宁县| 潞城市| 曲松县| 时尚| 安平县| 镇远县| 大竹县| 浪卡子县| 威远县| 讷河市| 蒙城县| 河北省| 民勤县| 乐亭县| 濮阳县| 阿荣旗| 边坝县| 黔西县| 高邑县| 平邑县| 平遥县| 米林县| 本溪市| 广丰县| 文水县|