Docker Nginx container production and deployment implementation method

Docker Nginx container production and deployment implementation method

Quick Start

1. Find the nginx image on Docker Hub

docker search nginx

2. Pull the official Nginx image

docker pull nginx

3. Find the mirror with REPOSITORY as nginx in the local mirror list

docker images nginx

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4bb46517cac3 4 weeks ago 133MB

4. The following command starts an Nginx container instance using the default configuration in the NGINX container:

Copy the code as follows:
docker run --rm --name nginx-test -p 8080:80 -d nginx

The meanings of the four command line parameters of this command are as follows.

  • --rm: Automatically delete container files after the container terminates.
  • --name nginx-test: The name of the container is nginx-test, and the name is defined by yourself.
  • -p: Map the port to map the local port 8080 to the port 80 inside the container
  • -d: After the container is started, run in the background
  • The nginx after the -d parameter is the name of the container image to be started.

5. View the started docker container

docker container ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acb0e263dff3 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp nginx-test

6. Access in the browser. I am using Tencent Cloud Host. Just access the public IP+port.

Open http://public network ip:8080 in the browser, the effect is as follows.

Deployment Service

1. Create a local directory to store Nginx related file information.

mkdir -p /home/nginx/www /home/nginx/logs /home/nginx/conf

in:

  • The www: directory will be mapped to the virtual directory configured in the nginx container.
  • logs: The directory will be mapped to the log directory of the nginx container.
  • The configuration files in the conf: directory will be mapped to the configuration files of the nginx container.

2. Copy the default Nginx configuration file in the container to the conf directory under the local current directory. The container ID can be viewed in the first column of the docker ps command input:

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acb0e263dff3 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp nginx-test

docker cp acb0e263dff3:/etc/nginx /home/nginx/conf

3. Stop this container

docker container stop nginx-test

Note the command to enter the container: docker exec -it nginx-test /bin/bash

4. Deployment Commands

docker run --rm -d -p 8080:80 --name nginx-test-web \
-v /home/nginx/www:/usr/share/nginx/html \
-v /home/nginx/conf/nginx:/etc/nginx \
-v /home/nginx/logs:/var/log/nginx \
nginx

Command Explanation:

  • --rm: Automatically delete container files after the container terminates.
  • -p 8080:80: Map the container's port 80 to the host's port 8080.
  • --name nginx-test-web: Name the container nginx-test-web
  • -v /home/nginx/www:/usr/share/nginx/html: mount the www directory we created to the container’s /usr/share/nginx/html.
  • -v /home/nginx/conf/nginx:/etc/nginx: mount the nginx directory under conf that we created ourselves to the /etc/nginx of the container.
  • -v /home/nginx/logs:/var/log/nginx: mount the logs we created ourselves to the container's /var/log/nginx.

5. After launching the above command, enter the /home/nginx/www directory:

cd /home/nginx/www/
vi index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Nginx test !!!</title>
</head>
<body>
<h1>My first title</h1>
<p>My first paragraph. </p>
</body>
</html>

6. Access in browser

Enter http://public network ip:8080/ in the browser, the output is as follows. If a 403 error appears during access, it should be that the index.html file has insufficient permissions. Just set it to 644.

Support HTTPS, HTTP2

1. Create a subdirectory certs in the directory /home/nginx/conf/nginx

mkidr certs

2. Generate a certificate

openssl req \
-x509 \
-nodes \
-days 365 \
-newkey rsa:2048 \
-keyout example.key \
-out example.crt

The meanings of the parameters in the above command are as follows.

  • req: Processes a certificate signing request.
  • -x509: Generate a self-signed certificate.
  • -nodes: Skip the stage of setting a password for the certificate so that Nginx can open the certificate directly.
  • -days 365: The certificate is valid for one year.
  • -newkey rsa:2048: Generates a new private key using the 2048-bit RSA algorithm.
  • -keyout: The newly generated private key file is example.key in the current directory.
  • -out: The newly generated certificate file is example.crt in the current directory.

If the directory is created successfully, two more files will be created: example.key and example.crt.

3.HTTPS configuration

Create the https.conf file in the /home/nginx/conf/nginx/conf.d directory and write the following:

server {
  listen 443 ssl http2;
  server_name localhost;

  ssl on;
  ssl_certificate /etc/nginx/certs/example.crt;
  ssl_certificate_key /etc/nginx/certs/example.key;

  ssl_session_timeout 5m;

  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }
}

4. Deployment Service

docker run --rm -d -p 8080:80 -p 8081:443 --name nginx-test-web \
-v /home/nginx/www:/usr/share/nginx/html \
-v /home/nginx/conf/nginx:/etc/nginx \
-v /home/nginx/logs:/var/log/nginx \
nginx

5. Quick Test

http://public network ip:8080/: access http
https://public network ip:8081/: access https

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:
  • Docker builds Nginx+PHP+MySQL environment and deploys WordPress practice
  • How to deploy nginx with Docker and modify the configuration file
  • Docker for Beginners and Detailed Steps for Deploying NGINX
  • Docker nginx example method to deploy multiple projects
  • Docker deployment nginx implementation process graphic and text detailed explanation
  • Docker deploys Nginx and configures reverse proxy
  • Try Docker+Nginx to deploy single page application method
  • How to deploy Nginx on Docker

<<:  How to get the maximum or minimum value of a row in sql

>>:  HTML table tag tutorial (12): border style attribute FRAME

Recommend

MySQL partitioning practice through Navicat

MySQL partitioning is helpful for managing very l...

CSS margin overlap and how to prevent it

The vertically adjacent edges of two or more bloc...

Tutorial diagram of installing TomCat in Windows 10

Install TomCat on Windows This article will intro...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Hyper-V Introduction and Installation and Use (Detailed Illustrations)

Preface: As a giant in the IT industry, Microsoft...

A very detailed explanation of Linux C++ multi-thread synchronization

Table of contents 1. Mutex 1. Initialization of m...

Introduction to the three essential logs for MySQL database interviews

Table of contents 1. redo log (transaction log of...

Detailed instructions for installing mysql5.7 database under centos7.2

The mysql on the server is installed with version...

HTML form tag tutorial (2):

This tutorial introduces the application of vario...

Vue implements button switching picture

This article example shares the specific code of ...

CSS code to achieve background gradient and automatic full screen

CSS issues about background gradient and automati...

Solve the problem of docker images disappearing

1. Mirror images disappear in 50 and 93 [root@h50...

Detailed explanation of the working principle and solution of Js modularization

Table of contents 1. Modular concept 2. Modulariz...

In-depth understanding of JavaScript event execution mechanism

Table of contents Preface The principle of browse...

Solve the problem of inconsistent MySQL storage time

After obtaining the system time using Java and st...