Getting started with Dcoker: Using Docker to deploy Node. js applications

Source: Internet
Author: User
Tags install node node server docker ps docker cp docker hub

Getting started with Dcoker: Using Docker to deploy Node. js applications
Docker Introduction


Zookeeper Docker is an open-source application container engine that provides us with secure, portable, and reusable automated deployment methods. Docker uses virtualization technology to virtualize the running environment of applications. The same is true. Docker is like a ship. Each small box on the ship can be seen as an application we need to deploy. Using Docker can make full use of the server's system resources, simplify the tedious process of automated deployment and O & M, and reduce many exceptions caused by different development and production environments. This increases productivity.
Zookeeper Docker has three core concepts:

  • Image (images): a read-only template that can be understood as the running environment of an application, including the environment and basic configurations on which the program runs. It is equivalent to something in every small box.
  • Repository: a repository used to store image files. It can be seen as the same as gitlab.
  • Container: a virtual container that runs an application. The biggest difference between a container and an image is that the top layer of the container is readable and writable. It is equivalent to every small box in.
Install Mac in Docker

Click here to download the official website.

Liunx

Open terminal Input

Curl https://releases.rancher.com/install-docker/17.12.sh | sh

Wait until the installation is complete. Check whether the installation is successful.

The environment has been installed. Now, deploy the application.

Docker preparations initialize a Node. Js Program

Follow these steps to install NodeJS. If not, refer to the previous tutorial on how to install Node. js in CentOS 7.
First, create an empty folder. And create the following files:

  • Server. js
  • Package. json
  • Dockerfile
  • . Dockerignore

Mkdir docker_demo
Cd docker_demo
Touch server. js
Touch package. json
Touch Dockerfile
Touch. dockerignore

Then write data in server. js

Const Koa = require ('koa ');
Const app = new Koa ();

App. use (async ctx => {
Ctx. body = 'Hello docker ';
});

App. listen (3000 );

Write in package. json

{
"Name": "docker_demo ",
"Version": "0.1.0 ",
"Private": true,
"Scripts ":{
"Start": "node server. js"
},
"Dependencies ":{
"Koa": "^ 2.5.0"
}
}

Test Program. Console input

Npm start

Open the local test in the browser, if. Indicates that the demo is successfully created. Continue.

Create a dockerfile

  Dockerfile is a script composed of a series of commands and parameters. A Dockerfile contains the complete command to build the entire image. Docker uses docker build to execute a series of commands in Dockerfile to automatically build an image.
Write the code in the .doc kerignore file. Filter files of this type. Similar to git. gitignore

# Logs
Logs
*. Log
Npm-debug.log *

# Runtime data
Pids
*. Pid
*. Seed

# Directory for instrumented libs generated by jscoverage/JSCover
Lib-cov

# Coverage directory used by tools like istanbul
Coverage

# Nyc test coverage
. Nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
. Grunt

# Node-waf configuration
. Lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
Build/Release

# Dependency directories
Node_modules
Jspm_packages

# Optional npm cache directory
. Npm

# Optional REPL history
. Node_repl_history
. Idea
. Node_modules
Node_modules
. Vscode

Zookeeper writes the following code into the Dockerfile:

# Create a node image version
FROM node: 8.9-alpine
# Author Declaration
MAINTAINER robin
# Move the files in the current directory to the app directory
ADD./app/
# Enter the app directory, similar to cd
WORKDIR/app
# Install Dependencies
RUN npm install
# Externally exposed ports
EXPOSE 3000
# Program Startup Script
CMD ["npm", "start"]

Build an image

Use the build command to construct the image. Pay attention to the one that follows.

Docker build-t docker_demo. robin: docker_demo robin $ docker build-t docker_demo.
Sending build context to Docker daemon 39.94kB
Step 1/7: FROM node: 8.9-alpine
---> 406f227b21f5
Step 2/7: MAINTAINER robin
---> Using cache
---> 78d6cdbcfee2
Step 3/7: ADD./app/
---> 2cb30678612d
Step 4/7: WORKDIR/app
Removing intermediate container e51377081039
---> C2b7d0f37d2d
Step 5/7: RUN npm install
---> Running in da0c3946ca8d
Npm notice created a lockfile as package-lock.json. You shocould commit this file.
Added 38 packages in 3.323 s
Removing intermediate container da0c3946ca8d
---> Eecee87f10e2
Step 6/7: EXPOSE 3000
---> Running in f00003cc168a4
Removing intermediate container f00003cc168a4
---> 2671a4c6deb4
Step 7/7: CMD ["npm", "start"]
---> Running in dec529f754aa
Removing intermediate container dec529f754aa
---> 6ec74253d353
Successfully built 6ec73793d353
Successfully tagged docker_demo: latest

Wait until the image construction is complete. Run the images command to view the image.

At this point, we can see that images has been constructed. Start images and test.

# Start image-d indicates that the image is executed in the background,-p 9000: 3000 indicates that the local port 9000 is implicitly routed to port 3000 in the container, and docker_demo indicates the image name.
Docker run-d-p 9000: 3000 docker_demo
# View containers
Docker ps


In this case, the browser opens http: // localhost: 9000/, if. Indicates that the container is running normally.

Zookeeper cannot be opened locally at this time. You can use the log command to view logs. Modify the corresponding vertex according to the log.

Bind docke hub

The Zookeeper image already exists on our local hard disk. Can we upload the image to the server? There are usually two methods. We have been building our own image repository (such as harbor), and the official docke hub, similar to gitlab. Here we first use docke hub as the column. Add harbor to the next blog. First, you must register the docke hub account at the official website registration address. Then log in.

Robin: docker_demo robin $ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxxxxx
Password:
Login Succeeded
Robin: docker_demo robin $

After successful login. You need to give the current images. Add tag and push

# Format: docker tag Name username/tag Name
Docker tag docker_demo limit 365/docker_demo
# Docker push username/Tag Name
Docker push logs 365/docker_demo

After zookeeper is complete, you can log on to the docke hub to view the image you just pushed. We can set whether the image is made public. Public: All users can download it. Private: indicates that you can download the file by yourself.
Then, we have uploaded images to the docke hub. When we need this images, we can directly download and use it using the docker pull command.
Finally, this tutorial is complete. Various problems may occur during deployment in the actual project. We need to view more logs. Modify logs accordingly. In the future, I will also sort out some errors that may be easily caused by docke in deploying nodejs applications.

Dockerc Common commands

# View the current docker version
Docker-v
# View all local Images
Docker images
# Create an image using the docker build-t image name.
Docker build-t docker_demo.
# It is used to copy data between containers and hosts. Use the docker cp host file address in the container. 12d7f14v45cv is the container id.
Docker cp/www/runoob 12d7f14v45cv:/www/
# Create a new container and run it.-d is executed in the background,-p 9000: 3000 is followed by the host port, and then the container port. Docker_demo image name
Docker run-d-p 9000: 3000 docker_demo
# Start a stopped container
Docker start docker_demo
# Disabling started containers
Docker stop docker_demo
# Restart the container
Docker restart docker_demo
# Kill a running container.
Docker kill-s KILL docker_demo
# Delete one or more containers. -F: Use the SIGKILL signal to force the deletion of a running container-l: remove the network connection between containers, rather than the container itself-v:-v to delete the volume associated with the container
Docker rm-f docker_demo, docker_demo1
# Run the command in the running container. Container id of 104e28f2f072
Sudo docker exec-it restart e28f2f072/bin/bash
# List containers. -A: All containers contain
Docker ps
# Get the container's log 104e28f2f072 container id,-t: display the timestamp
Docker logs-f-t unzip e28f2f072
# Log on to the image repository
Docker login
# Getting Images
Docker pull
# Uploading images
Docker push
# View the creation history of the specified image.
Docker history docker_demo

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151318.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.