docker的使用

举报
黄生 发表于 2022/06/25 16:20:47 2022/06/25
【摘要】 有一台ECS:CentOS Linux release 8.5.2111找到docker镜像,当然华为云的但是地址替换那一步不要做,否则会出现404 not found.按照步骤完成后,系统里就有了docker了。先来看一下基本命令,docker命令选项,常用的有ps, logs, exec, inspect, restart, status,run, network, rm, stop表中...

有一台ECS:CentOS Linux release 8.5.2111
找到docker镜像,当然华为云的
image.png
但是地址替换那一步不要做,否则会出现404 not found.
按照步骤完成后,系统里就有了docker了。
先来看一下基本命令,

  1. docker命令选项,常用的有ps, logs, exec, inspect, restart, status,run, network, rm, stop

    表中没有列出的

    1. inspect: Return low-level information on Docker objects

    2. status: Display a live stream of container(s) resource usage statistics

    3. exec一般是执行/bin/bash得到一个shell,加参数-it

      -i, --interactive Keep STDIN open even if not attached
      –privileged Give extended privileges to the command
      -t, --tty Allocate a pseudo-TTY

  2. docker-compose: Define and run multi-container applications with Docker.

image.png

  1. docker build>ship>run的流程
    image.png

  2. dockerfile常用指令
    image.png

  3. docker build命令参数
    image.png

以上可以参考 华为云云原生黄金课程02:容器技术基础介绍

来实际操作一下,先写一个dockfile:

FROM node
LABEL maintainer xx@yy.com
RUN git clone -q https://github.com/docker-in-practice/todo.git
WORKDIR todo
RUN npm install > /dev/null
EXPOSE 8000
CMD ["npm","start"]

然后构建镜像:

[root@ecs-d589 docker]# docker build .
Sending build context to Docker daemon  2.048kB # dockfile 是 175 个字节
Step 1/7 : FROM node
latest: Pulling from library/node
1339eaac5b67: Pull complete #这里用3个线程并发下载并解压
4c78fa1b9799: Pull complete
14f0d2bd5243: Pull complete
76e5964a957d: Pull complete
cc4bb1a04a94: Pull complete
eba57464a96a: Pull complete
1c6bdae10107: Pull complete
93c6143f0972: Pull complete
5681e060b2f8: Pull complete
Digest: sha256:5244663c5cc6392808aa1c4a78f90369e75c3d9e9a27589cffed0ae73d1f0815
Status: Downloaded newer image for node:latest
 ---> 057129cb5d6f
Step 2/7 : LABEL maintainer xx@yy.com
 ---> Running in 39ace661cee1
Removing intermediate container 39ace661cee1
 ---> c4d5730a8f0a
Step 3/7 : RUN git clone -q https://github.com/docker-in-practice/todo.git
 ---> Running in 317dc8d60932
Removing intermediate container 317dc8d60932
 ---> d280dc10b44f
Step 4/7 : WORKDIR todo
 ---> Running in a32d3847902c
Removing intermediate container a32d3847902c
 ---> 3f3d278a71ae
Step 5/7 : RUN npm install > /dev/null
 ---> Running in 015577985eb3
npm WARN EBADENGINE Unsupported engine {
#这里去掉了一些内容
npm notice
Removing intermediate container 015577985eb3
 ---> 63da8134d26e
Step 6/7 : EXPOSE 8000
 ---> Running in 1d0f54b1962c
Removing intermediate container 1d0f54b1962c
 ---> 8a628b0e38da
Step 7/7 : CMD ["npm","start"]
 ---> Running in 246843c89b90
Removing intermediate container 246843c89b90
 ---> 98fd87257843
Successfully built 98fd87257843

镜像就建立好了,但是不知道它放在了什么地方。接下来给他打个标签,这样比较好叫它,叫他todoapp
docker tag 98fd87257843 todoapp
然后把他跑起来看看
docker run -i -t -p 8000:8000 --name example1 todoapp
这里example1是容器的名字,todoapp是镜像的名字
输出如下:

todomvc-swarm@0.0.1 prestart
make all

npm install
npm WARN EBADENGINE Unsupported engine {

这里略掉一些信息

> todomvc-swarm@0.0.1 start
> node TodoAppServer.js

Swarm server started port 8000
^Cshutting down http-server...  #因为是前台运行,这里按了Ctrl-C中断
closing swarm host...
swarm host closed
npm ERR! path /todo
npm ERR! command failed
npm ERR! signal SIGINT
npm ERR! command sh -c node TodoAppServer.js

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-06-25T06_43_49_447Z-debug-0.log

看一下刚才运行的状态:

[root@ecs-d589 docker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                      PORTS     NAMES
3a265ddcb7e4   todoapp   "docker-entrypoint.s…"   3 minutes ago   Exited (1) 10 seconds ago             example1

重新启动容器,在后台运行,可惜,结果是启动失败了,因为docker ps没信息

[root@ecs-d589 docker]# docker start example1
example1

看了一下日志,里面从前台启动打印的日志内容开始展示,
docker logs -f example1

我也不知道为什么第二次报这些错呢?

> todomvc-swarm@0.0.1 start
> node TodoAppServer.js

node:internal/validators:227
    throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "cb" argument must be of type function. Received undefined
    at makeCallback (node:fs:199:3)
    at Object.rename (node:fs:1014:14)
    at FileStorage.rotateLog (/todo/node_modules/swarm/lib/FileStorage.js:122:16)
    at new FileStorage (/todo/node_modules/swarm/lib/FileStorage.js:25:10)
    at Object.<anonymous> (/todo/TodoAppServer.js:88:19)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v18.4.0

用docker diff来看一下镜像自容器化以来的文件的变化:

[root@ecs-d589 docker]# docker diff example1
C /todo
C /todo/package-lock.json
C /todo/node_modules
C /todo/node_modules/.package-lock.json
A /todo/dist
A /todo/dist/LocalTodoApp.app.js
A /todo/dist/TodoApp.app.js
A /todo/dist/react.min.js
A /todo/.swarm
A /todo/.swarm/TodoItem
A /todo/.swarm/TodoItem/FxqHo02+swarm~nodejs
A /todo/.swarm/TodoList
A /todo/.swarm/TodoList/FxqHo+swarm~nodejs
A /todo/.swarm/_log
C /root
C /root/.npm
C /root/.npm/_update-notifier-last-checked
C /root/.npm/_cacache
C /root/.npm/_cacache/index-v5
C /root/.npm/_cacache/index-v5/7d
C /root/.npm/_cacache/index-v5/7d/e8
C /root/.npm/_cacache/index-v5/7d/e8/5446c5cdae0493e4ad3b10347b77c8df4b17b5fbe14834f5c6b06faa9f95
#这里省略了一些内容
C /root/.npm/_logs
A /root/.npm/_logs/2022-06-25T06_43_49_447Z-debug-0.log
A /root/.npm/_logs/2022-06-25T06_43_49_811Z-debug-0.log
A /root/.npm/_logs/2022-06-25T06_47_36_551Z-debug-0.log
A /root/.npm/_logs/2022-06-25T06_47_36_940Z-debug-0.log

又start了一下,启动起来几秒就挂了

[root@ecs-d589 docker]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS         PORTS                                       NAMES
3a265ddcb7e4   todoapp   "docker-entrypoint.s…"   2 hours ago   Up 2 seconds   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   example1

就这样吧

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。