jackieli123723 / jackieli123723.github.io

✅lilidong 个人博客
9 stars 0 forks source link

docker 构建node服务 #58

Open jackieli123723 opened 6 years ago

jackieli123723 commented 6 years ago

docker 构建node微服务

Docker version 17.05.0-ce, build 89658be(基于该版本的构建) [root@instance-7aqka2xd hello-vue]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@instance-7aqka2xd hello-vue]# docker --version Docker version 17.05.0-ce, build 89658be

版本太低的话会导致如下问题:

例如:docker 1.13.1版本

docker build 的时候 step 走不完

生成none 的镜像

还有就是各种bug问题

服务启动不了等等

(是docker版本的问题 坑啊啊坑!!!!!!!!!!!!!!) (升级到最新的就好)

项目结构的源码

[root@instance-7aqka2xd hello-vue]# tree .
.
├── Dockerfile
├── index.js
└── package.json

0 directories, 3 files
[root@instance-7aqka2xd hello-vue]# 
[root@instance-7aqka2xd hello-vue]# cat index.js 
var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello vue!')
})

app.listen(8081, function () {
  console.log('app listening on port 8081!')
})
[root@instance-7aqka2xd hello-vue]# cat Dockerfile 
FROM registry.docker-cn.com/library/node (必须使用国内的镜像)
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node index.js

构建镜像

[root@instance-7aqka2xd hello-vue]# docker build -t hello-vue .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM registry.docker-cn.com/library/node
latest: Pulling from library/node
f2b6b4884fc8: Already exists 
4fb899b4df21: Already exists 
74eaa8be7221: Already exists 
2d6e98fe4040: Already exists 
452c06dec5fa: Already exists 
7b3c215894de: Already exists 
cc0e8883f3a1: Pull complete 
c2f5b6c538cd: Pull complete 
Digest: sha256:77179a1b2c4618d2d7abc292cd949b262e0b5d0b5443d00f0132c5209bc551de
Status: Downloaded newer image for registry.docker-cn.com/library/node:latest
 ---> 26cbfbc03e3f
Step 2/7 : WORKDIR /app
 ---> f8fe5a9f7002
Removing intermediate container 6eae648d1c37
Step 3/7 : COPY package.json /app
 ---> 13b20def19ce
Removing intermediate container 2c440f9d3e46
Step 4/7 : RUN npm install
 ---> Running in 3720b27548f9
added 50 packages in 13.488s
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN hello@1.0.0 No description
npm WARN hello@1.0.0 No repository field.

 ---> 1a336a453a2a
Removing intermediate container 3720b27548f9
Step 5/7 : COPY . /app
 ---> e667c3afef05
Removing intermediate container ce24c4ea54a3
Step 6/7 : CMD node index.js
 ---> Running in eda4d334d9e9
 ---> e486a4395ca5
Removing intermediate container eda4d334d9e9
Step 7/7 : EXPOSE 8082
 ---> Running in a91bff6c883a
 ---> 07fa74b8a462
Removing intermediate container a91bff6c883a
Successfully built 07fa74b8a462
Successfully tagged hello-vue:latest

docker 镜像列表

[root@instance-7aqka2xd hello-vue]# docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
hello-vue                             latest              07fa74b8a462        6 seconds ago       678MB
demo                                  latest              7cf2a0467b7b        9 minutes ago       675MB
registry.docker-cn.com/library/node   latest              26cbfbc03e3f        4 days ago          675MB
registry.docker-cn.com/library/node   carbon              4635bc7d130c        4 weeks ago         672MB

创建docker 容器

[root@instance-7aqka2xd hello-vue]# docker run -p 8082:8081 -d hello-vue
662cf64018ff76717f97f26dcaf8076e84a1bf76c8e41e3ca5cab89aaf43e761
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   8 seconds ago       Up 7 seconds        8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon

image

通过镜像可启动多个

[root@instance-7aqka2xd hello-vue]# docker run -p 8083:8081 -d hello-vue
93119059084ca246f7f53b3ac47b583c77ed4ff050d8204d3ab5b490a8857e51
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
93119059084c        hello-vue           "/bin/sh -c 'node ..."   7 seconds ago       Up 6 seconds        8082/tcp, 0.0.0.0:8083->8081/tcp   frosty_darwin
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   12 minutes ago      Up 12 minutes       8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd hello-vue]# 

image

docker 停止容器

[root@instance-7aqka2xd hello-vue]# docker stop 93119059084c
93119059084c
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                              NAMES
93119059084c        hello-vue           "/bin/sh -c 'node ..."   2 minutes ago       Exited (137) 3 seconds ago                                      frosty_darwin
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   14 minutes ago      Up 14 minutes                8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd hello-vue]# 

image

docker 重启容器

[root@instance-7aqka2xd hello-vue]# docker restart 93119059084c
93119059084c
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
93119059084c        hello-vue           "/bin/sh -c 'node ..."   4 minutes ago       Up 2 seconds        8082/tcp, 0.0.0.0:8083->8081/tcp   frosty_darwin
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   16 minutes ago      Up 16 minutes       8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd hello-vue]# 

docker 改为npm script 命令可切换dev/qa/prod变量打包上线容器部署

#CMD node index.js
#EXPOSE 8082
改为
CMD [ "npm", "start" ]
EXPOSE 8082

---

[root@instance-7aqka2xd hello-vue]# docker build -t hello-vue2 .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM registry.docker-cn.com/library/node
 ---> 26cbfbc03e3f
Step 2/7 : WORKDIR /app
 ---> Using cache
 ---> f8fe5a9f7002
Step 3/7 : COPY package.json /app
 ---> Using cache
 ---> 13b20def19ce
Step 4/7 : RUN npm install
 ---> Using cache
 ---> 1a336a453a2a
Step 5/7 : COPY . /app
 ---> 1a5c8907d30b
Removing intermediate container 22a6c491fa6d
Step 6/7 : CMD npm start
 ---> Running in 53bd722f5baf
 ---> 12c49a785d19
Removing intermediate container 53bd722f5baf
Step 7/7 : EXPOSE 8082
 ---> Running in 5f5fac8ba4b6
 ---> dcc402f6640c
Removing intermediate container 5f5fac8ba4b6
Successfully built dcc402f6640c
Successfully tagged hello-vue2:latest
[root@instance-7aqka2xd hello-vue]# docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
hello-vue2                            latest              dcc402f6640c        6 seconds ago       678MB
hello-vue                             latest              07fa74b8a462        33 minutes ago      678MB
demo                                  latest              7cf2a0467b7b        42 minutes ago      675MB
registry.docker-cn.com/library/node   latest              26cbfbc03e3f        4 days ago          675MB
registry.docker-cn.com/library/node   carbon              4635bc7d130c        4 weeks ago         672MB
[root@instance-7aqka2xd hello-vue]# docker run -p 8085:8081 hello-vue2 (注意这里没有-d 参数 关闭shell终端的话就会导致 退出 docker ps -a  status == Exited (0) 15)

> hello@1.0.0 start /app
> node index.js

app listening on port 8081!
^C^C[root@instance-7aqka2xd hello-vue]# ^C
[root@instance-7aqka2xd hello-vue]# docker images 
REPOSITORY                            TAG                 IMAGE ID            CREATED              SIZE
hello-vue2                            latest              dcc402f6640c        About a minute ago   678MB
hello-vue                             latest              07fa74b8a462        34 minutes ago       678MB
demo                                  latest              7cf2a0467b7b        44 minutes ago       675MB
registry.docker-cn.com/library/node   latest              26cbfbc03e3f        4 days ago           675MB
registry.docker-cn.com/library/node   carbon              4635bc7d130c        4 weeks ago          672MB
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                              NAMES
1ac3573df59d        hello-vue2          "npm start"              36 seconds ago      Exited (0) 15 seconds ago                                      wonderful_hamilton
5648ea673a54        hello-vue           "/bin/sh -c 'node ..."   15 minutes ago      Up 15 minutes               8082/tcp, 0.0.0.0:8083->8081/tcp   relaxed_lovelace
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   34 minutes ago      Up 34 minutes               8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd hello-vue]# docker run -p 8085:8081 -d  hello-vue2 
60d2b9aee2361b0496f3112cbb95a97916ad0d63336543710555dbe1ce90095f
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS                              NAMES
60d2b9aee236        hello-vue2          "npm start"              2 seconds ago        Up 2 seconds                8082/tcp, 0.0.0.0:8085->8081/tcp   affectionate_hopper
1ac3573df59d        hello-vue2          "npm start"              About a minute ago   Exited (0) 48 seconds ago                                      wonderful_hamilton
5648ea673a54        hello-vue           "/bin/sh -c 'node ..."   16 minutes ago       Up 16 minutes               8082/tcp, 0.0.0.0:8083->8081/tcp   relaxed_lovelace
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   34 minutes ago       Up 34 minutes               8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd hello-vue]# docker rm 1ac3573df59d
1ac3573df59d
[root@instance-7aqka2xd hello-vue]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
60d2b9aee236        hello-vue2          "npm start"              28 seconds ago      Up 27 seconds       8082/tcp, 0.0.0.0:8085->8081/tcp   affectionate_hopper
5648ea673a54        hello-vue           "/bin/sh -c 'node ..."   16 minutes ago      Up 16 minutes       8082/tcp, 0.0.0.0:8083->8081/tcp   relaxed_lovelace
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   35 minutes ago      Up 34 minutes       8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd hello-vue]# 

image

进入不同容器containerID

[root@instance-7aqka2xd ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
60d2b9aee236        hello-vue2          "npm start"              14 hours ago        Up 14 hours         8082/tcp, 0.0.0.0:8085->8081/tcp   affectionate_hopper
5648ea673a54        hello-vue           "/bin/sh -c 'node ..."   15 hours ago        Up 15 hours         8082/tcp, 0.0.0.0:8083->8081/tcp   relaxed_lovelace
662cf64018ff        hello-vue           "/bin/sh -c 'node ..."   15 hours ago        Up 15 hours         8082/tcp, 0.0.0.0:8082->8081/tcp   pensive_shannon
[root@instance-7aqka2xd ~]# sudo docker exec -it 60d2b9aee236  /bin/bash 
root@60d2b9aee236:/app# ls
Dockerfile  index.js  node_modules  package-lock.json  package.json
root@60d2b9aee236:/app# cat Dockerfile 
FROM registry.docker-cn.com/library/node
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
#CMD node index.js
#EXPOSE 8082
CMD [ "npm", "start" ]
EXPOSE 8082
EXPOSE 8082root@60d2b9aee236:/app# exit
exit
[root@instance-7aqka2xd ~]# sudo docker exec -it 60d2b9aee236  /bin/bash 
root@60d2b9aee236:/app# ^C
root@60d2b9aee236:/app# exit
exit
[root@instance-7aqka2xd ~]# sudo docker exec -it 5648ea673a54  /bin/bash 
root@5648ea673a54:/app# ll
bash: ll: command not found
root@5648ea673a54:/app# ls
Dockerfile  index.js  node_modules  package-lock.json  package.json
root@5648ea673a54:/app# cat Dockerfile 
FROM registry.docker-cn.com/library/node
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node index.js
EXPOSE 8082root@5648ea673a54:/app# 
jackieli123723 commented 6 years ago

CentOS7升级最新版本Docker(需要最新的docker否则你要踩坑无数)

首先升级一下系统包 (可以跳过) $ sudo yum update

卸载旧版本的 Docker $ sudo yum erase docker docker-common docker-client docker-compose

添加 Docker 的 yum 源 创建一个 /etc/yum.repos.d/docker.repo 文件,内容如下:

[dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg

安装新版本 Docker $ sudo yum install -y docker-engine

安装完后设置为系统开机自动启动服务

$ sudo systemctl enable docker.service

启动服务

$ sudo systemctl start docker

测试一下

docker info

升级过程常见的错误

[root@instance-7aqka2xd default]# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

[root@instance-7aqka2xd run]# docker info
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
或
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.错误1

使用
Remove /var/lib/docker (rm -rf /var/lib/docker). Restart Docker solved the problem.

效果
[root@instance-7aqka2xd run]# rm -rf /var/lib/docker
[root@instance-7aqka2xd run]# service docker start
Redirecting to /bin/systemctl start docker.service

[root@instance-7aqka2xd run]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@instance-7aqka2xd home]# docker --version
Docker version 17.05.0-ce, build 89658be
jackieli123723 commented 6 years ago

docker push 镜像到hub.docker.com


[root@instance-7aqka2xd ~]# 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: jackieli123456
Password: 
Login Succeeded
[root@instance-7aqka2xd ~]# 

[root@instance-7aqka2xd node-docker]# cd hello-vue/
[root@instance-7aqka2xd hello-vue]# ll
total 12
-rw-r--r-- 1 root root 171 May  2 19:58 Dockerfile
-rw-r--r-- 1 root root 196 Apr 27 10:06 index.js
-rw-r--r-- 1 root root 281 May  2 16:50 package.json
[root@instance-7aqka2xd hello-vue]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
docker-node-server_my-node-server       latest              1367fc30f732        3 hours ago         431MB
my-node-server                          latest              ecffa79c850d        3 hours ago         431MB
hello-vue2                              latest              dcc402f6640c        18 hours ago        678MB
hello-vue                               latest              07fa74b8a462        19 hours ago        678MB
demo                                    latest              7cf2a0467b7b        19 hours ago        675MB
registry.docker-cn.com/library/ubuntu   latest              452a96d81c30        5 days ago          79.6MB
registry.docker-cn.com/library/node     latest              26cbfbc03e3f        5 days ago          675MB
registry.docker-cn.com/library/node     carbon              4635bc7d130c        4 weeks ago         672MB
[root@instance-7aqka2xd hello-vue]# docker build -t jackieli123456/hello-vue .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM registry.docker-cn.com/library/node
 ---> 26cbfbc03e3f
Step 2/7 : WORKDIR /app
 ---> Using cache
 ---> f8fe5a9f7002
Step 3/7 : COPY package.json /app
 ---> Using cache
 ---> 13b20def19ce
Step 4/7 : RUN npm install
 ---> Using cache
 ---> 1a336a453a2a
Step 5/7 : COPY . /app
 ---> Using cache
 ---> 1a5c8907d30b
Step 6/7 : CMD npm start
 ---> Using cache
 ---> 12c49a785d19
Step 7/7 : EXPOSE 8082
 ---> Using cache
 ---> dcc402f6640c
Successfully built dcc402f6640c
Successfully tagged jackieli123456/hello-vue:latest
[root@instance-7aqka2xd hello-vue]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
docker-node-server_my-node-server       latest              1367fc30f732        3 hours ago         431MB
my-node-server                          latest              ecffa79c850d        3 hours ago         431MB
jackieli123456/hello-vue                latest              dcc402f6640c        18 hours ago        678MB
hello-vue2                              latest              dcc402f6640c        18 hours ago        678MB
hello-vue                               latest              07fa74b8a462        19 hours ago        678MB
demo                                    latest              7cf2a0467b7b        19 hours ago        675MB
registry.docker-cn.com/library/ubuntu   latest              452a96d81c30        5 days ago          79.6MB
registry.docker-cn.com/library/node     latest              26cbfbc03e3f        5 days ago          675MB
registry.docker-cn.com/library/node     carbon              4635bc7d130c        4 weeks ago         672MB
[root@instance-7aqka2xd hello-vue]# docker push jackieli123456/hello-vue:latest
The push refers to a repository [docker.io/jackieli123456/hello-vue]
341ffb164d92: Pushed 
5f8e08491715: Pushed 
d32c0e689a72: Pushed 
63c722598511: Pushed 
bda9895de7e8: Pushed 
3d5c4fdcdf7f: Pushed 
33aed7748ee3: Pushed 
0e0b4ee1c6dc: Pushed 
ff57bdb79ac8: Pushed 
6e5e20cbf4a7: Pushed 
86985c679800: Pushed 
8fad67424c4e: Pushed 
latest: digest: sha256:88389a44d230c90058bcb1926d0be1ec490d518bb02ef2ec15297a63cbf1f0e9 size: 2838
[root@instance-7aqka2xd hello-vue]# 

image

拉取官方和中国镜像

FROM registry.docker-cn.com/library/node

docker pull jackieli123456/hello-vue (官方)
docker pull registry.docker-cn.com/jackieli123456/hello-vue (中国镜像)

[root@instance-7aqka2xd hello-vue]# docker pull registry.docker-cn.com/jackieli123456/hello-vue
Using default tag: latest
latest: Pulling from jackieli123456/hello-vue
Digest: sha256:88389a44d230c90058bcb1926d0be1ec490d518bb02ef2ec15297a63cbf1f0e9
Status: Downloaded newer image for registry.docker-cn.com/jackieli123456/hello-vue:latest
[root@instance-7aqka2xd hello-vue]# docker pull jackieli123456/hello-vue
Using default tag: latest
latest: Pulling from jackieli123456/hello-vue
Digest: sha256:88389a44d230c90058bcb1926d0be1ec490d518bb02ef2ec15297a63cbf1f0e9
Status: Downloaded newer image for jackieli123456/hello-vue:latest
[root@instance-7aqka2xd hello-vue]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
docker-node-server_my-node-server                 latest              1367fc30f732        4 hours ago         431MB
my-node-server                                    latest              ecffa79c850d        4 hours ago         431MB
jackieli123456/hello-vue                          latest              dcc402f6640c        19 hours ago        678MB
hello-vue2                                        latest              dcc402f6640c        19 hours ago        678MB
registry.docker-cn.com/jackieli123456/hello-vue   latest              dcc402f6640c        19 hours ago        678MB
hello-vue                                         latest              07fa74b8a462        20 hours ago        678MB
demo                                              latest              7cf2a0467b7b        20 hours ago        675MB
registry.docker-cn.com/library/ubuntu             latest              452a96d81c30        5 days ago          79.6MB
registry.docker-cn.com/library/node               latest              26cbfbc03e3f        5 days ago          675MB
registry.docker-cn.com/library/node               carbon              4635bc7d130c        4 weeks ago         672MB
[root@instance-7aqka2xd hello-vue]#