docker学习(4) DevOps视角
纵观Docker
运维(Ops)视角
开发(Dev)视角
运维包括:下载镜像、运行新的容器,在容器内运行命令、以及销毁容器
开发包括:与应用相关的内容,比如从github拉取一些应用,解释其中的Dockerfile,将应用容器化,在容器里运行他们。
- 快速启动Docker的方式:Play With Docker:http://play-with-docker.com/
运维视角
- 安装Docker涉及两个组将:Docker客户端和Docker daemon
- daemon实现了Docker引擎的API
- Linux默认安装的视角,客户端和daemon之间的通信是通过本地IPC/UNIX Socker完成的(/var/run/docker.sock)
- 使用docker version命令来检查客户端和服务端是否已经成功运行,并且可以相互通信。
$ docker version
Client: Docker Engine - Community
Version: 19.03.4
API version: 1.40
Go version: go1.12.10
Git commit: 9013bf583a
Built: Fri Oct 18 15:53:51 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.4
API version: 1.40 (minimum version 1.12)
Go version: go1.12.10
Git commit: 9013bf583a
Built: Fri Oct 18 15:52:23 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
镜像
- 可以将Docker镜像理解为一个包含OS文件系统和应用的对象。
- 在Docker世界里,镜像实际上等价于未启动的容器。
- 查看镜像
~$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
- 获取一个新镜像——ubuntu:latest
$ docker pull ubuntu:latest
latest: Pulling from library/ubuntu
22e816666fd6: Pull complete
079b6d2a1e53: Pull complete
11048ebae908: Pull complete
c58094023a2e: Pull complete
Digest: sha256:a7b8b7b33e44b123d7f997bd4d3d0a59fafc63e203d17efedf09ff3f6f516152
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
lhf@lhf-virtual-machine:~$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cf0f3ca922e0 4 days ago 64.2MB
- Docker每个镜像都有自己的唯一ID
- 用户可以通过镜像的ID和名称来使用镜像
容器
- 启动容器
$ docker container run -it ubuntu:latest /bin/bash
root@19e6da84f9c4:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
docker container run命令使Docker daemon启动新的容器
-it 参数将Shell切换到容器终端,开启容器的交互模式,使当前的shell连接到容器的终端
使用ps查看容器的进程
root@19e6da84f9c4:/# ps -elf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 4627 wait 14:38 pts/0 00:00:00 /bin/bash
4 R root 11 1 0 80 0 - 8600 - 14:56 pts/0 00:00:00 ps -elf
- 按Ctrl-PQ组合键,可以退出容器的同时来保持容器的运行。
- 查看系统内全部运行状态的容器
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19e6da84f9c4 ubuntu:latest "/bin/bash" 22 minutes ago Up 22 minutes unruffled_hoover
连接到运行中的容器
- 连接到运行中的容器
$ docker container exec -it unruffled_hoover bash
root@19e6da84f9c4:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
- 使用命令docker container stop 和docker container rm来停止并杀死容器
$ docker container stop unruffled_hoover
unruffled_hoover
lhf@lhf-virtual-machine:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19e6da84f9c4 ubuntu:latest "/bin/bash" 28 minutes ago Exited (0) 44 seconds ago unruffled_hoover
lhf@lhf-virtual-machine:~$ docker container rm unruffled_hoover
unruffled_hoover
lhf@lhf-virtual-machine:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- -a 的参数让Docker列出所有容器,包括已经停止的状态的容器。
开发视角
- 容器及应用
- 分析一份应用代码的Dockefile并容器化,最终以容器的方式运行。
$ ll psweb-master/
总用量 36
drwxrwxr-x 4 lhf lhf 4096 10月 23 23:11 ./
drwxrwxr-x 6 lhf lhf 4096 10月 23 23:11 ../
-rw-rw-r-- 1 lhf lhf 341 10月 23 23:11 app.js
-rw-rw-r-- 1 lhf lhf 216 10月 23 23:11 circle.yml
-rw-rw-r-- 1 lhf lhf 338 10月 23 23:11 Dockerfile
-rw-rw-r-- 1 lhf lhf 421 10月 23 23:11 package.json
-rw-rw-r-- 1 lhf lhf 370 10月 23 23:11 README.md
drwxrwxr-x 2 lhf lhf 4096 10月 23 23:11 test/
drwxrwxr-x 2 lhf lhf 4096 10月 23 23:11 views/
- 查看dockerfile内容
$ cat Dockerfile
# Test web-app to use with Pluralsight courses and Docker Deep Dive book
# Linux x64
FROM alpine
LABEL maintainer="[email protected]"
# Install Node and NPM
RUN apk add --update nodejs nodejs-npm
# Copy app to /src
COPY . /src
WORKDIR /src
# Install dependencies
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]
- 根据Dockerfile中的指令来创建新的容器
$ docker build -t test:latest .
Sending build context to Docker daemon 10.24kB
Step 1/8 : FROM alpine
latest: Pulling from library/alpine
89d9c30c1d48: Pull complete
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Downloaded newer image for alpine:latest
---> 965ea09ff2eb
Step 2/8 : LABEL maintainer="[email protected]"
---> Running in 75106e6bad84
Removing intermediate container 75106e6bad84
---> 5935d67da60e
Step 3/8 : RUN apk add --update nodejs nodejs-npm
---> Running in 56b0d36a8f41
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/8) Installing ca-certificates (20190108-r0)
(2/8) Installing c-ares (1.15.0-r0)
(3/8) Installing libgcc (8.3.0-r0)
(4/8) Installing http-parser (2.9.2-r0)
(5/8) Installing libstdc++ (8.3.0-r0)
(6/8) Installing libuv (1.29.1-r0)
(7/8) Installing nodejs (10.16.3-r0)
(8/8) Installing npm (10.16.3-r0)
Executing busybox-1.30.1-r2.trigger
Executing ca-certificates-20190108-r0.trigger
OK: 59 MiB in 22 packages
Removing intermediate container 56b0d36a8f41
---> dc254500dcda
Step 4/8 : COPY . /src
---> 6c293be80bd1
Step 5/8 : WORKDIR /src
---> Running in ebc1257e0d2c
Removing intermediate container ebc1257e0d2c
---> 4f515774097f
Step 6/8 : RUN npm install
---> Running in 48bb066e51b2
npm WARN deprecated [email protected]: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released.
npm WARN deprecated [email protected]: Please use the native JSON object instead of JSON 3
> [email protected] postinstall /src/node_modules/core-js
> node postinstall || echo "ignore"
Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!
The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock
Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)
npm notice created a lockfile as package-lock.json. You should commit this file.
added 163 packages from 460 contributors and audited 262 packages in 29.419s
found 5 vulnerabilities (2 low, 2 moderate, 1 critical)
run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 48bb066e51b2
---> 677c10bc6201
Step 7/8 : EXPOSE 8080
---> Running in 2ab783cd6ca3
Removing intermediate container 2ab783cd6ca3
---> a3656648909c
Step 8/8 : ENTRYPOINT ["node", "./app.js"]
---> Running in 7495a0f5807a
Removing intermediate container 7495a0f5807a
---> e63fd667d16a
Successfully built e63fd667d16a
Successfully tagged test:latest
- 查看当前镜像
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest e63fd667d16a About a minute ago 71.4MB
alpine latest 965ea09ff2eb 46 hours ago 5.55MB
ubuntu latest cf0f3ca922e0 4 days ago 64.2MB
- 从镜像启动容器,并测试应用
$ docker container run -d --name web1 --publish 8080:8080 test:latest
6906f6b41b395cf0e35a1dad541beb827830e8584124ae2e8c723bfc97262570
- 登录网页查看测试
- 将应用代码构建到Dockerfile——docker镜像当中,已容器的方式启动镜像,这个过程叫做“应用容器化”