其他分享
首页 > 其他分享> > docker 期末考试练习题

docker 期末考试练习题

作者:互联网

1 跑compose集群

curl -L "https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
获取文件
chmod +x /usr/local/bin/docker-compose 
[root@docker my_wordpress]# cat docker-compose.yml 
version: '3.3'
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}
docker-compose up -d
看起来像是通过yml文件跑集群

2 dockerfile编写

弄一个阿里源

[root@docker centos7]# cp /etc/yum.repos.d/CentOS-Base.repo .
mkdir centos7 && cd  centos7
[root@docker centos7]# cat Dockerfile 
FROM centos:7
RUN rm -rf /etc/yum.repos.d/*
COPY  CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum repolist && yum install -y httpd
EXPOSE 80
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

创建镜像
docker build -t http:v1.0 .

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
6ebde8431e11   d521e0174cbb   "/usr/sbin/httpd -D 鈥   21 seconds ago   Up 19 seconds   0.0.0.0:8099->80/tcp, :::8099->80/tcp   mqy
[root@docker ~]# 

创建文件

[root@docker centos7]# echo hello apache >> index.html
[docker cp index.html  mqy:/var/www/html   (正在运行的容器6ebde8431e11)]
拷贝文件到运行中的容器,可以跳过此步骤

更改dockfile
[root@docker centos7]# cat Dockerfile 
FROM centos:7
RUN rm -rf /etc/yum.repos.d/*
COPY CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum repolist && yum install -y httpd
EXPOSE 80
COPY index.html /var/www/html
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
ENV HOSTNAME www.example.com
docker build  -t http:v2.0  .

[root@docker centos7]# docker run -d  -p 8080:80 http:v2.0
f06a389a52b682f7331bab418f1c21cc11c92dafa35237d291d5c2c4dd8a3a98
[root@docker centos7]# curl localhost:8080
hello apache

[root@docker centos7]# docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED             SIZE
http                                                 v2.0      c647b7cefc78   9 seconds ago       407MB
http                                                 v1.0      d521e0174cbb   About an hour ago   407MB

3 registry的使用

上传registry.tar文件
加载包
docker load -i registry_latest.tar 
[root@docker /]# docker run -d --name registry -p 5000:5000 --restart=always registry:latest
设置此容器自启动
3572789db594a8a4fba383bf4b0c64919f6bdc92d47aa79784b1d3212e72b8b4

[root@docker /]# docker ps -l
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS        PORTS                                       NAMES
3572789db594   registry:latest   "/entrypoint.sh /etc鈥   3 seconds ago   Up 1 second   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@docker /]# 
验证仓库
[root@docker /]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}

打标签上传镜像到本地仓库

[root@docker ~]# docker tag centos:7 127.0.0.1:5000/centos
[root@docker ~]# docker push 127.0.0.1:5000/centos
Using default tag: latest
The push refers to repository [127.0.0.1:5000/centos]
174f56854903: Pushed 
latest: digest: sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f size: 529
[root@docker ~]# 
[root@docker /]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":["centos"]}
删除镜像
[root@docker /]# docker rmi 127.0.0.1:5000/centos
再拉取
[root@docker /]# docker pull 127.0.0.1:5000/centos

4 将仓库的镜像供其他人使用

使用远程私有镜像
客户端

cat /etc/docker/daemon.json 
{ "insecure-registries":["192.168.5.128:5000"],"registry-mirrors":["https://lrppnyd2.mirror.aliyuncs.com"] }
systemctl daemon-reload
systemctl  restart  docker
客户端拉取服务端镜像时使用的是https,"insecure-registries":["192.168.5.128:5000"]加上这条使用http

运行镜像
docker run --name centos-7 -itd    192.168.5.128:5000/centos
进入镜像并安装net-tools
[root@docker ~]# docker exec -it c224eb01c3dd /bin/bash
[root@c224eb01c3dd /]# yum -y install net-tools
ctrl + q + p 退出
将容器封装成镜像
[root@docker ~]# docker commit  c224eb01c3dd centos:net
sha256:189d33053d5f60df37af4be7863dac11d0467023d04438af8cca19447c9d0114
[root@docker ~]# docker images 
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
centos                      net       189d33053d5f   6 seconds ago   375MB
打标签推送
[root@docker ~]# docker tag centos:net 192.168.5.128:5000/centos2:net
[root@docker ~]# docker push 192.168.5.128:5000/centos2:net
查看
[root@docker ~]# curl http://192.168.5.128:5000/v2/_catalog
{"repositories":["centos","centos2"]}


服务端
将客户端上传的镜像打成tar包

拉取客户端上传的镜像
[root@docker ~]# docker pull 127.0.0.1:5000/centos2:net
将文件打成tag
[root@docker ~]# docker save 127.0.0.1:5000/centos2:net > /opt/centos2_net.tar     
[root@docker ~]# cd /opt/
[root@docker opt]# ls
centos2_net.tar  containerd  jdk1.8.0_271  tmp

修改镜像centos:net的/etc/hosts并打包成tag

跑容器
docker run -dit --name centos-net  127.0.0.1:5000/centos
docker exec -it 1b0f2d8f686 /bin/bash
修改配置文件
[root@1b0f2d8f686a /]# tail -n 1 /etc/hosts       
172.17.0.3      centos-net
退出容器
[root@docker opt]# docker export 1b0f2d8f6 > /opt/finalcentos.tar
[root@docker opt]# ls
centos2_net.tar  containerd  finalcentos.tar  jdk1.8.0_271  tmp

标签:练习题,5000,centos,期末考试,yum,docker,root,net
来源: https://www.cnblogs.com/supermao12/p/16358893.html