Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

In the past, when deploying projects, we always encountered the problem of inconsistent local environment and online environment, resulting in troubleshooting difficulties. In the use of Windows system computer development learning, but want to learn some operations of Linux system or project deployment, but also need a server or a virtual machine, Docker is not only convenient to deploy but also more secure, Docker container is relatively lightweight, occupy fewer resources, low cost and many other advantages. So, Docker is definitely one of the tools programmers must know.

The following will deepen your learning by deploying the Node service using Docker

Docker deploys the Node service

  • Create the Node project and build an index.js file

    • const Koa = require('koa');
      const app = new Koa();
      ​
      app.use(async ctx => {
        ctx.body = 'Hello Jasen Docker';
      });
      ​
      const port = 8004;
      app.listen(port);
      console.log(`http://localhost:${port}`);
      Copy the code
    • Add Dockerfile, Docker automatically generates the image by reading the instructions in Dockerfile

      • Vscode installs the Docker plug-in

      • Press F1 to enter: Docker: Add Docker Files into Workspace. Then enter the option and enter

      • Dockfile:

        • FROM Basic Mirror
        • MAINTAINER MAINTAINER information
        • ENV Sets environment variables
        • RUN Specifies the command to RUN when building an image
        • The ADD/COPY file is added to the container
        • WORKDIR == CD, working directory
        • VOLUME Specifies the persistence directory
        • EXPOSE specifies the port to interact with the outside world
        • CMD: Run the command, invoked when the container is started
  • Create an image: Go to the current directory of the project where the Dockerfile resides and run the following command:docker build -t node-proj .
  • Docker run -d [–name [name]] [-p [external port]:[Internal port]] [Service image name]

    • -d: container background execution. –name: container name; -p: indicates port mapping. Service image name: Image Id or REPOSITORY REPOSITORY

After the execution is complete, “Hello Jasen Docker” is displayed on the page by accessing http://localhost:8004 in the browser. The whole process succeeds.

Docker Compose manages the service

How does the image update when the local project is updated?

The way I started was to stop the container and then delete it, delete the image, rebuild and then run again. The whole operation is too tedious, if I have more than one container, more time to increase the workload.

With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.

Docker-compose configuration file:

Version: '3.4' services: dockerNode: container_name: docker_web image: dockerProj build: context:. Dockerfile: ./Dockerfile environment: NODE_ENV: production depends_on: - redis - db ports: - 8004:8004 redis: image: redis db: image: postgresCopy the code

Run the docker-compose command to create and start the configured service

docker-compose stop
docker-compose  up -d --build
Copy the code

Where, depends_on is used to set the dependency.

  • Docker-compose up: starts services in dependency order. In the following example, DB and Redis are started before Web is started.
  • Docker-compose up SERVICE: automatically contains SERVICE dependencies. In the following example, docker-compose up Web will also create and start db and Redis.
  • Docker-compose stop: Stops services in dependency order. In the following example, the Web stops before DB and Redis.