Home Operation and Maintenance Nginx How docker nginx deploys multiple projects

How docker nginx deploys multiple projects

May 14, 2023 pm 04:16 PM
docker nginx

Prerequisites

1. Docker has been installed on the local computer and server. You can download it on Google.

2. Already have an account on docker hub, register and send it Door:

3. You need to be familiar with docker and understand some instructions in dockerfile

Use dockerfile to make images

If this machine There is a project called web

Create a new dockerfile in the web root directory and write the following content

from nginx:1.13.6-alpine
label maintainer="lilywang <lilywang.cd@gmail.com>"

arg tz="asia/shanghai"

env tz ${tz}

run apk upgrade --update \
 && apk add bash tzdata \
 && ln -sf /usr/share/zoneinfo/${tz} /etc/localtime \
 && echo ${tz} > /etc/timezone \
 && rm -rf /var/cache/apk/*

copy dist /usr/share/nginx/html 

cmd ["nginx", "-g", "daemon off;"]
Copy after login

At this time, the file structure in the web is:

.
|____dockerfile
|____dist // 为项目打包后的文件
| |____index.html
Copy after login

Next, enter the web directory in bash

cd web

docker build -t lilywang711/web .
Copy after login

If you see the following in the print information, it means that the image has been built successfully

successfully built 4c050212ce0d
successfully tagged lilywang711/web: latest

You can also enter docker images to view the current image list

Then enter the command docker push lilywang711/web to The image just built is uploaded to docker hub so that we can pull the image on the server later.

If there are multiple projects that need to be deployed, just follow the above steps and repeat them. How many projects are there? Just build as many mirrors

Server-side deployment

Ssh log in to the server, create a new nginx folder in the current user directory (I am the root directory), and put it in it Create new nginx.conf
Write the following content in nginx.conf

user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid  /var/run/nginx.pid;
events {
 use epoll;
 worker_connections 2048;
}
http {
 include /etc/nginx/mime.types;
 # include /etc/nginx/conf.d/*.conf;
 root /usr/share/nginx/html;
 index index.html index.htm;
 server {
  listen 80;
  server_name a.yourdomain.cn;
  location / {
  }
 }
 server {
  listen 80;
  server_name b.yourdomain.cn;
  location / {
   proxy_pass http://your_vps_ip:81;
  }
 }
 server {
  listen 80;
  server_name localhost;
  location / {
  }
 }
}
Copy after login

Next

Start docker systemctl start docker

Pull The two images just created and uploaded

docker pull lilywang711/web

docker pull lilywang711/web1

Enter The following command starts the container

docker run -itd --name web -p 80:80 -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf lilywang711/web
// -i 交互模式运行容器, -t 为容器分配一个伪终端,-d 后台运行容器,可直接连写 -itd
// --name 是给该容器起个叫web的名字,方便辨识
// -p 是绑定端口 本机端口80:容器端口80
// -v 声明volume,意思是将容器中的/etc/nginx/nginx.conf 挂载到 宿主机里的/root/nginx/nginx.conf,以后配置nginx只需要修改/root/nginx/nginx.conf就行了
Copy after login

The same goes for the other lilywang711/web1 image, just change the port and name

docker run -itd --name web1 -p 81: 80 -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf lilywang711/web1

At this time, enter docker ps to see these two The container is already running

Dockerized the project and deployed it on nginx.

Enter http://a.yourdomain.cn and http://b in the browser .yourdomain.cn you can see the effect, corresponding to the web and web1 projects in the local computer respectively

docker nginx如何部署多个项目

docker nginx如何部署多个项目

The above is the detailed content of How docker nginx deploys multiple projects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What's wrong with nginx running for a while? What's wrong with nginx running for a while? Apr 14, 2025 am 07:18 AM

The reasons why nginx hangs up after running for a period of time: 1. Memory leak; 2. Configuration error; 3. Insufficient resources; 4. External factors. Solution: 1. Diagnose memory leaks; 2. Fix configuration errors; 3. Provide more resources; 4. Exclude external factors.

How to implement nginx load balancing How to implement nginx load balancing Apr 14, 2025 am 07:21 AM

Nginx load balancing defines backend servers through the upstream module and uses the location block to proxy the request to these servers. Supports load balancing strategies such as polling, minimum number of connections, response time weighting, and ip_hash. Configuration examples include defining an upstream group and pointing to it using the proxy_pass directive.

nginx restart command nginx restart command Apr 14, 2025 am 07:27 AM

nginx restart command: sudo systemctl restart nginx. Other related commands include: 1. Start: sudo systemctl start nginx; 2. Stop: sudo systemctl stop nginx; 3. Check status: sudo systemctl status nginx.

Apr 14, 2025 am 07:36 AM

Nginx Autoindex is a function of generating directory listing HTML pages, which is used to browse files and view file information when requesting directories instead of files. It can be customized with configuration options such as displaying the exact file size, local time, and custom page format. Advantages include easy browsing, easy configuration and providing file information. Disadvantages include security risks, performance impact, and the inability to customize the appearance of the page.

What is the reason for nginx403 What is the reason for nginx403 Apr 14, 2025 am 07:39 AM

nginx 403 error indicates that the client does not have permission to access the resource. Factors that cause this problem may include: permission settings, nginx configuration, CGI script errors, .htaccess files, or other reasons. Troubleshooting steps include: checking permission settings, reviewing nginx configuration, testing CGI scripts, checking .htaccess files, excluding firewalls or security software, and checking servers and file systems.

How to configure virtual hosts in nginx How to configure virtual hosts in nginx Apr 14, 2025 am 08:15 AM

Configuring nginx virtual host allows multiple websites to be hosted on a single server, each with a separate domain name and root directory. The specific configuration steps include: creating a virtual host configuration file to configure the server block, specifying the server listening port, virtual host domain name and document root directory to enable the virtual host, and linking the configuration file to the enabled directory to reload nginx

How to redirect in nginx How to redirect in nginx Apr 14, 2025 am 08:42 AM

Methods for redirecting through Nginx are 301 permanent redirects (update links or mobile pages) and 302 temporary redirects (handling errors or temporary changes). Configuring redirection involves using location directives in server blocks, advanced features include regular expression matching, proxy redirection, and condition-based redirection. Common uses of redirects include updating URLs, handling errors, redirecting HTTP to HTTPS, and guiding users to a specific country or language version.

The relationship between nginx and web server The relationship between nginx and web server Apr 14, 2025 am 07:09 AM

nginx is a lightweight, non-blocking web server and reverse proxy, commonly used for front-end proxy, load balancing, and caching. Its relationship with a web server is usually: Front-end proxy: nginx handles requests and forwards them to the back-end server. Load Balancer: nginx distributes requests to multiple backend servers. Caching: nginx caches frequently accessed files for performance.

See all articles