其他分享
首页 > 其他分享> > docker考试模拟题

docker考试模拟题

作者:互联网

第一次作业:安装docker

请大家使用阿里云镜像站里的docker-ce源在线部署一个docker引擎,源地址:https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo部署完成后,运行docker version命令,并将命令及运行结果截图到答题区域。

答题步骤:

# 1.安装gcc相关环境(需要确保 虚拟机可以上外网)
yum -y install gcc
yum -y install gcc-c++

# 2.安装需要的软件包
yum install -y yum-utils

# 3.设置镜像仓库
yum-config-manager \
	--add-repo \
	http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  #推荐使用阿里云的,十分的快
	
# 4.更新yum软件包索引
yum makecache fast

# 5.安装 Docker CE
yum install docker-ce docker-ce-cli containerd.io

# 6.启动 Docker
systemctl start docker

# 7.测试
docker version  #查看版本信息
docker run hello-world  #拉取镜像
docker images  #查看镜像

第二次作业:配置镜像加速器

答题步骤:

#创建文件夹
[root@docker ~]# mkdir -p /etc/docker

#配置加速器参数
[root@docker ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
   "registry-mirrors": ["https://z2uujcy0.mirror.aliyuncs.com"]
}
EOF

#重启
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker

第三次作业:常规命令的使用

1、请你将官方镜像nginx:1.18和registry:latest下载到本地,并将命令及运行结果以文本形式提交到答题区域。

#下载nginx:1.18镜像
[root@docker ~]# docker pull nginx:1.18
1.18: Pulling from library/nginx
f7ec5a41d630: Pull complete 
0b20d28b5eb3: Pull complete 
1576642c9776: Pull complete 
c12a848bad84: Pull complete 
03f221d9cf00: Pull complete 
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18
docker.io/library/nginx:1.18

#下载registry:latest镜像
[root@docker ~]# docker pull registry:latest
latest: Pulling from library/registry
2408cc74d12b: Pull complete 
ea60b727a1ce: Pull complete 
c87369050336: Pull complete 
e69d20d3dd20: Pull complete 
fc30d7061437: Pull complete 
Digest: sha256:bedef0f1d248508fe0a16d2cacea1d2e68e899b2220e2258f1b604e1f327d475
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

2、请使用registry:latest搭建一个本地私有仓库,命名为registry,将本地宿主机的5000端口映射到容器的5000端口上,并将仓库设置为自动启动。请将命令及运行的结果以文本形式提交到答题区域。

#搭建本地私有仓库
[root@docker ~]# docker run -itd --name registry -p 5000:5000 --restart=always registry 
23cf09d52cefc7bbeb24dc67452da4b87135b24d950c2d65435372f0bed632cd

#查看当前运行的容器
[root@docker ~]# docker ps 
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
23cf09d52cef   registry   "/entrypoint.sh /etc…"   8 seconds ago   Up 7 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

3、使用nginx:1.18镜像来启动一个nginx容器,容器命名为nginx-1,并将宿主机的8080端口映射到容器的80端口上,且将宿主机的/opt/nginx目录挂到容器内部nginx默认的主页路径(/usr/share/nginx/html)下,在/opt/nginx下创建一个主页文件index.html,编辑内容hello world,最后请你使用浏览器访问该主页。请将主页以截图形式提交到答题区域。

#启动一个nginx容器
[root@docker ~]# docker run -itd --name nginx-1 -p 8080:80 -v /opt/nginx:/usr/share/nginx/html nginx:1.18 
5df1988907ed8fd35405084b456b1f4e8b77e274b934f3fa00fd5c5c667b6499

#查看最后一个容器
[root@docker ~]# docker ps -l
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                   NAMES
5df1988907ed   nginx:1.18   "/docker-entrypoint.…"   13 seconds ago   Up 12 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx-1

image

第四次作业:镜像的构建

1、请你在本地docker宿主机上加载registry的tar包为registry:latest镜像,并启动为私有仓库容器,将本地宿主机的5000端口映射到容器的5000端口,仓库命名为registry,设置为自启动状态。请将docker ps -l命令及运行结果截图提交到答题区域。

#将tar包加载为镜像
[root@docker ~]# docker load -i registry_latest.tar 
d9ff549177a9: Loading layer  4.671MB/4.671MB
f641ef7a37ad: Loading layer  1.587MB/1.587MB
d5974ddb5a45: Loading layer  20.08MB/20.08MB
5bbc5831d696: Loading layer  3.584kB/3.584kB
73d61bf022fd: Loading layer  2.048kB/2.048kB
Loaded image: registry:latest

#查看加载后的镜像
[root@docker ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
registry     latest    f32a97de94e1   3 years ago   25.8MB

#创建私有仓库
[root@docker ~]# docker run -itd --name registry --restart=always -p 5000:5000 registry:latest 
72e407f423a2e8609ac1bd73fe499558fc5eae87ff5a6bdba0096ff848ee69ba

#查看最后一个容器
[root@docker ~]# docker ps -l
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS         PORTS                                       NAMES
72e407f423a2   registry:latest   "/entrypoint.sh /etc…"   10 seconds ago   Up 9 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

2、下载官方的centos:7镜像,将该镜像上传到registry仓库。请将关键命令及运行结果以文本形式提交到答题区域。

#下载centos:7镜像
[root@docker ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
Status: Downloaded newer image for centos:7
docker.io/library/centos:7

#添加
[root@docker ~]# vim /etc/docker/daemon.json 
{
  "insecure-registries":["http://docker:5000"],
  "registry-mirrors":["https://z2uujcy0.mirror.aliyuncs.com"] 
}

#给镜像打标签
[root@docker ~]# docker tag eeb6ee3f44bd docker:5000/centos:7

#推送镜像
[root@docker ~]# docker push docker:5000/centos:7 
The push refers to repository [docker:5000/centos]

3、在远程docker主机上下载registry中的centos:7镜像,将该镜像运行为一个容器,命名为centos-7,进入容器安装net-tools工具,实现netstat命令的自由使用,并将该容器打包封装为新的镜像,命名为centos:net,重新上传至registry。请将关键命令及运行结果以文本形式提交到答题区域。

#下载centos:7镜像
[root@docker ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7

#进入容器安装
[root@docker ~]# docker exec -it e0333f71d2ad /bin/bash

#安装包
[root@e0333f71d2ad /]# yum -y install net-tools 

#测试
[root@e0333f71d2ad /]# netstat -pant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    

#构建成新的镜像
[root@docker ~]# docker commit -a "centos:net" e0333f71d2ad centos:net
sha256:6c23c6f76e8a5690fbeb37e7f5417b036e7e4b13b96d3192e727931079f42dfd

#查看构建后的镜像
[root@docker ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       net       6c23c6f76e8a   8 seconds ago   375MB
centos       7         eeb6ee3f44bd   8 months ago    204MB

#给新的镜像打标签
[root@docker ~]# docker tag 6c23c6f76e8a docker:5000/centos:net

#推送镜像到本地仓库
[root@docker ~]# docker push 127.0.0.1:5000/centos:net 
The push refers to repository [127.0.0.1:5000/centos]
f8138eaa45f0: Pushed 
174f56854903: Pushed 
net: digest: sha256:0337f571576519a533fb43fdced060472dcd183533012610639178a70131ec50 size: 741

4、在本地docker宿主机上,下载registry中的centos:net镜像,并将其保存为tar包。请将关键命令及运行结果以文本形式提交到答题区域。

#下载centos:net
[root@docker ~]# docker pull 127.0.0.1:5000/centos:net 
net: Pulling from centos
Digest: sha256:0337f571576519a533fb43fdced060472dcd183533012610639178a70131ec50
Status: Image is up to date for 127.0.0.1:5000/centos:net
127.0.0.1:5000/centos:net

[root@docker ~]# docker images 
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
127.0.0.1:5000/centos   net       6c23c6f76e8a   28 minutes ago   375MB

[root@docker ~]# docker save -o centos_net.tar 127.0.0.1:5000/centos:net 
[root@docker ~]# ls
centos_net.tar  

5、在本地docker宿主机上,将registry中的centos:net镜像运行为容器,命名为centos-net,并在该容器中,通过/etc/hosts文件修改本容器主机IP映射为centos-net,然后将该容器导出为一个tar包。请将关键命令及运行结果以文本形式提交到答题区域。

[root@docker ~]# docker run -itd --name centos-net 6c23c6f76e8a
894552476d79fb7f36e04d524a9bc75d02b665fdcf9ef7218c78b30d01c8cd21

[root@docker ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS         PORTS     NAMES
894552476d79   6c23c6f76e8a   "/bin/bash"   6 seconds ago   Up 5 seconds             centos-net

[root@docker ~]# docker exec -it 894552476d79 /bin/bash

[root@894552476d79 /]# vi /etc/hosts
[root@894552476d79 /]# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	894552476d79
10.104.44.32 centos-net

[root@docker ~]# docker save -o centos_net_hosts.tar 127.0.0.1:5000/centos:net 
[root@docker ~]# ls
centos_net_hosts.tar  centos_net.tar       


第五次作业:使用dockerfile构建镜像

1、基于centos7镜像编写dockerfile文件,构建http服务,Dockerfile要求删除镜像的yum源,使用当前docker宿主机系统的yum源文件,完成后安装http服务,此镜像要求暴露80端口,构建的镜像名称为http:v1.0。请将Dockerfile文件内容以文本形式提交到答题区域,并将docker ps -a命令及结果截图到答题区域。

[root@docker dockerfile]# ls
CentOS-Base.repo  Dockerfile

[root@docker dockerfile]# cat Dockerfile 
FROM centos:7
ADD CentOS-Base.repo /etc/yum.repos/CentOS-Base.repo
RUN yum -y install httpd
EXPOSE 80 

[root@docker dockerfile]# docker build -t http:v1.0 .
Complete!
Removing intermediate container 446a58bf1f6e
 ---> bbe5f2592a78
Step 4/4 : EXPOSE 80
 ---> Running in c94a60bdaf54
Removing intermediate container c94a60bdaf54
 ---> f1273fafeffd
Successfully built f1273fafeffd
Successfully tagged http:v1.0

[root@docker dockerfile]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                        PORTS     NAMES
0b5a334d2587   61a1a2ad9afc   "/bin/sh -c 'yum -y …"   24 minutes ago   Exited (127) 23 minutes ago             friendly_stonebraker

2、基于centos7镜像编写dockerfile文件,构建http服务,Dockerfile要求删除镜像的yum源,使用当前系统的yum源文件,完成后安装http服务,上传index.html文件至镜像内部/var/www/html路径下,index文件内容为“hello apache",此镜像要求暴露80端口,且修改参数HOSTNAME为www.example.com,设置httpd服务为前台启动,构建的镜像名称为http:v2.0。启动该镜像为容器,命名为http-1,将8080端口映射至容器内部80端口,访问8080端口网页。请将Dockerfile文件内容以文本形式提交到答题区域,并将容器主页的浏览器访问结果截图到答题区域。

[root@docker dockerfile]# cat Dockerfile 
FROM centos:7
ADD CentOS-Base.repo /etc/yum.repos/CentOS-Base.repo
RUN yum -y install httpd
ENV HOSTNAME "www.example.com"
EXPOSE 80 

[root@docker dockerfile]# docker build -t http:v2.0 .
Sending build context to Docker daemon  31.23kB
Step 1/4 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/4 : ADD CentOS-Base.repo /etc/yum.repos/CentOS-Base.repo
 ---> Using cache
 ---> 0e6df3dcb106
Step 3/4 : RUN yum -y install httpd
 ---> Using cache
 ---> bbe5f2592a78
Step 4/4 : EXPOSE 80
 ---> Using cache
 ---> f1273fafeffd
Successfully built f1273fafeffd
Successfully tagged http:v2.0

[root@docker dockerfile]# docker run -it --name http-1 -p 8080:80  http:v2.0 
[root@www /]# hostname
www.example.com

第六次作业:使用docker compose编排

请基于教材P140面的内容,使用docker compose技术完成WordPress博客项目的部署。请你将博客系统登录的页面提交到答题区域。

# 安装Docker Compose(将二进制文件docker-compose-linux-x86_64上传到/root下)
[root@docker ~]# ll
total 25376
-rw-r--r--. 1 root root 25968640 Jun  9 12:47 docker-compose-linux-x86_64

# 给二进制文件可执行权限
[root@docker ~]# chmod +x docker-compose-linux-x86_64 

# 给二进制文件重命名为docker-compose
[root@docker ~]# mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose

# 查看docker-compose的版本
[root@docker ~]# docker-compose --version 
Docker Compose version v2.6.0

#创建文件夹
[root@docker ~]# mkdir wordpress && cd wordpress

#编写yml文件
[root@docker wordpress]# vim docker-compose.yml
version: "3.3"
services:
    db:
      image: mysql:5.7
      volumes:
        - db_data:/var/lib/mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 123456
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
    wordpress:
      depends_on:
        - db   
      image: wordpress:latest
      ports:
        - "8008:80"
      restart: always
      environment:
        WORDPRESS_DB_HOST: db:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
        WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}
    
# 执行yml文件
[root@docker wordpress]# docker-compose up -d         
[+] Running 4/4
 ⠿ Network wordpress_default        Created                                                                                               0.1s
 ⠿ Volume "wordpress_db_data"       Created                                                                                               0.0s
 ⠿ Container wordpress-db-1         Started                                                                                               2.1s
 ⠿ Container wordpress-wordpress-1  Started    
 
# 查看容器运行结果
[root@docker wordpress]# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED              STATUS              PORTS                                   NAMES
ba9f3e8b8ca8   wordpress:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:8008->80/tcp, :::8008->80/tcp   wordpress-wordpress-1
85481c18222c   mysql:5.7          "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp, 33060/tcp                     wordpress-db-1

# 浏览器访问:http://IP:8008

image

标签:5000,centos,模拟题,镜像,docker,root,net,考试
来源: https://www.cnblogs.com/whwh/p/16361159.html