前言
上一次作业同学们实现了docker环境的安装和基本配置,已经能够从docker的官方镜像仓库中拉取镜像,初步实现了入门。然而,从官方仓库拉取的镜像是最简单的镜像,在实际应用中经常需要根据需求额外修改镜像,这就涉及到了镜像的定制。在docker中我们可以使用Dockerfile文件实现自定义镜像,本次作业需要完成2个Dockerfile编写实践,掌握编写Dockerfile文件的基本方法。
实现一个自定义的web容器服务
推荐apache或nginx,要求标明镜像作者信息,安装必要的工具以方便维护;设定你自己的web存放目录,安全起见,请将默认的监听端口80更改为你自定义的端口,并且声明暴露的端口,容器启动时,能直接进入web代码的存放目录。
获取Nginx镜像
查看是否安装了nginx
安装完成后,我们可以使用以下命令来运行 nginx 容器
可以通过浏览器可以直接访问 8080 端口的 nginx 服务
进入nginx容器并查看其配置信息
创建dockerfile
default.conf文件
dockerfile文件
index.html文件
三个文件都写好了之后,在该目录下运行构建镜像
其中,lqynginx是所要构建镜像的名称,.表示上下文,千万不能漏掉
验证查看lqynginx是否构建成功
启动lqynginx容器并映射端口
通过浏览器查看nginx网页(访问5039端口)
实现一个自定义的数据库容器服务
可选择Mysql,Mariadb等,要求标明镜像作者信息,为了方便维护,需要能够查看容器内的配置信息,包括但不限于网络、应用配置文件等。在环境变量中设置好数据库的root密码且不允许空密码登录,创建一个测试数据库,指定用户名和密码。
从镜像库中获取mysql:5.7镜像并查看
创建mysql文件夹
创建dockerfile
#基础镜像
FROM mysql:5.7
#维护人信息
MAINTAINER LQY
#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
#经所需文件放到容器中
COPY setup.sh /home/liqingyu/mysql/setup.sh
COPY schema.sql /home/liqingyu/mysql/schema.sql
COPY privileges.sql /home/liqingyu/mysql/privileges.sql
#设置容器启动是执行的命令
CMD ["sh", "/home/liqingyu/mysql/setup.sh"]
编写容器启动脚本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 < /home/liqingyu/mysql/privileges.sql
echo '5.修改密码完毕...'
echo `service mysql status`
echo 'mysql容器启动完毕,且数据导入成功'
tail -f /dev/null
创建需要导入数据的mysql脚本命令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
(031702539,'lqy');
创建mysql权限设置命令privileges.sql:
use mysql;
select host,user from user;
-- 因为mysql版本是5.7,因此新建用户为如下命令:
create user docker identified by '123456';
--将docker_mysql数据库的权限授权给创建的lqy用户,密码为123456:
grant all on docker_mysql.* to docker@'%' identified by '123456' with grant option;
-- 这一条命令一定要有:
flush privileges;
【注】dockerfile、setup.sh、schema.sql、privileges.sql这四个文件都放在主目录下的mysql文件下
构建镜像
运行容器 ,将端口映射到3306
到目前为止,除了文件配置出错外,我做的还算顺利吧,但是后面因为Ubuntu的硬盘容量满了的问题导致我创建的容器一直闪退,所以在此处提醒后来的同学--创建虚拟机给的硬盘容量一定要大一点!(50G左右差不多吧)
运行成功之后执行exec进入容器
sudo docker exec -it be30f8159b6b /bin/bash
使用docker用户登陆
mysql -u docker -p (这里的docker和你配置文件privileges.sql中的名字相关,不一定是docker)
切换至docker_mysql数据库:
use docker_mysql
查询user表并exit退出mysql
查看容器配置信息
sudo docker inspect [容器ID]
[
{
"Id": "a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667",
"Created": "2020-04-23T04:23:27.565452688Z",
"Path": "docker-entrypoint.sh",
"Args": [
"sh",
"/home/liqingyu/mysql/setup.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5983,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-23T04:23:28.683887516Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0f2319941fa25c668ad5c8e7c05c87151fde17e11811396beda3885c399c2e47",
"ResolvConfPath": "/var/lib/docker/containers/a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667/hostname",
"HostsPath": "/var/lib/docker/containers/a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667/hosts",
"LogPath": "/var/lib/docker/containers/a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667/a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667-json.log",
"Name": "/lqymysql",
"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/f57fc1aca42e95defd6ae63cd0b689a6b09380709479023c2efebf4ab5df1a80-init/diff:/var/lib/docker/overlay2/a72503b2d56f07e287248338284c59596b495ebcc289c163f5d351ed2f2037b5/diff:/var/lib/docker/overlay2/2669451c3ccb7d60a3c071aefd62d00ad18dfa2f1ad45637b54ff4bc605597d7/diff:/var/lib/docker/overlay2/9561ec6d28c7a3a20827e7861b938a5ddeb49c6b77fdbc87f87ce3421ef85ffb/diff:/var/lib/docker/overlay2/314cfa96c36595f4ea24bb776b4e0afe809121a24fa09e20a0e1d29662658dc9/diff:/var/lib/docker/overlay2/47f83d6cf79fb3aaee1d9029dab9599ce534ce4c35bb38367eba05b3935bfd56/diff:/var/lib/docker/overlay2/fd39211c1f8b20c2f8204d75760ca778ad4f4655d89be7a920027b4378a2c49a/diff:/var/lib/docker/overlay2/73cf12fbecf0b395144e4cdcc1afa40f3f97a3fdc241f1d6a0a617dd7c503b88/diff:/var/lib/docker/overlay2/82e5208794a275ab8b83980a1cccc4ed07c2220053e2a3fc10c568288eba182a/diff:/var/lib/docker/overlay2/732b5139f7be03f107c92b17341f7d006d3b3fe230238d262ca9becfc6347faa/diff:/var/lib/docker/overlay2/ecb0f6e3c27d1ba78b8028437f542068ce9649a3f45a6b2f5db0213dde8a384d/diff:/var/lib/docker/overlay2/8e6bfa7e49c719773127fb4d16c35fde164dd2476e6d7aeb3b99a5b67b761ec0/diff:/var/lib/docker/overlay2/2bc0d9f29c00d6f293d6ae1c7ecd4055840bcfcdca2993f087754b55435b5f98/diff:/var/lib/docker/overlay2/76486d9cf91e95e107d0bea5bc81bfaf3760cd1e6486d53266aaddcba0c2a2a4/diff:/var/lib/docker/overlay2/1e827476cfaeb5d2cac109fb88afb8ade2338ab276c2f7321477b1b1592bc74a/diff",
"MergedDir": "/var/lib/docker/overlay2/f57fc1aca42e95defd6ae63cd0b689a6b09380709479023c2efebf4ab5df1a80/merged",
"UpperDir": "/var/lib/docker/overlay2/f57fc1aca42e95defd6ae63cd0b689a6b09380709479023c2efebf4ab5df1a80/diff",
"WorkDir": "/var/lib/docker/overlay2/f57fc1aca42e95defd6ae63cd0b689a6b09380709479023c2efebf4ab5df1a80/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "4d78089b0786945199e0f154e11a1cea89a1142bb93e10ad6b3e0474fb4a3a42",
"Source": "/var/lib/docker/volumes/4d78089b0786945199e0f154e11a1cea89a1142bb93e10ad6b3e0474fb4a3a42/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "a1eb1491689b",
"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=yes"
],
"Cmd": [
"sh",
"/mysql/setup.sh"
],
"Image": "lrmysql",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "904dcddd6885b32d231a0bd99cacc00c564e96797ced1f7fe133c76b098d77ba",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/904dcddd6885",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3ec448f16281c20febead242d6955351e7c2bde675e3c17bd4c3ab41baba8cd7",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "5130acc46901ae45589989f722bf423a183cb280a2982f88c15c1eb085161508",
"EndpointID": "3ec448f16281c20febead242d6955351e7c2bde675e3c17bd4c3ab41baba8cd7",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
总结
因为配置文件写错,还有硬盘空间不足的问题(在进入数据库后面那一部分,借用了几张其他同学的截图),我耽误了不少时间,但是也对docker的各项指令和原理加深了印象。
还有就是
执行docker logs lqymysql命令查看出错也蛮关键的,可以节省大量改错的实践
最后 谢谢各位前辈们为不辞辛劳,身先士卒,为我们踩了无数的坑,谢谢你们!希望这种优良传统一直保持下去。