fudonghai

导航

 

一,课时1:第一堂“云原生”课

 

二,课时2:容器基本概念

1.已运行 docker run -d -t —name demo ubuntu top 命令, 是否可以在 demo 这个容器内部停止容器?

 

2.已运行 docker run -d —name demo busybox:1.25 top 命令,如何使用 docker 命令来获取容器 demo 的 Init 进程 PID?

A. docker inspect demo -f '{{.State.Pid}}'

 

3.以下哪个 docker 命令创建出来的容器可以自动重启?

C. docker run -d --restart always busybox top

 

4.已运行 docker run -d -t —name demo ubuntu top 和 docker run --name demo-x --pid container:demo ubuntu ps 命令,是否可以在 demo-x 容器内部停止容器?

A. 是

 

5.已知容器 Init 进程 PID,在宿主机上通过 kill -9 PID 的方式结束该进程,容器当前的状态是什么?

A. Exited

 

6.已运行 docker run -d -t —name demo ubuntu top 和 docker run --name demo-x --pid container:demo ubuntu ps 命令,如果 demo 容器退出了,正在运行的 demo-x 容器是否会退出?

A. 是

 

7.已运行 docker run -d -t —name demo ubuntu top 命令, 在 demo 这个容器内看到 top 命令的 PID 是什么?

B. 1

 

8.以下哪个 docker 命令可以用来创建一个使用宿主机主机名的容器?

A. docker run --uts=host ubuntu hostname

 

9.已运行 docker run -d -t —name demo ubuntu top 命令,以下哪个 docker 命令创建出的容器能看见 demo 容器进程?

B. docker run --name demo-x --pid container:demo ubuntu ps

 

10.如何快速判断 docker daemon 是否支持动态接管运行容器?

 

课后实验

1.1 实验资源
# lsb_release -a
LSB Version:    core-9.20160110ubuntu0.2-amd64:core-9.20160110ubuntu0.2-noarch:security-9.20160110ubuntu0.2-amd64:security-9.20160110ubuntu0.2-noarch
Distributor ID:    Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:    xenial
# free -m
              total        used        free      shared  buff/cache   available
Mem:           7821         101        6710           0        1008        7437
Swap:             0           0           0
# uname -a
Linux iZbp1ed56c9f3xy81pl2m8Z 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 x
# cat /proc/cpuinfo 
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 85
model name    : Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
stepping    : 4
省略其他核信息
# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b1b45

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83875364    41936658+  83  Linux
[root@iZbp1ed56c9f3xy81pl2m8Z ~]# free
              total        used        free      shared  buff/cache   available
Mem:        8008856      104780     6871428         472     1032648     7615876
Swap:             0           0           0

 

 
1.2 登录阿里云ECS,安装docker环境

通过实验资源中ECS的IP和密码,登录远程ECS机器。

安装Docker。

sudo yum update -y
sudo yum install docker -y

sudo systemctl start docker

验证 docker 是否安装成功并在容器中执行一个测试的镜像。

sudo docker run hello-world
 

 提示信息

Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:5f179596a7335398b805f036f7e8561b6f0e32cd30a32f5e19d17a3cda6cc33d
Status: Downloaded newer image for docker.io/hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

 

1.3 准备应用代码和Dockerfile

在ECS上生成一个文件夹‘demo’,将 golang 代码拷贝到 `demo` 文件夹下的 `main.go`

package main

import (
        "fmt"
        "net/http"
)

func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Hello! World\n")
        })

        fmt.Println("start to serve...")
        http.ListenAndServe(":80", nil)
}

在当前 `demo` 目录下编写 Dockerfile ,如下所示(无需在本地执行操作)

FROM golang:1.12-alpine

# change current working dir
WORKDIR /go/src/app

# copy main.go into /go/src/app
COPY . .

# go build and install the app
RUN go install -v ./...

# run the app by default
CMD ["app"]

 

1.4构建镜像

通常情况下,使用以下命令即可构建镜像

# docker build . -t demo:v1

显示信息

Sending build context to Docker daemon 3.072 kB
Step 1/5 : FROM golang:1.12-alpine
Trying to pull repository docker.io/library/golang ... 
1.12-alpine: Pulling from docker.io/library/golang
e7c96db7181b: Pull complete 
5297bd381816: Pull complete 
3a664477889c: Pull complete 
bb89ac7faaa9: Pull complete 
8bb43bd9c21d: Pull complete 
Digest: sha256:06ba1dae97f2bf560831497f8d459c68ab75cc67bf6fc95d9bd468ac259c9924
Status: Downloaded newer image for docker.io/golang:1.12-alpine
 ---> c7330979841b
Step 2/5 : WORKDIR /go/src/app
 ---> f0ec88674b57
Removing intermediate container 34bb5acd4139
Step 3/5 : COPY . .
 ---> 84d85454cd6b
Removing intermediate container c7b3608f6593
Step 4/5 : RUN go install -v ./...
 ---> Running in ace6f449ee12

app
 ---> c5c206239f84
Removing intermediate container ace6f449ee12
Step 5/5 : CMD app
 ---> Running in baa4131f9ac8
 ---> 7d4981d0a5eb
Removing intermediate container baa4131f9ac8
Successfully built 7d4981d0a5eb

 

使用docker images查看本机镜像

# docker images

显示信息

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
demo                    v1                  7d4981d0a5eb        2 minutes ago       358 MB
docker.io/golang        1.12-alpine         c7330979841b        3 days ago          350 MB
docker.io/hello-world   latest              fce289e99eb9        4 months ago        1.84 kB

注:
在国内访问 Docker Hub 速度比较慢,可以在Docker引擎中设置镜像加速器加速对Docker Hub的访问。
更新 `/etc/docker/daemon.json`,添加如下参数,并重启Docker引擎。

{
   "registry-mirrors": ["https://registry.docker-cn.com"]
}

 

构建完毕之后,可以在本地运行验证下是否符合预期。如果看到 `Hello! World` 字样,我们就可以进入下一个环节了。

# 映射容器内 80 端到宿主机上的 8000 端口
$ docker run -d -p 8000:80 demo:v1

# curl 一下查看结果
$ curl localhost:8000
Hello! World

 

使用docker ps 查看正在运行的容器

# docker ps

显示信息

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
d5a2823410aa        demo:v1             "app"               23 seconds ago      Up 22 seconds       0.0.0.0:8000->80/tcp   suspicious_cori

 

1.5 推送镜像至阿里云容器镜像服务

在推送之前,需要注册阿里云账号和开通阿里云容器镜像服务。

 

注:

阿里云注册链接:https://account.aliyun.com/register/register.htm

阿里云登录链接:https://account.aliyun.com/login/login.htm

阿里云容器镜像服务页面:https://cr.console.aliyun.com

容器镜像服务(Container Registry)提供安全的应用镜像托管能力,精确的镜像安全扫描功能,稳定的国内外镜像构建服务,便捷的镜像授权功能,方便用户进行镜像全生命周期管理。


当我们拥有阿里云容器镜像服务账号,并在访问凭证页面设置固定密码后,就可以使用 docker 客户端来登录服务。回到ECS实例,执行以下命令:

$ docker login  -username=**** registry.cn-hangzhou.aliyuncs.com
Password: 
Login Succeeded

注意:此处的密码是在容器镜像服务-访问凭证页面 设置的固定密码。

在推送到镜像之前,我们在容器镜像服务的杭州地域,建立一个命名空间为mydemo、仓库名为demo的本地仓库。之后,我们在ECS上将镜像地址修改为对应的镜像仓库地址:

# mydemo 可以替换成自己的命名空间
$ docker tag demo:v1 registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

$ docker push registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

 

1.6 在本地下载demo:v1镜像

 登出 ECS 实例,在本地(已安装docker环境) docker pull 来下载镜像。

# mydemo 请替换成第5小节步骤中指定的命令空间
$ docker pull registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

下载完毕之后,我们就可以直接运行该镜像。

$ docker run -d -p 8000:80 registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

并查看本地的 `8000` 端口。

$ curl localhost:8000

 

posted on 2019-05-12 08:29  fudonghai  阅读(768)  评论(1编辑  收藏  举报