1. Preface
There may be some students who don't understand the docker project. Docker is written in the Go language. It is a lightweight virtual technology project that is quickly deployed. It allows developers to package their programs and run environments together and make them into a docker image (mirror). In this way, deploy them on the server. You only need to download this image to run the program, avoiding the hassle of installing various dependencies and environments every time, and also achieving isolation between applications.
2. Realize preparation
I will first create a simple Node.js web app to build a mirror. Then run a container based on this Image. This enables rapid deployment.
Due to network reasons, my Node.js image was downloaded from the domestic image library, not Docker Hub.
Let’s first pull the nodejs image from the domestic mirror website.
docker pull hub.c.163.com/nce2/nodejs:0.12.2
After downloading, check our image and find its name. We will use it later.
3. Create Node.js program
Create package.json and write related information and dependencies
vi package.json
{ "name": "webtest", "version": "1.0.0", "description": "Node.js on Docker", "author": "lpxxn", "main": "server.js", "scripts": { "start": "node server.js" }, "dependencies": { "express": "^4.13.3" }}Create server.js
vi server.js
Write the simplest web This web is based on the express framework and returns Hello word. Note that we are listening to port 8888
'use strict';var expression = require('express');var PORT = 8888;var app = express();app.get('/', function (req, res) { res.send('Hello world/n');});app.listen(PORT);console.log('Running on http://localhost:' + PORT);4. Create a Dockerfile
Next, the protagonist takes the stage to create the Dockerfile file. This file is a necessary file for creating the image.
vi Dockerfile
Docker will build an image according to the contents of the Dockerfile. I'll give the complete code first, and then explain it one by one line
FROM hub.c.163.com/nce2/nodejs:0.12.2# Create app directoryRUN mkdir -p /home/ServiceWORKDIR /home/Service# Bundle app sourceCOPY . /home/ServiceRUN npm installEXPOSE 8888CMD [ "npm", "start" ]
Let's explain it sentence by sentence
FROM hub.c.163.com/nce2/nodejs:0.12.2
FROM is the basic source image for building the image. hub.c.163.com/nce2/nodejs:0.12.2 is the name of the image, which is the Image we pulled down from the domestic server at the beginning. If there is no Docker locally, it will pull the image itself.
# Create app directoryRUN mkdir -p /home/ServiceWORKDIR /home/Service
The first sentence RUN is used to create a folder in Image and will be used to save our code in the future.
The second sentence, WORKDIR, is to use the folder we created as a working directory.
# Bundle app sourceCOPY . /home/ServiceRUN npm install
The first sentence COPY is to copy all files in the current directory of the machine to the /home/Service folder of Image.
The second sentence RUN uses npm to install all the dependencies required for our app.
EXPOSE 8888
Since our web app listens for port 8888, we expose this port to the host so that I can access the web from the outside.
CMD [ "npm", "start" ]
I believe I can tell what he does without explaining it. Run the npm start command, this command will run node service.js
Start our web app.
5. Build Image
Run the following command in the directory where your Dockerfile is located to build an Image.
docker build -t mynodeapp .
Don't forget the last point
After building, check out our image
6. Run the mirror
docker run -d -p 8888:8888 ac5
-d indicates that the container will run in the background, -p indicates port mapping, map the 8888 products of the local machine to the 8888 port of the container so that the external network can access our web through the 8888 products of the local machine.
The ac5 behind is our Image ID. Because the first 3 can already locate this Image, I did not write down the following.
Check the ID of the Container we just ran via docker ps
Print log 7370 is our Container ID. Just like Image ID, you can write it all. I am lazy and write the first 4 digits, which is enough to identify this Container.
docker logs 7350
If you think of the following commands in Container, you can operate on ordinary Linux after entering it. If you want to exit, you can execute the exit command.
7. Test
Let’s first check whether we can access our web through curl.
curl -i localhost:8888
You can also check it out through the browser
8. Summary
Okay, this tutorial is over here. Have you learned it? Hope this article helps you get started. If you have any questions or questions, you can leave a message to communicate. Thank you for your support to Wulin.com.