foreword
Minimal practice means opening up the link and looking at the problem at a higher level, the problem becomes clear. In this article, the author will take you to implement a simplest koa service and deploy it to the server.
Prerequisites: Some understanding of docker
Realize ideas
Step 1: Start a koa application first,
Step 2: Generate a dockerfile again and write it
Step 3: Generate an image based on dockerfile (docker build)
Step 4: Generate (run) a container based on this image and check whether it can run on the local machine
Step 5: If it works, export the image
Step 6: xshell connects to the server (assuming docker is installed on the server), import this image, and generate a container from this image
This is the minimal implementation
Here is another way to do it:
In the fifth step, publish it to the remote repository, and in the sixth step, pull it from the remote to the mirror, but the disadvantage is that your mirror needs to be public (only one private quota)
Step 1: Start a koa application first
Create a new dockerfile_koa_demo file and npm init -y to generate package.json. Download the koa package and create a new app.js
mkdir dockerfile_koa_demo
cd dockerfile_koa_demo
npm init -y
npm i koa --save
touch app.js
Write app.js again
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
ctx.body = 'hello, docker';
});
app.listen(3010, () => {
console.log('3010端口已启动');
});
Modify the script of package.json
...
"scripts": {
"start": "nodemon app.js"
},
...
startup script
npm run start
See the picture below:
Browser access is normal
Step 2: Write a Dockerfile
Create a new Dockerfile
touch Dockerfile
Write Dockerfile
# base image
# FROM
FROM node
# 复制文件到容器
ADD . /home/www
# 进入工作目录
WORKDIR /home/www
# 安装项目依赖包
RUN npm install --registry=https://registry.npm.taobao.org
# 暴露 端口
EXPOSE 3010
# 开始命令
CMD ["node", "./app.js"]
Finished the second step
Step 3: Generate an image based on dockerfile
In order to speed up the build, we first .dockerignore
and write node_modules
touch .dockerignore
Write ---87f052345f74784c731be7ff407a9c6f .dockerignore
in node_modules
, which means that when you docker build
node_modules
to speed up the build speed ---60cdb8ab214723d0a876
Generate an image through the command line and name it johan/koa_server:v1.0.0
docker build . -t johan/koa_server:v1.0.0
# -t 就是给镜像起名
Results as shown below:
Check if the mirror is available
docker images
Step 4: Generate a container based on this image
Generate a container from the command line
docker run -d --name koa_server_container -p 3010:3010 johan/koa_server:v1.0.0
# -d 后台运行
# --name 给容器起名字
# -p 本机端口隐射镜像中的端口
Check if the image is generated
docker ps -a
Enter http://localhost:3010/
in the browser, bingo
Step 5: Export this image
docker save johan/koa_server:v1.0.0 > koa_server.tar
Step 6: Run through the server
Here we assume that the docker and lrzsz packages have been installed on the server (this article will not expand)
Connect to the server through xshell and upload the tar package through the command rz
rz
Unpack this archive (import)
docker load < koa_server.tar
Build a container based on this container
docker run -d --name koa_server_container -p 3010:3010 johan/koa_server:v1.0.0
and look at the containers that are running through
docker ps -a
Enter the domain name in the browser, bingo
The above is a minimal implementation of dockerfile
, is it very simple. Of course, you can upload the image to docker hub
in the fifth step, and pull the image from dockerfile
ae44b0096cb5b8165c2affb82c0b74d8--- in the sixth step, and then generate the container. as follows
Another method Step 5: Upload the image
Log in to dockerhub from the command line (register first)
docker login
name the image
docker tag johan/koa_server:v1.0.0 johanbo/koa_server:v1.0.0
Upload the image to docker hub
docker push johanbo/koa_server:v1.0.0
Another method Step 6: Pull the image
Log in to the server and pull the image
docker pull johanbo/koa_server:v1.0.0
Note : You need to tag here, otherwise the latest will be pulled by default
start the container
docker run -d --name koa_server_container -p 3010:3010 johan/koa_server:v1.0.0
Enter the domain name in the browser, bingo
Note: The server development port needs to go to the cloud service provider to develop the corresponding port (security group configuration rules)
follow-up
You can configure the environment variables you need in dockerfile
. The container generated by the image is a "mini server", you can do whatever you want, it has nothing to do with the whole server, so your application is not affected by the environment.
Here is a pain point: even if you solve the environment problem, CICD is still a problem, how to integrate more quickly? In the next section, I will introduce the combination of docker and jenkins, generate jenkins through docker, and enable CICD with jenkins
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。