banner
{小朋友}

xiaopy

Install Nginx image and deploy project using Docker.

Installing Docker Engine-Community#

1. Installation using Docker repository#

Before installing Docker Engine-Community for the first time on a new host, you need to set up the Docker repository. After that, you can install and update Docker from the repository.

  • Set up the repository

Install the required packages. yum-utils provides yum-config-manager, and the device mapper storage driver requires device-mapper-persistent-data and lvm2.

sudo yum install -y yum-utils  device-mapper-persistent-data  lvm2
  • Use the following command to set up the stable repository.
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

2. Install Docker Engine-Community#

Install the latest version of Docker Engine-Community and containerd, or go to the next step to install a specific version. If prompted to accept the key, choose yes.

sudo yum install docker-ce docker-ce-cli containerd.io

3. Start Docker#

sudo systemctl start docker

4. Verify the installation of Docker Engine-Community by running the hello-world image.#

sudo docker run hello-world

Installing nginx#

1. View official repository images#

docker search nginx

01

2. Pull the image#

docker pull nginx

image

3. View the image#

docker images

xxxxxxxxxx # Copy copy path\filename path\filename # Move move path\filename path\filename PowerShell

4. Start the nginx image#

The --name nginx here is optional. It is used to name the nginx container.

docker run -d -p 8080:80 --name nginx-8080 nginx

image-20220927225235759

5. Access the web page through the port#

Use the public IP address of the server followed by port 8080 to access.

If you don't see the content shown below in your browser at this step, it means that your server has not opened port 8080.

For example, in Alibaba Cloud, you can open port 8080 in the security group settings.

image-20220927225250876

6. Nginx shutdown/startup#

# Shutdown
docker stop nginx-8080    # If you named nginx using my method above
docker stop id            # If you didn't name nginx, you need to use docker ps to view the running containers and get the id of nginx to stop it

# Startup
docker restart nginx-8080 # Same as above, if you didn't name it, you need to get the id of nginx to start it

Installing git and cloning the code#

Installation#

yum install -y git

# Check if the version number appears. If it does, the installation is successful.
git version

Git configuration#

# Configure a user for committing code
git config --global user.name "Your Name"

# Configure a user email
git config --global user.email "[email protected]"

# Generate public and private keys
ssh-keygen -t rsa -C "[email protected]"

# View the public and private keys
cat /root/.ssh/id_rsa.pub

image-20220927225305381

The following is the key, copy it#

image-20220927225315877

Open GitHub#

image-20220927225327507

image-20220927225343849

image-20220927225358487

Now you can clone the code on the server#

git clone repository_address

Installing nvm on CentOS#

  • Installation can be done using curl or wget. Use the following commands:
# curl

curl -o- [https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh](https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh) | bash

# wget

wget -qO- [https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh](https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh) | bash

After installation, if you are using xshell to connect to the remote host, reconnect once, otherwise you will find that the nvm command cannot be found.

You may still see a message saying that the nvm command cannot be found. In that case, please use the source command as follows:

source ~/.bashrc

# If you are using zsh

source ~/.zshrc

At this point, execute nvm to see if it is installed successfully.

Install yarn

npm install yarn -g

Project packaging and deployment#

After normal git pull, execute the following commands#

git clone ...  # Clone the remote repository

yarn           # Install project dependencies

yarn build     # Build the project, which will generate a dist folder

Stop the docker container first#

docker ps    # Check if nginx is running, stop it if necessary


# Make sure to execute the command in the project root directory. $PWD represents the current path.
docker run -d -p 8080:80 -v $PWD/dist:/usr/share/nginx/html nginx


docker ps    # Check if nginx is running

Now you can access the project IP and successfully view it.

Simplify deployment commands with a script#

It can be cumbersome to execute commands to stop the container, pull the code, and start the container every time you write code. Create a file named start.sh in the root directory of the project.


# start.sh     Execute the command sh start.sh

git pull

yarn --registry=https://registry.npm.taobao.org/ && yarn build

# Remove the container
docker rm -f xiaopy &> /dev/null

# Start the container
docker run -d --restart=on-failure:5\
    -p 8080:80 \
    -v $PWD/dist:/usr/share/nginx/html \
    --name xiaopy nginx
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.