Docker nginx example method to deploy multiple projects

Docker nginx example method to deploy multiple projects

Prerequisites

1. Docker has been installed on the local computer and server. Please Google the download method.

2. Already have an account on Docker Hub, register at: https://hub.docker.com/

3. You need to be familiar with Docker and understand some instructions in Dockerfile

Using Dockerfile to create an image

If this machine has 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 <[email protected]>"

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;"]

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

.
|____Dockerfile
|____dist // Files packaged for the project| |____index.html

Next, enter the web directory in bash

cd web

docker build -t lilywang711/web .

If you see the following in the printed 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

Next, enter the command docker push lilywang711/web to upload the image just built to the docker hub, so that we can pull the image on the server later.

If there are multiple projects to deploy, just repeat the above steps and build as many images as there are projects.

Server deployment

Log in to the server through ssh, create a new nginx folder in the current user directory (my directory is root), and create nginx.conf in it
Write the following 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 / {
  }
 }
}

Next Steps

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 to start the container

docker run -itd --name web -p 80:80 -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf lilywang711/web
// -i runs the container in interactive mode, -t allocates a pseudo terminal for the container, -d runs the container in the background, you can directly write -itd
// --name is to give the container a name called web for easy identification // -p is to bind the port local port 80: container port 80
// -v declares volume, which means to mount /etc/nginx/nginx.conf in the container to /root/nginx/nginx.conf in the host. To configure nginx in the future, you only need to modify /root/nginx/nginx.conf

The same goes for another 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 point, enter docker ps and you can see that the two containers are already running.

Dockerizing the project and deploying it in nginx is complete

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

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 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
  • Docker Nginx container production and deployment implementation method
  • How to deploy Nginx on Docker

<<:  MySQL 5.5 installation and configuration graphic tutorial

>>:  Install two MySQL5.6.35 databases under win10

Recommend

Vue uses mixins to optimize components

Table of contents Mixins implementation Hook func...

How to make your own native JavaScript router

Table of contents Preface Introduction JavaScript...

How to display web pages properly in various resolutions and browsers

The key codes are as follows: Copy code The code i...

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...

Summary of Linux date command knowledge points

Usage: date [options]... [+format] or: date [-u|-...

React Router V6 Updates

Table of contents ReactRouterV6 Changes 1. <Sw...

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

In-depth explanation of Mysql deadlock viewing and deadlock removal

Preface I encountered a Mysql deadlock problem so...

The difference between MySQL database host 127.0.0.1 and localhost

Many of my friends may encounter a problem and do...

Echarts Bar horizontal bar chart example code

Table of contents Horizontal bar chart Dynamicall...

Vue complete code to implement single sign-on control

Here is a Vue single sign-on demo for your refere...

How to solve the problem of margin overlap

1. First, you need to know what will trigger the v...

How to insert Emoji expressions into MySQL

Preface Today, when I was designing a feedback fo...