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

Implementation of new issues of CSS3 selectors

Table of contents Basic Selector Extensions Attri...

How to create a MySQL master-slave database using Docker on MacOS

1. Pull the MySQL image Get the latest MySQL imag...

td width problem when td cells are merged

In the following example, when the width of the td...

Introduction to the use and disabling of transparent huge pages in Linux

introduction As computing needs continue to grow,...

Steps to create your own YUM repository

To put it simply, the IP of the virtual machine u...

Detailed instructions for installing Jenkins on Ubuntu 16.04

1. Prerequisites JDK has been installed echo $PAT...

Implementation process of nginx high availability cluster

This article mainly introduces the implementation...

Detailed explanation of VUE responsiveness principle

Table of contents 1. Responsive principle foundat...

Two ways to understand CSS priority

Method 1: Adding values Let's go to MDN to se...

Vue basic instructions example graphic explanation

Table of contents 1. v-on directive 1. Basic usag...

Native js implementation of magnifying glass component

This article example shares the specific code for...

How to configure https for nginx in docker

Websites without https support will gradually be ...

Detailed tutorial on installation and configuration of nginx under Centos7

Note: The basic directory path for software insta...

A brief discussion on the role of HTML empty links

Empty link: That is, there is no link with a targ...

Summary of common problems and application skills in MySQL

Preface In the daily development or maintenance o...