2020系统综合实践 第二次实践作业
一.实现一个自定义的web容器服务
要求标明镜像作者信息,安装必要的工具以方便维护;设定你自己的web存放目录,安全起见,请将默认的监听端口80更改为你自定义的端口,并且声明暴露的端口,容器启动时,能直接进入web代码的存放目录。
(1)从 docker hub拉取nginx作为基础镜像
sudo docker pull nginx
(2)创建一个容器并运行
执行pwd命令,可以查看工作目录在根目录下
sudo docker run -it nginx /bin/bash
pwd
(3)复制运行中nginx容器的配置文件,为后续修改监听端口备用
sudo docker cp <容器ID>:/etc/nginx/conf.d/default.conf .
也可以设置第二个参数指定复制到宿主机的目录
sudo docker cp <容器ID>:路径/文件 本地路径
可以看到宿主机根目录下已经出现了default.conf文件
使用 vim default.conf 指令来修改监听端口,默认的工作目录,和访问的界网页页面
(4)编写dockerfile文件
(5)build dockerfile
sudo docker build -f dockerfile -t mynginx:test .
注意 测试页面,配置文件要和 dockerfile文件放到同一目录下,出现success则表示build成功
(6)创建容器并映射主机端口
sudo docker run -p 8000:2020 -d mynginx:test /bin/bash
查看容器端口的映射
(7)打开浏览器访问:localhost:8000
二.实现一个自定义的数据库容器服务
要求标明镜像作者信息,为了方便维护,需要能够查看容器内的配置信息,包括但不限于网络、应用配置文件等。在环境变量中设置好数据库的root密码且不允许空密码登录,创建一个测试数据库,指定用户名和密码。
(1)创建并mysql文件夹
mkdir ~/mysql
cd ~/mysql
(2)使用vim指令编辑dockerfile文件
vim dockerfile
FROM mysql:5.7
#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
#将所需文件放到容器中
COPY setup.sh /mysql/setup.sh
COPY schema.sql /mysql/schema.sql
COPY privileges.sql /mysql/privileges.sql
#设置容器启动时执行的命令
CMD ["sh", "/mysql/setup.sh"]
(2)使用vim指令编写容器启动脚本setup.sh
vim 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
(3)使用vim指令编写导入数据的mysql脚本命令schema.sql:
vim schema.sql
-- 创建数据库
create database `docker_mysql` default character set utf8 collate utf8_general_ci;
use docker_mysql;
-- 建表
DROP TABLE IF EXISTS test;
CREATE TABLE test (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT ""
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- 插入数据
INSERT INTO test (`id`, `name`)
VALUES
(2020,'monster');
(4)使用vim指令编写权限设置命令privileges.sql
vim privileges.sql
use mysql;
select host, user from user;
-- 新建用户为如下命令:
create user docker identified by '123456';
-- 将docker_mysql数据库的权限授权给创建的docker用户,密码为123456:
grant all on docker_mysql.* to docker@'%' identified by '123456' with grant option;
-- use privileges
flush privileges;
(5)build dockerfile
sudo docker build -t mysql:test
(6)创建并运行容器,查看容器状态
sudo docker run -d -p 13306:3306 mysql:test
sudo docker ps -a
(7)进入容器的shell终端,使用用户账号输入密码后进入mysql,使用docker_mysql数据库
sudo docker exec -it <容器ID> /bin/bash
mysql -u docker -p
use docker_mysql
(8)查看tableb表,且用select指令查看测试数据
show tables;
select * from test
;
(9)查看容器信息
sudo docker inspect <容器ID>
[
{
"Id": "d13f526816f06aa00b2728753baecba9b59b1a6889c76bddc4405ffca1f32135",
"Created": "2020-04-23T05:55:00.425670896Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3641,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-24T02:12:24.459971836Z",
"FinishedAt": "2020-04-23T07:25:36.726746804Z"
},
"Image": "sha256:da09579a3a3bc55798df9cc92b4c6e2b054b0555029cb2e8b49ce83df8a83793",
"ResolvConfPath": "/var/lib/docker/containers/d13f526816f06aa00b2728753baecba9b59b1a6889c76bddc4405ffca1f32135/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/d13f526816f06aa00b2728753baecba9b59b1a6889c76bddc4405ffca1f32135/hostname",
"HostsPath": "/var/lib/docker/containers/d13f526816f06aa00b2728753baecba9b59b1a6889c76bddc4405ffca1f32135/hosts",
"LogPath": "/var/lib/docker/containers/d13f526816f06aa00b2728753baecba9b59b1a6889c76bddc4405ffca1f32135/d13f526816f06aa00b2728753baecba9b59b1a6889c76bddc4405ffca1f32135-json.log",
"Name": "/crazy_wozniak",
"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": {
"2020/tcp": [
{
"HostIp": "",
"HostPort": "8000"
}
]
},
"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/400ba5daf7d017c73c449c023bb8c5be8f04f6f51d801b249dbfaf424494bb4e-init/diff:/var/lib/docker/overlay2/5c52947b10b248b7ef34576df0be71d925f706a3d3cb2896276fc3171c118a2c/diff:/var/lib/docker/overlay2/33307f601386a2e6c661d1d15c66be0694f4de024f24ab84bf148657046f067f/diff:/var/lib/docker/overlay2/906a133ea323788d4adfd29699dc952d368a15a2480c8300ce1514b6fcbe2b0e/diff:/var/lib/docker/overlay2/129c02c3e53c6b2ba443ba62ceaaf58aec7cc89ca4f338b51e97a0250279872d/diff:/var/lib/docker/overlay2/bb808c0382aa74ab1f015550dbb80d0d779fa90868478fa05bf9a3cfcbd02ef6/diff:/var/lib/docker/overlay2/54e0c839292d19bee454feaf85c76ae4a99fe0c6c13b903cc397a1bc0e00de7b/diff",
"MergedDir": "/var/lib/docker/overlay2/400ba5daf7d017c73c449c023bb8c5be8f04f6f51d801b249dbfaf424494bb4e/merged",
"UpperDir": "/var/lib/docker/overlay2/400ba5daf7d017c73c449c023bb8c5be8f04f6f51d801b249dbfaf424494bb4e/diff",
"WorkDir": "/var/lib/docker/overlay2/400ba5daf7d017c73c449c023bb8c5be8f04f6f51d801b249dbfaf424494bb4e/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "d13f526816f0",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"2020/tcp": {},
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.10",
"NJS_VERSION=0.3.9",
"PKG_RELEASE=1~buster",
"mypath=/web"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "mynginx:test",
"Volumes": null,
"WorkingDir": "/web",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b6eec9deeff87b6c84fda535330d198c0a4f1a2ccf93f87d57848ae1309c3a46",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"2020/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8000"
}
],
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/b6eec9deeff8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3c1d3c63256fc8a62c264528325071b611be7d9417220244629661ff20a72553",
"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": "8ef04b7c0f9f0befc0a09cc2e12f3632afa63cca748f25c4d3df5fd7ce049f4e",
"EndpointID": "3c1d3c63256fc8a62c264528325071b611be7d9417220244629661ff20a72553",
"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
}
}
}
}
]
总结
过程一度十分艰难;实践一的问题主要在于端口号的映射;从正在运行的容器里面复制配置文件到宿主机;实践二的问题在于编写的setup.sh不适合docker hub最新版mysql的配置,只能使用mysql5.7的版本。