Method steps for Docker to create an Nginx server

  • 2021-01-02 22:03:01
  • OfStack

Operating environment: MAC

Docker version: Docker version 17.12.0-ce, build c97c6d6

1. Start the Nginx server

Start the Nginx server and enter the analog terminal


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

2. Know the configuration file location of the Nginx image

Log file location: /var/log/nginx

Configuration file location: /etc/nginx

: resource storage locations/usr share/nginx/html

The configuration path above is the address in the virtual linux on my computer, please go to see your own configuration location

3. Modify the default homepage of Nginx to test whether it can be run

Important: For those who don't want to do this, you can run it directly from Step 4


/usr/share/nginx/html

echo "<h1>Hello Docker</h1>" > index.html

For those of you who are here, you might notice that when I visit port localhost:8080, the welcome screen for Nginx comes up the first time, and the 404 message comes up the second time.

On this issue, this paper does not expand the detailed sequence, if you do not understand, you can refer to:
1.docker Running nginx Why use daemon off
2.docker container exits after running, how can 1 run directly?
3. Use of Docker run command

After Docker executes docker run, it virtualizes a compact version of linux(containing only the leanest features the system can run) based on the current operating system, and then loads 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 is not run as a daemon. So when a request is heard on port 80 in Docker, the Nginx process exits after completion. There is only one process in the container, and it is non-daemon, and the request process is destroyed after execution. There is no need for the container to exist, so the service in Docker is stopped. This is why docker top does not see the currently running container.

As a temporary solution to the problem of Nginx executing once and exiting, we can go into the interactive terminal and execute nginx & Let nginx run in the background as a daemon.

Look at the container we are running


roverliang$ docker ps
CONTAINER 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 -a
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS           PORTS         NAMES
5bff285f60b3    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_web

echo "<h1>Hello Docker</h1>" > /usr/share/nginx/html/index.html

nginx & 

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

Then we visited http://localhost:8080/

It's been a long time coming, but we're finally seeing what we want.

[

Hello Docker

]

4. Turn Nginx Demo into a playable Demo

Start by creating the folders that we need to map natively


 mkdir -p docker_study/log docker_study/etc docker_study/html

Note: Create in your own home directory

Copy the nginx configuration file in our docker


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

Close our container


docker stop nginx_web

Remove demo from our exercise, and let's rebuild one that works.


docker rm nginx_web

The Nginx image is mapped to our native directory so that we can modify the files


/usr/share/nginx/html

echo "<h1>Hello Docker</h1>" > index.html
0

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, the reference is as follows:

Docker run nginx

1 passage that suddenly dawned on me:

When I ran it before, I generally used the interactive: -i guarantee container stdin open -t generate one tty terminal for the container, and add one /bin/bash at the end of the command to guarantee interaction. In reality, however, nginx is not running, leading me to believe that the container's port binding is not persistent.

Next we need to close, delete our container, and restart 1 as follows:


/usr/share/nginx/html

echo "<h1>Hello Docker</h1>" > index.html
1

5. Modified Nginx configuration to parse 1 website

Modify the nginx configuration that we just copied


/usr/share/nginx/html

echo "<h1>Hello Docker</h1>" > index.html
2

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

You're done


Related articles: