• 2020系统综合实践 第2次实践作业


    2020系统综合实践 第2次实践作业

    (1) 实现一个自定义的web容器服务

    推荐apachenginx,要求标明镜像作者信息,安装必要的工具以方便维护;设定你自己的web存放目录,安全起见,请将默认的监听端口80更改为你自定义的端口,并且声明暴露的端口,容器启动时,能直接进入web代码的存放目录。

    实验具体步骤如下:

    1.先拉取nginx镜像:

    sudo docker pull nginx
    

    查看nginx镜像是否已经拉取成功:

    sudo docker images
    

    2.根据拉取的nginx镜像创建一个容器:

    sudo docker run -it nginx /bin/bash
    

    使用pwd指令来查看当前容器的工作目录:

    3.完成nginx的相应部署:

    先编写nginx的配置文件default.conf,并和dockerfile文件放入同一文件夹下。这里我们先去运行一个nginx容器,然后找到配置文件default.conf,再把这个文件中的相应代码复制出来

    • 运行一个nginx容器,然后找到配置文件default.conf(这里的9d3fof9a5d07是nginx容器的ID):
     sudo docker cp 9d3fof9a5d07 : /etc/nginx/conf .d/default.conf .
    

    此时可以在根目录下查看到配置文件default.conf,如下所示:

    相应的default.conf文件的内容如下,其中listen是监听的端口号,root为Web的默认工作目录,这两个参数要特别留意:

    • 在根目录下新建一个文件夹(我这里命名为work1),文件夹下需要有三个文件default.conf、Dockerfile文件和自定义的一个HTML文件,如下所示:

    (1)修改default.conf文件:

    把之前得到的在根目录下的配置文件default.conf里的内容复制过来,然后在刚刚自己新建的文件夹下新建一个文档,也把其命名为default.conf。接着把默认的监听端口80更改为自定义的端口(我这里改为2430),把root修改为自己的工作目录(这里即work1文件夹的位置),如下所示:

    (2)编辑dockerfile文件:

    FROM nginx
    #声明镜像来源
    MAINTAINER cyk
    #声明拥有者
    COPY default.conf /etc/nginx/conf.d/
    #复制文件到镜像
    COPY index.html /home/cyk/
    #复制文件到镜像
    WORKDIR /home/cyk
    #设置工作目录
    EXPOSE 2430
    #设置暴露端口号
    

    (3)创建一个自定义的HTML文件,如下所示:

    4.在完成nginx的相应部署后,就可以开始构造构建镜像了(此时要注意先关闭之前运行的容器,然后进入保存dockerfile文件所在的目录,在该目录下启动终端来运行!!)

    sudo docker build - t mynginx1
    

    当出现successfully built.....时,说明我们的镜像已构建成功。

    • 接着运行容器
    sudo docker run --name cyk_ nginx -p 8000:2430 -d mynginx 1
    

    • 当进入这个容器,就会直接进入web代码的存放目录,如下所示:
    sudo docker exec -it 37a889d7cbac /bin/bash
    

    5.查看容器创建的结果

    打开浏览器访问http://localhost:8000/,即可看到之前我们所自定义的HTML界面:

    (2) 实现一个自定义的数据库容器服务

    可选择MysqlMariadb等,要求标明镜像作者信息,为了方便维护,需要能够查看容器内的配置信息,包括但不限于网络、应用配置文件等。在环境变量中设置好数据库的root密码且不允许空密码登录,创建一个测试数据库,指定用户名和密码。

    实验具体步骤如下:

    1.首先拉取一个mysql镜像(选择5.7版本):

    sudo docker pull mysql:5.7
    

    2.接着编辑构建mysql镜像所需的文件(这里我在根目录下新建一个work2文件,在work2目录中应包含Dockerfile、setup.sh、schema.sql以及privileges.sql这四个文件,如下所示:)

    • 编辑Dockerfile文件:
    FROM mysql:5.7
    #声明基础镜像
    
    MAINTAINER cyk2430
    #声明维护者信息
    
    ENV MYSQL_ALLOW_EMPTY_PASSWORD no
    ENV MYSQL_ROOT_PASSWORD=031702430
    #设置root密码且当密码为空时不允许登陆
    
    COPY setup.sh /mysql/setup.sh
    COPY schema.sql /mysql/schema.sql
    COPY privileges.sql /mysql/privileges.sql
    #将所需文件放到容器中
    
    CMD ["sh", "/mysql/setup.sh"]
    #设置容器启动时执行的命令
    
    • 编辑导入数据的mysql脚本命令schema.sql:
    -- 创建数据库
    create database `docker_mysql` default character set utf8 collate utf8_general_ci;
    
    use docker_mysql;
    
    -- 建表
    DROP TABLE IF EXISTS user;
    
    CREATE TABLE user (
      `id` varchar(10) NOT NULL,
      `name` varchar(10) DEFAULT NULL,
      `age` smallint NOT NULL,
      `sdept` varchar(10) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    -- 插入数据
    INSERT INTO user (`id`, `name`, `age`, `sdept`)
    VALUES
        ('031702430','cyk2430',22,'CS');
    
    • 编辑mysql权限设置文件privileges.sql:
    use mysql;
    select host, user from user;
    create user cyk identified by '031702430';
    -- 将docker_mysql数据库的权限授权给创建的用户cyk,密码为031702430:
    grant all on docker_mysql.* to cyk@'%' identified by '031702430' with grant option;
    -- 这一条命令一定要有:
    flush privileges;
    
    • 编辑启动脚本setup.sh:
    #!/bin/bash
    set -e
    
    #查看mysql服务的状态,方便调试,这条语句可以删除
    echo `service mysql status`
    
    echo '1.启动mysql....'
    #启动mysql
    service mysql start
    sleep 3
    echo `service mysql status`
    
    echo '2.开始导入数据....'
    #导入数据
    mysql < /mysql/schema.sql
    echo '3.导入数据完毕....'
    
    sleep 3
    echo `service mysql status`
    
    tail -f /dev/null
    

    3.在dockerfile文件所在的目录下创建镜像,如下所示:

    sudo docker build -t mysql_ cyk2430
    

    查看镜像是否成功创建:

    • 运行容器:
    sudo docker run --name cyk_ mysqL -p 3306:3306 -d mysql_ cyk2430
    

    查看此时正在运行的容器,如下所示:

    • 查看日志信息:
    sudo docker Logs cyk_ mysq
    

    从中可以看出日志信息和启动脚本中规范的步骤一致,说明数据导入和权限设置成功。

    • 进入创建的容器:
    sudo docker exec -it cyk_ mysqL /bin/bash
    

    • 使用设定的用户名和密码进入数据库:
    mysql -u cyk -P;
    

    • 查看已存在的数据库:
    show databases;
    

    • 进入docker_mysql数据库并查看所创建的表:
    use docker_ mysqL;
    select * from user ;
    

    • 查看mysql的配置信息:
    show var iables;
    

    • 查看容器的配置信息:
    sudo docker inspect cyk_mysql
    
    [
        {
            "Id": "ea0f9db65a9788799b3cb0df2777405d889de31ed59263f96bf1ae55f62aa85e",
            "Created": "2020-04-23T01:25:06.416069581Z",
            "Path": "docker-entrypoint.sh",
            "Args": [
                "sh",
                "/mysql/setup.sh"
            ],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 5498,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2020-04-23T01:25:08.969841477Z",
                "FinishedAt": "0001-01-01T00:00:00Z"
            },
            "Image": "sha256:f65e16116f018df6be3be1502795ce355b0f9494ae3613ea3a8d2c467dc38e6e",
            "ResolvConfPath": "/var/lib/docker/containers/ea0f9db65a9788799b3cb0df2777405d889de31ed59263f96bf1ae55f62aa85e/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/ea0f9db65a9788799b3cb0df2777405d889de31ed59263f96bf1ae55f62aa85e/hostname",
            "HostsPath": "/var/lib/docker/containers/ea0f9db65a9788799b3cb0df2777405d889de31ed59263f96bf1ae55f62aa85e/hosts",
            "LogPath": "/var/lib/docker/containers/ea0f9db65a9788799b3cb0df2777405d889de31ed59263f96bf1ae55f62aa85e/ea0f9db65a9788799b3cb0df2777405d889de31ed59263f96bf1ae55f62aa85e-json.log",
            "Name": "/cyk_mysql",
            "RestartCount": 0,
            "Driver": "overlay2",
            "Platform": "linux",
            "MountLabel": "",
            "ProcessLabel": "",
            "AppArmorProfile": "docker-default",
            "ExecIDs": null,
            "HostConfig": {
                "Binds": null,
                "ContainerIDFile": "",
                "LogConfig": {
                    "Type": "json-file",
                    "Config": {}
                },
                "NetworkMode": "default",
                "PortBindings": {
                    "3306/tcp": [
                        {
                            "HostIp": "",
                            "HostPort": "3306"
                        }
                    ]
                },
                "RestartPolicy": {
                    "Name": "no",
                    "MaximumRetryCount": 0
                },
                "AutoRemove": false,
                "VolumeDriver": "",
                "VolumesFrom": null,
                "CapAdd": null,
                "CapDrop": null,
                "Capabilities": null,
                "Dns": [],
                "DnsOptions": [],
                "DnsSearch": [],
                "ExtraHosts": null,
                "GroupAdd": null,
                "IpcMode": "private",
                "Cgroup": "",
                "Links": null,
                "OomScoreAdj": 0,
                "PidMode": "",
                "Privileged": false,
                "PublishAllPorts": false,
                "ReadonlyRootfs": false,
                "SecurityOpt": null,
                "UTSMode": "",
                "UsernsMode": "",
                "ShmSize": 67108864,
                "Runtime": "runc",
                "ConsoleSize": [
                    0,
                    0
                ],
                "Isolation": "",
                "CpuShares": 0,
                "Memory": 0,
                "NanoCpus": 0,
                "CgroupParent": "",
                "BlkioWeight": 0,
                "BlkioWeightDevice": [],
                "BlkioDeviceReadBps": null,
                "BlkioDeviceWriteBps": null,
                "BlkioDeviceReadIOps": null,
                "BlkioDeviceWriteIOps": null,
                "CpuPeriod": 0,
                "CpuQuota": 0,
                "CpuRealtimePeriod": 0,
                "CpuRealtimeRuntime": 0,
                "CpusetCpus": "",
                "CpusetMems": "",
                "Devices": [],
                "DeviceCgroupRules": null,
                "DeviceRequests": null,
                "KernelMemory": 0,
                "KernelMemoryTCP": 0,
                "MemoryReservation": 0,
                "MemorySwap": 0,
                "MemorySwappiness": null,
                "OomKillDisable": false,
                "PidsLimit": null,
                "Ulimits": null,
                "CpuCount": 0,
                "CpuPercent": 0,
                "IOMaximumIOps": 0,
                "IOMaximumBandwidth": 0,
                "MaskedPaths": [
                    "/proc/asound",
                    "/proc/acpi",
                    "/proc/kcore",
                    "/proc/keys",
                    "/proc/latency_stats",
                    "/proc/timer_list",
                    "/proc/timer_stats",
                    "/proc/sched_debug",
                    "/proc/scsi",
                    "/sys/firmware"
                ],
                "ReadonlyPaths": [
                    "/proc/bus",
                    "/proc/fs",
                    "/proc/irq",
                    "/proc/sys",
                    "/proc/sysrq-trigger"
                ]
            },
            "GraphDriver": {
                "Data": {
                    "LowerDir": "/var/lib/docker/overlay2/eaab1f7d401eb127dc42bce613d365897a495483c6ed816aa578afa0327bb3bb-init/diff:/var/lib/docker/overlay2/b6098b7ebf7a66fe36ceaf7fbbf958420f71365ba3665f9bea4a313f92c000f4/diff:/var/lib/docker/overlay2/4eb4289553c7e30e68e2f52d8eb0671309f59f2ae82f656a03410e0746c6687e/diff:/var/lib/docker/overlay2/49af6c5fa060050b9838822d55877646f2c4e6ca860775667885b393b08844d8/diff:/var/lib/docker/overlay2/01c5a0b5c846cc76a444e3c1f03355dee260f1e3d6a4a4f48952e9b81c502776/diff:/var/lib/docker/overlay2/f9eccc4ebcb5360120f85534642d14a4f3dfad407952aad1b7c3dfc3e87133d3/diff:/var/lib/docker/overlay2/df4860da6f81089473096104b08208bc44902c950b9ca9e6e3a2d2a8e3d2e2a7/diff:/var/lib/docker/overlay2/a70720b60fa6e55a2179ac60993c6c70e9661c460701614c953441866c8f6b68/diff:/var/lib/docker/overlay2/f4852b6e141a355bd3cfc651294cbb47c6cb12cfc451320eaecae39e0dbef97d/diff:/var/lib/docker/overlay2/5f067469a496d7784af6db441ce08992a09009eded22167f7eb6ae16577716d2/diff:/var/lib/docker/overlay2/7f2e8195f1093d57cf5f5501d04e2a094d3898ab554bddbd9f58e96a31e0c723/diff:/var/lib/docker/overlay2/21dee9ab2e6b9e6a93ce638a8f6c52e0204b48790b671157aa2f29d6f2eecf60/diff:/var/lib/docker/overlay2/977e4b1be46ddb9c4f384c43981801f390d1b3ec0a4d5a6d5bbe7dfa5ab471b3/diff:/var/lib/docker/overlay2/92e9234d25b0225923bdfa4a405297dca0c85df1f19f11f44c0460a853ac28dc/diff:/var/lib/docker/overlay2/b8e5193a3766e6971bd09b948302a167b08355f3ecb6c95ddb19651f1b87c951/diff",
                    "MergedDir": "/var/lib/docker/overlay2/eaab1f7d401eb127dc42bce613d365897a495483c6ed816aa578afa0327bb3bb/merged",
                    "UpperDir": "/var/lib/docker/overlay2/eaab1f7d401eb127dc42bce613d365897a495483c6ed816aa578afa0327bb3bb/diff",
                    "WorkDir": "/var/lib/docker/overlay2/eaab1f7d401eb127dc42bce613d365897a495483c6ed816aa578afa0327bb3bb/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [
                {
                    "Type": "volume",
                    "Name": "b48cc6b243f806f76f345c05831c0b9a570d4e6adf829553f9c0bf02505b132b",
                    "Source": "/var/lib/docker/volumes/b48cc6b243f806f76f345c05831c0b9a570d4e6adf829553f9c0bf02505b132b/_data",
                    "Destination": "/var/lib/mysql",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                }
            ],
            "Config": {
                "Hostname": "ea0f9db65a97",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "ExposedPorts": {
                    "3306/tcp": {},
                    "33060/tcp": {}
                },
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "GOSU_VERSION=1.12",
                    "MYSQL_MAJOR=5.7",
                    "MYSQL_VERSION=5.7.29-1debian10",
                    "MYSQL_ALLOW_EMPTY_PASSWORD=no",
                    "MYSQL_ROOT_PASSWORD=031702430"
                ],
                "Cmd": [
                    "sh",
                    "/mysql/setup.sh"
                ],
                "Image": "mysql_cyk2430",
                "Volumes": {
                    "/var/lib/mysql": {}
                },
                "WorkingDir": "",
                "Entrypoint": [
                    "docker-entrypoint.sh"
                ],
                "OnBuild": null,
                "Labels": {}
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "deb6cc5c45d1dd2dfd9464d3b7c8eb4f22a9a1869f86c8b05b6bbd178a069381",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {
                    "3306/tcp": [
                        {
                            "HostIp": "0.0.0.0",
                            "HostPort": "3306"
                        }
                    ],
                    "33060/tcp": null
                },
                "SandboxKey": "/var/run/docker/netns/deb6cc5c45d1",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "0cf7b7a9a1fe35d957d33b442811fd02003d8ba6308069c1d84f24ec107e3751",
                "Gateway": "172.17.0.1",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "MacAddress": "02:42:ac:11:00:02",
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "3a94d53cf364dbbb2952937746505eedddb46d48359e7544972bf3a106c4558b",
                        "EndpointID": "0cf7b7a9a1fe35d957d33b442811fd02003d8ba6308069c1d84f24ec107e3751",
                        "Gateway": "172.17.0.1",
                        "IPAddress": "172.17.0.2",
                        "IPPrefixLen": 16,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:ac:11:00:02",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
    
    

    (3)遇到的问题和总结

    问题1:在实现一个自定义的web容器服务这第一个实验中,有一次我已经成功构建了相应的镜像并且运行了容器,但在浏览器上查看html界面时,却显示404notfound,如下所示:

    解决方法:在后面寻思了一会后,我发现我应该是dockerfile文件中工作目录的位置配置错了,然后把它重新修改成创建的work1文件所在的位置,再重新构建镜像,运行容器,最后解决了这个问题。

    问题2:在构造镜像的时候,第一次我没有进入保存dockerfile文件所在的目录,在该目录下启动终端来运行,然后导致了所创建的镜像出现错误,并且这个镜像还特别的大(快1个G,吓死人了...)

    解决方法:进入保存dockerfile文件所在的目录,在该目录下启动终端来运行。

    总结:刚开始做这个实验的时候,确实是一点思路也没有,然后通过同学的推荐,去网上看了相关的视频以及博客,后面才逐渐开始理清了实验的整体框架。通过这次的实验,我学会了如何实现一个自定义的web容器服务以及实现一个自定义的数据库容器服务,掌握了编写Dockerfile文件的基本方法,收获很大,但也遇到了不少挑战!

    相关参考材料:

    dockerfile运行mysql并初始化数据

    https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

  • 相关阅读:
    成功实施的APS项目故事分享---我们数据治理的心路历程
    成功实施的APS项目故事分享---如何管理与激励APS项目团队
    东莞裕同&易普优APS项目启动啦!
    包装印刷行业裕同集团&易普优APS项目顺利验收!
    易普优APS混流排序算法助力汽车整车厂的均衡生产
    动态调用WebService接口
    C#推送RocketMQ信息
    DEV带筛选行CheckBox选中问题
    验证上传文件真实格式
    PLSQL导出触发器代码
  • 原文地址:https://www.cnblogs.com/chenyoukun/p/12771218.html
Copyright © 2020-2023  润新知