《Docker Deep Dive》Note - 纵观 Docker

《Docker Deep Dive》Note

由于GFW的隔离,国内拉取镜像会报TLS handshake timeout的错误;需要配置 registry-mirrors 为国内源解决这个问题。
可以配置为阿里的加速源:https://cr.console.aliyun.com/undefined/instances/mirrors,阿里的加速器可以提升获取Docker官方镜像的速度。
登录开发者账号后,将自己的加速器地址复制到 Docker Settings > Daemon > Registry mirrors 中,并点击 Apply 按钮,等待 Docker 重启完成即可。

纵观 Docker

1. 运维视角

1.1 镜像
  • docker pull
  • docker image ls
# 拉取一个镜像
$ docker pull ubuntu:lastest

latest: Pulling from library/ubuntu
7ddbc47eeb70: Pull complete                                                                                             
c1bbdc448b72: Pull complete                                                                                            
8c3b70e39044: Pull complete                                                                                            
45d437916d57: Pull complete                                                                                             
Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status: Downloaded newer image for ubuntu:latest

# 查看拉取的镜像
$ docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              775349758637        9 days ago          64.2MB
1.2 容器
  • docker container run
  • docker container ls
  • 快捷键 Ctrl+P+Q
# 启动容器
# -it 参数会将 Shell 切换到容器终端
$ docker container run -it ubuntu:latest /bin/bash

root@90eb9f237521:/#

Ctrl + P + Q组合键,可以在退出容器的同时还保持容器运行。

# windows下可以通过 tasklist 命令查看进程
$ tasklist /FI "imagename eq docker*"

映像名称                                        PID            会话名                       会话#                 内存使用
========================= ======== ================ =========== ============
Docker for Windows.exe                   13972         Console                     5                      102,560 K
Docker.Watchguard.exe                    3772           Services                     0                      2,252 K
Docker.Watchguard.exe                    4032           Services                     0                      2,228 K

# 查看系统内全部处于运行状态的容器
$ docker container ls

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
90eb9f237521        ubuntu:latest       "/bin/bash"         21 minutes ago      Up 21 minutes                           focused_shaw
1.3 连接到运行中的容器
  • docker container exec
  • docker container stop
  • docker container rm
  • docker container ls -a
# 将 shell 连接到一个运行中的容器终端
# docker container exec <options> <cotainer-name or container-id> <command/app>
$ docker container exec -it 90eb9f237521 bash

root@90eb9f237521:/# 

# 停止一个容器
$ docker container stop 90eb9f237521

90eb9f237521

# 杀死一个容器
$ docker container rm 90eb9f237521

90eb9f237521

# 再次执行 docker container ls 可以查看容器是否已经被删除
# -a 参数可以列出所有(包括停止状态)容器
$ docker container ls -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# 可以看到容器 90eb9f237521 已经被删除了

2 开发视角

容器即应用。

  • docker image build
# 构建一个镜像
$ docker image build -t test:lastest .

...
Successfully built afe9c1f8a70f
Successfully tagged test:lastest

# 查看已经构建的镜像
$ docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                lastest             afe9c1f8a70f        44 seconds ago      71.5MB

# 应用容器化
$ docker container run -d --name web1 --publish 8080:8080 test:lastest

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
946d5306bb5e        test:lastest        "node ./app.js"     13 seconds ago      Up 12 seconds       0.0.0.0:8080->8080/tcp   web1

# 此时打开浏览器 localhost:8080 即可看到运行的应用

The end.
Last updated by Jehorn 11/10 2019

posted @ 2019-11-10 16:20  古宝只  阅读(393)  评论(0编辑  收藏  举报