First, the preface
Some students may not understand Docker this project, Docker is written in the go language, a rapidly deployable lightweight virtual technology project, he allows developers to package their own programs and the operating environment, made into a Docker image (mirroring), so deployed to the server, Also only need to download this image can run the program, eliminates every time to install a variety of dependencies and the environment of trouble, but also to do the isolation between applications
II. Preparation for implementation
I will first create a simple Node.js web app to build a mirror. Then run a container based on this image. In order to achieve rapid deployment.
Because of the network reason my node.js mirror is downloading from the domestic mirror library, not the Docker Hub.
First from the domestic mirror site pull next Nodejs mirror.
Docker Pull hub.c.163.com/nce2/nodejs:0.12.2
After downloading to see our mirror, find his name, we will use the
Third, create Node.js program
Create Package.json and write related information and dependencies
{
"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
Write a simplest web based on the Express framework, return Hello Word. Note that we're listening for port 8888.
' Use strict ';
var express = 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 ');
Four, create Dockerfile
And then the main actor came. Create Dockerfile file This file is required to create a mirror
Docker will build a mirror according to Dockerfile's content. I'll give you the complete code first, and then a line to explain
From hub.c.163.com/nce2/nodejs:0.12.2
# Create app directory
RUN mkdir-p/home/service
workdir/home/ Service
# Bundle app source
COPY./home/service
RUN npm install
expose 8888
CMD ["NPM", "Start"]
We'll explain it in one sentence.
From hub.c.163.com/nce2/nodejs:0.12.2
From is the underlying source image of the build mirror, which is the name of the hub.c.163.com/nce2/nodejs:0.12.2
mirror, the image that we first pulled down from the domestic server. If you do not have Docker locally, you will pull mirroring yourself.
# Create App directory
RUN mkdir-p/home/service
workdir/home/service
The first word run is used to create a folder in image that will be used to save our code in the future.
The second sentence Workdir the folder we created as the working directory.
# Bundle App Source
COPY./home/service
RUN npm Install
Copy of the first sentence is to copy all the 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 our app needs.
Since our web app is listening to port 8888, we expose the port to the host so that I can access the Web from the outside.
Well, I'm sure I don't have to explain it to you to see what he does. Run the npm start
command, this command will run node service.js to
Start our web App.
V. Construction of Image
Run the following command in the directory where you dockerfile the file to build an image.
Docker build-t Mynodeapp.
Don't forget the last point.
Look at our mirrors when we're finished building
Six, run the mirror
Docker run-d-P 8888:8888 ac5
-D indicates that the container will run in the background,--------------------port mapping, 8888 of the local product map to the container 8888 port so the extranet can access our web through 8888 of the products.
The AC5 in the back is the ID of our image because the first 3 have been able to locate the image so I didn't write back.
View the ID of the container we just ran through Docker PS
Print log 7370 is our container ID, like the image ID, you can write it all, I am lazy on the top 4, already enough to identify the container
If you think container can execute the following commands, you can go into the inside and you will be able to operate like normal Linux. If you want to quit the Execute Exit command.
VII. Testing
Let's go through curl to see if we can access our web.
You can also look through the browser
Viii. Summary
All right, so this little tutorial is over, have you learned it? Hopefully this article will help you get started. If you have any questions or questions, you can exchange messages. Thank you for your support to the cloud-dwelling community.