How to create an Nginx server with Docker

How to create an Nginx server with Docker

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 simulated terminal

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

2. Understand the location of the Nginx image configuration file

Log file location: /var/log/nginx

Configuration file location: /etc/nginx

Resource storage location: /usr/share/nginx/html

The above configuration path is the address of the virtual Linux on my computer. Please check your own configuration location.

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

Important Tip: For those who don't want to bother, you can run it directly from step 4

/usr/share/nginx/html

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

Some friends who have come here may find that when I access the localhost:8080 port, the Nginx welcome interface appears for the first time, and a 404 prompt appears for the second time.

This article does not go into detail about this issue. If you don’t understand, you can refer to:
1. Why use daemon off when running nginx in docker
2. How can I keep the docker container running after it exits?
3. How to use the Docker run command

After Docker executes docker run, it first virtualizes a simplified version of Linux (containing only the most simplified functions of the system operation) 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 in the system, and this script is Nginx.

Because the default Nginx does not run as a daemon process. So when Docker listens to the request on port 80, it exits the Nginx process after completion. There is only one process in the container, and it is a non-daemon process. It is destroyed after executing the request process. Then there is no need for this container to exist, so this service in Docker is stopped. This is why we cannot see the currently running container when we execute docker top.

As a temporary solution to the problem that Nginx exits after being executed only once, we can enter the interactive terminal and execute nginx & to let nginx run as a daemon process in the background.

View our running containers

roverliang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

If there is nothing, it means there is no running container.

View the 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

Entering our container

docker attach nginx_web

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

nginx & 

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

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

After so much trouble, we finally see the content we expected.

Hello Docker

4. Turn the previous Nginx Demo into a playable Demo

First create the folder we need to map on our local machine

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

Note: Create it in your home directory

Copy the configuration file of nginx in our docker

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

Close our container

docker stop nginx_web

Delete the demo for our practice and build a usable one from scratch.

docker rm nginx_web

Map the Nginx image to our local directory to facilitate file modification

docker run \
-p 8080: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 \

At this point, we may still find that there is no content when accessing http://localhost:8080/. But don't worry, the process of solving problems is the process of learning new things. Continue to search for information online, refer to the following:

Docker runs nginx

Here is a passage from the article that made me suddenly enlightened:

When I ran it before, I usually used interactive mode: -i ensured that the container's stdin was turned on -t generated a tty terminal for the container, and added a /bin/bash at the end of the command to ensure interaction. But in fact, nginx was not running, which led me to think that the port binding of the container was not persistent.

Next we need to shut down and delete our container, and then restart it with the following command:

docker run \
-p 8080: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

5. Modify Nginx configuration and parse a website

Modify the nginx configuration we just copied

cd /Users/roverliang/docker_study/etc
vim 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

You're done!

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to configure nginx+php+mysql in docker
  • Solution to invalid Nginx cross-domain setting Access-Control-Allow-Origin
  • Example method of deploying react project on nginx
  • Use nginx.vim tool for syntax highlighting and formatting configuration nginx.conf file
  • Detailed explanation of the pitfalls of add_header in nginx configuration tutorial
  • How to configure two-way certificate verification on nginx proxy server
  • Solution to the problem of information loss with "_" in header when using Nginx proxy
  • Shell script nginx automation script
  • A brief discussion on why daemon off is used when running nginx in docker
  • nginx proxy_cache batch cache clearing script introduction

<<:  Windows 10 and MySQL 5.5 installation and use without installation detailed tutorial (picture and text)

>>:  Detailed explanation of how to implement login function by combining React with Antd's Form component

Recommend

How to fix abnormal startup of mysql5.7.21

A colleague reported that a MySQL instance could ...

MySQL master-slave synchronization principle and application

Table of contents 1. Master-slave synchronization...

Implementation code for operating mysql database in golang

Preface Golang provides the database/sql package ...

MySQL installation tutorial under Windows with pictures and text

MySQL installation instructions MySQL is a relati...

Prototype and prototype chain prototype and proto details

Table of contents 1. Prototype 2. Prototype chain...

Detailed explanation of MySQL semi-synchronization

Table of contents Preface MySQL master-slave repl...

MySQL database connection exception summary (worth collecting)

I found a strange problem when deploying the proj...

MySQL detailed summary of commonly used functions

Table of contents MySQL Common Functions 1. Numer...

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...

This article teaches you how to play with CSS combination selectors

CSS combination selectors include various combina...

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...