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

一,实现一个自定义的web容器服务

(1)拉取nginx镜像

sudo docker pull nginx

(2) 查看镜像

sudo docker images

(3) 运行一个nginx容器

sudo docker run --name nginx_test -p 8080:80 -d nginx

(4) 修改默认的default.conf文件,并将配置文件放于一个目录下

  • 复制default.conf
sudo docker exec -it [容器id] /bin/bash
cd /etc/nginx/conf.d
cat default.conf

  • 设置Dockerfile

  • 修改default.conf,端口号修改为5678,并把root修改为mydockerfile

  • 设置index.html

  • 将三个文件放于同一个目录mydockerfile之下

(5) 构建nginx镜像,并运行容器

docker build -t newnginx .

docker run --name newnginx -p 7878:5678 -d newnginx

(6) 查看index.html

二,实现一个自定义的数据库容器服务

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

sudo docker pull mysql:5.7

(2)接着编辑构建mysql镜像所需的文件(mysql目录中包含Dockerfile、setup.sh、schema.sql以及

privileges.sql这四个文件)

  • 编辑Dockerfile文件
from mysql:5.7

maintainer JOKER

ENV MYSQL_ALLOW_EMPTY_PASSWORD no
ENV MYSQL_ROOT_PASSWORD=123456

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(9) NOT NULL,
  `name` varchar(20) 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
    ('031702444','lsj',21,'CS');
  • 编辑启动脚本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`

#重新设置mysql密码
echo '4.开始修改密码....'
mysql < /mysql/privileges.sql
echo '5.修改密码完毕....'

#sleep 3
echo `service mysql status`
echo `mysql容器启动完毕,且数据导入成功`

tail -f /dev/null
  • 编辑mysql权限设置文件privileges.sql
use mysql;
select host, user from user;
create user lsj identified by '123456';
-- 将docker_mysql数据库的权限授权给创建的用户lsj,密码为123456:
grant all on docker_mysql.* to lsj@'%' identified by '123456' with grant option;
-- 这一条命令一定要有:
flush privileges;

(3)在mysql文件所在的目录下创建镜像:

  • 创建镜像
sudo docker build -t newmysql .

  • 查看镜像是否成功创建
sudo docker images

  • 运行容器
sudo docker run --name newmysql -p 3456:3456 -d newmysql

  • 查看此时正在运行的容器
sudo docker ps

  • 查看日志信息
sudo docker logs newmysql

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

  • 使用设定的用户名和密码进入数据库
mysql -u lsj -p

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

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

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

  • 查看容器的配置信息
sudo docker inspect newmysql
[
    {
        "Id": "b22370a7d51bdefa22472302dc94b2a015b1072ebe04ae42396e815c70252168",
        "Created": "2020-04-24T18:02:15.815383271Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "sh",
            "/mysql/setup.sh"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 7455,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-04-24T18:02:16.44750932Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:c8296f86cfda488c6bf056b4093f3dd97300b74b7cc38a384d879518cc2ddc4a",
        "ResolvConfPath": "/var/lib/docker/containers/b22370a7d51bdefa22472302dc94b2a015b1072ebe04ae42396e815c70252168/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/b22370a7d51bdefa22472302dc94b2a015b1072ebe04ae42396e815c70252168/hostname",
        "HostsPath": "/var/lib/docker/containers/b22370a7d51bdefa22472302dc94b2a015b1072ebe04ae42396e815c70252168/hosts",
        "LogPath": "/var/lib/docker/containers/b22370a7d51bdefa22472302dc94b2a015b1072ebe04ae42396e815c70252168/b22370a7d51bdefa22472302dc94b2a015b1072ebe04ae42396e815c70252168-json.log",
        "Name": "/newmysql",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": [
            "89d6aaa6504f404c3820f8bed2e27cba5865e723f1c17f3e34654def12f311b4"
        ],
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "3456/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "3456"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "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/9b93c85088bdaff2cb7db39db1787781a11e43a85d098ab50182ddb6f4347b3e-init/diff:/var/lib/docker/overlay2/91ef72cacfbe696723fe8c8c3d54d78b3cd6e4db43394b95f57efeedc2ff3e5b/diff:/var/lib/docker/overlay2/b95adc5254e184196112f32dde827a6c22114ec27066c7d9068d11cbbb6498d9/diff:/var/lib/docker/overlay2/d41a19e21d181640987a187d175c3c85eaee26ecd138503a4370ad2a1742c0bb/diff:/var/lib/docker/overlay2/1b6c04e10a51f3a0b4d9f63d7e6538c9dacecf93dcbff7ad410473836f89ee35/diff:/var/lib/docker/overlay2/38b7923596f26a2446fc7030cc17e9aea7bf0d4cb96e1c037318d4d78cab0857/diff:/var/lib/docker/overlay2/8f97c08c64b0e5dd0d7f07c7fb9ef30b01c6c9fabbf3e7a0b60b96c35e399c90/diff:/var/lib/docker/overlay2/7865fbcde60a5766eaae4c1e8d856041d6b2293b410d04c6eb170a7bd5f539b4/diff:/var/lib/docker/overlay2/5cb5e11182b0c94313468bc40cae1849ba6e893e2eae5a4b26e2679ab6e3ec17/diff:/var/lib/docker/overlay2/734916a39088f1c3ebade138cfc6c5c1a90f58abe31f18a8bd79fd5b399ab99e/diff:/var/lib/docker/overlay2/2fde95d829b3016f3fa65fa41f502907f2b3a22748033461660b714addf54f5b/diff:/var/lib/docker/overlay2/e167713325a3520650330c86ad36930f758013f0002e27570d70147fc18ca387/diff:/var/lib/docker/overlay2/bcd669a8464bc1e8f63f36ee6296b30a8eb56bb79a2c76fbd3dcaae4ac6a8029/diff:/var/lib/docker/overlay2/c9e79f668f7f1ba466e592c4c955c68f55947c1d54722ad3d4ee82c3f69cdf4a/diff:/var/lib/docker/overlay2/36d26e28ec1d5defacdeff03317be4e112c239074b7fd15f6008d21a9854fe0d/diff",
                "MergedDir": "/var/lib/docker/overlay2/9b93c85088bdaff2cb7db39db1787781a11e43a85d098ab50182ddb6f4347b3e/merged",
                "UpperDir": "/var/lib/docker/overlay2/9b93c85088bdaff2cb7db39db1787781a11e43a85d098ab50182ddb6f4347b3e/diff",
                "WorkDir": "/var/lib/docker/overlay2/9b93c85088bdaff2cb7db39db1787781a11e43a85d098ab50182ddb6f4347b3e/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "38d0afab64c4d02e804c943e7f83c4f49fcd5e2a686551080caf719ddef35ea3",
                "Source": "/var/lib/docker/volumes/38d0afab64c4d02e804c943e7f83c4f49fcd5e2a686551080caf719ddef35ea3/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "b22370a7d51b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {},
                "3456/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=123456"
            ],
            "Cmd": [
                "sh",
                "/mysql/setup.sh"
            ],
            "ArgsEscaped": true,
            "Image": "newmysql",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "de208a0a790c4dd68867ca6eac343414259dedf989d5c469ae25e20cc9ab2537",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null,
                "33060/tcp": null,
                "3456/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3456"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/de208a0a790c",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "4335480cffe9b3f9badfd6c0cce6158230026b6d1f304873e1ece63dee9639c1",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "0733516f6077aa39ed8675be040802c68e7c53fd4d09e82aa8ba571943f1362d",
                    "EndpointID": "4335480cffe9b3f9badfd6c0cce6158230026b6d1f304873e1ece63dee9639c1",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

三,遇到的问题和解决方法

问题:在一开始的时候,由于构建了错误的新的newnginx镜像,没有修改名字,和原始镜像nginx重

名,使得原始镜像的TAG编程NONE,无法使用。

有时候重新构建镜像的时候,该镜像正在被某容器使用中,那么在重新构建同名同版本镜像后,docker保留原来的镜像,即容器还是用原来的,除非重启。 那么原来的镜像名称变成NONE,TAG也成了NONE。

解决方法

列出所有的容器 ID:

docker ps -a -q

删除前应该停止所有的容器:

docker stop $(docker ps -a -q)

批量删除tag为""镜像可以采用如下方法(下面两种方式均可以):

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
docker images | grep none | awk '{print $3}' | xargs docker rmi

注:
删除所有停止的容器:

docker rm $(docker ps -a -q)

删除所有的镜像:

docker rmi $(docker images -q)

一般情况下参考资料1能解决,实在删除不掉使用参考资料2

四,总结

在实验过程中由于粗心大意,把个别配置写错, 导致不断地返工,要恢复删除容器和镜像恢复原始的环境,经过不断磕磕碰碰,终于搞定了实验,复习巩固了实践1的基本操作,对实践2中的整体操作框架和细节也有了完整的认识。

posted @ 2020-04-25 00:52  Joker·D  阅读(289)  评论(0编辑  收藏  举报