Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method steps for Docker to create a Nginx server

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Operating environment: MAC

Docker version: Docker version 17.12.0-ce, build c97c6d6

First, start the Nginx server

Start the Nginx server and enter the analog terminal

Docker run-p 8080 it nginx 80-- name nginx_web-it nginx / bin/bash

2. Know the configuration file location of Nginx image

Log file location: / var/log/nginx

Configuration file location: / etc/nginx

Location where resources are stored: / usr/share/nginx/html

The above configuration path is the address in the virtual linux on my computer. Please take a look at your own configuration location.

Modify the default home page of Nginx to test whether it can be run

Important note: for students who don't want to make trouble, you can run it directly from step 4.

/ usr/share/nginx/htmlecho "Hello Docker" > index.html

Some friends here may find that when I visit the localhost:8080 port, the welcome interface of Nginx appears for the first time, and 404 prompts appear the second time.

On this issue, this article does not expand the detailed preface, if you do not understand, you can refer to:

Why should 1.docker use daemon off to run nginx

The 2.docker container exits after running, how can it run all the time?

How to use the 3.Docker run command

After Docker executes docker run, we first virtualize a stripped-down version of linux (including only the most streamlined features that the system is running) based on the current operating system, and then load our Nginx image. When the Nginx image is loaded into our virtual Linux environment, it is equivalent to executing a script on the system, and this script is Nginx.

Because the default Nginx does not run as a daemon. So when the Docker internal monitor heard the request from port 80 and finished, he exited the process of Nginx. There is only one process in the container, and it is unguarded, and the request process is destroyed after execution. Then there is no need for the container to exist, so the service in Docker is stopped. That's why we can't see the currently running container when we execute docker top.

As a temporary solution to the problem of Nginx exiting after only one execution, we can go to the interactive terminal and execute nginx & let nginx run in the background as a daemon.

Look at the container we are running

Roverliang$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

If there is nothing, there is no currently running container.

View containers that have finished running

Roverliang$ docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5bff285f60b3 nginx "/ bin/bash" 9 minutes ago Exited (0) 6 minutes ago nginx_web

Restart the container we just started

Docker start nginx_web

Into our container.

Docker attach nginx_webecho "Hello Docker" > / usr/share/nginx/html/index.htmlnginx &

Then use the shortcut key control + Q to exit the current container

Then we visit again in the browser: http://localhost:8080/

After tossing about for so long, we finally saw what we expected.

Hello Docker

4. Change the front Nginx Demo into a playable Demo

First create the folder that we need to map on this machine

Mkdir-p docker_study/log docker_study/etc docker_study/html

Note: create in your own home directory

Copy the configuration file of nginx in our docker

Docker cp 65bc23f952db:/etc/nginx/ / Users/roverliang/docker_study/etc/

Close our containers.

Docker stop nginx_web

Delete the demo of our exercise and we'll re-build one that works.

Docker rm nginx_web

Map the Nginx image to our local directory to make it easier for us to modify the file

Docker run\-p 8080 Users/roverliang/docker_study/html/:/usr/share/nginx/html 80\-- name nginx_web\-v / Users/roverliang/docker_study/log/:/var/log/nginx\-v / Users/roverliang/docker_study/etc/nginx.conf:/etc/nginx/nginx.conf\-v / Users/roverliang/docker_study/html/:/usr/share/nginx/html\-it\-d\ nginx\ / bin/bash\

Running here, we may still find that accessing http://localhost:8080/ has no content. But don't worry, the process of solving the problem is the process of learning new things. Continue to look up information online. Refer to the following:

Docker runs nginx

An excerpt from the article suddenly enlightened me:

When I run it before, I usually use interactive:-I make sure that the stdin of the container is on-t generate a tty terminal for the container, and add a / bin/bash at the end of the command to ensure that it can interact. But in fact, nginx is not running, which leads me to think that the port binding of the container is not persistent.

Next we need to shut down and delete our container, and restart one as follows:

Docker run\-p 8080 Users/roverliang/docker_study/log/:/var/log/nginx 80\-- name nginx_web\-v / Users/roverliang/docker_study/log/:/var/log/nginx\-v / Users/roverliang/docker_study/etc/nginx.conf:/etc/nginx/nginx.conf\-v / Users/roverliang/docker_study/html/:/usr/share/nginx/html\-d\ nginx

Modify the Nginx configuration to parse a website

Modify the nginx configuration we just copied

Cd / Users/roverliang/docker_study/etcvim nginx.conf

Add the following configuration to the Http module:

Server {listen 80; server_name www.test_nginx.com; index index.html; root / usr/share/nginx/html;}

Then go back to the host and bind host 127.0.0.1 www.test_nginx.com

The great task has been completed

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report