Deploy simple projects using docker

Time:2022-1-13

Objective: to record the idea of learning docker to deploy a simple project

Idea:

  1. Use docker pull to pull the corresponding image source
  2. Create dockerfile file and write configuration file
  3. Docker build runs the dockerfile file and uses its configuration to create a new image
  4. Docker run creates a container and runs it

    Here’s the official logic diagram~~~~

Deploy simple projects using docker

Environment configuration

Environment: CentOS 7

//Install docker using Yum or up2date. The personal server used here is the highest privilege
sudo yum install docker

Use docker – V or docker to check whether the installation is successful

Deploy simple projects using docker

PS: about the configuration of the environment, you can use Baidu or Google. There are a lot of tutorials. Generally, the installation fails because of the permission problem (what I encountered)

Formal deployment

Create a simple file with the following main directories

`-- your-app-name/
    |-- src/
        `-- app. JS // write anything
    `-- Dockerfile   (we will create this in the following steps)

Our main focus is on the configuration of dockerfile files

#Pull the node image source. If it does not exist locally, it will be automatically pulled from the docker hub
FROM node:latest
#Execute the command to create a folder. The path here does not need to be created
#In the next step of copy, if there is no such file, the file will be automatically created under the path
RUN mkdir -p /home/nodeDemojs
#Copy the file. Copy the file under SRC to / home / nodedemojs
COPY ./src /home/nodeDemojs
#The command executed when the container is started, similar to NPM run start
CMD ["node", "/home/nodeDemojs/app.js"]

Executive document

  1. Execute docker build

    Docker build - t image name
    //The image name can be followed by the version number. For example, the image name: v1 0
    //Last We should pay special attention to the fact that we can't omit it. This function is to bind the online text we execute
  2. Execute docker images to view all our images

    Deploy simple projects using docker

  3. Execute docker run

    Using this directive, you can create a container and run it

    docker run -d -p 3000:3000 docker-demo:v1.0
    • -d: Indicates background operation
    • -p: Indicates the specified port mapping, (3000:3000) indicates (native port: container port). 3000 is the access port monitored in the author’s nestjs code and the port exposed by the container
    • After the image name: v1 0: indicates the tag number

Some common instructions

  • View mirror

    docker images
  • delete mirror

    Docker RMI image ID
  • View running containers

    docker ps
  • Stop container

    Docker stop container ID
  • Detect docker startup status

    sudo systemctl status docker.service

    Deploy simple projects using docker

  • Start docker

    sudo systemctl start docker.service
    
    //After executing the above instructions, check the current status of docker again and execute sudo systemctl status docker service~~~~
  • Restart docker

    sudo systemctl restart docker.service