实现一个自定义的web服务器
这里我采用库里已有nginx镜像作为基础镜像构建自定义的nginx镜像
获取nginx镜像:
sudo docker pull nginx
【注】这里也可以先不pull镜像,直接在dockerfile中FROM(FROM会从本地或者镜像库中直接获取基础镜像)
查看nginx是否pull成功:
sudo docker images
启动nginx容器:
sudo docker run --name nginx-test -p 8080:80 -d nginx
进入nginx容器并查看其配置信息:
sudo docker exec -it 9fa0577f4261 /bin/bash
cd /etc/nginx/conf.d
cat default.conf
在主目录中创建myweb文件夹并在该文件下创建文件default.conf,修改配置文件监听端口和根目录,如下图所示:
在同一目录下创建dockerfile文件并编辑:
创建一个index.html文件:
【注】要将default.conf 、dockerfile和index.html 三个文件放在同一目录下
打开dockerfile文件存放目录:
cd /home/zlr/myweb
sudo docker build -t lrnginx .
其中,lrnginx
是所要构建镜像的名称,.
表示上下文,千万不能漏掉
验证查看lrnginx是否构建成功:
sudo docker images
启动lrnginx容器并映射端口
sudo docker run --name lrnginx -d -p 8024:8024 lrnginx
通过浏览器查看nginx网页(访问8024端口):
实现一个自定义的数据库容器服务
首先从镜像库中获取mysql:5.7镜像:
sudo docker pull mysql:5.7
验证查看是否pull成功:
sudo docker images
pull成功后,创建dockerfile:
#基础镜像
FROM mysql:5.7
#维护人信息
MAINTAINER zlr zlrong@163.com
#设置免密登录
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"]
编写容器启动脚本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.修改密码完毕...'
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 `user`;
CREATE TABLE `user`(
`id` bigint(20) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- 插入数据
INSERT INTO `user`(`id`,`name`,`email`,'age`)
VALUES
(0,'lrong','zlrong@163.com',20);
需要测试,为了方便就只写了一个user表
创建mysql权限设置命令privileges.sql:
use mysql;
select host,user from user;
-- 因为mysql版本是5.7,因此新建用户为如下命令:
create user lrong identified by '666';
--将docker_mysql数据库的权限授权给创建的lrong用户,密码为666:
grant all on docker_mysql.* to lrong@'%' identified by '666' with grant option;
-- 这一条命令一定要有:
flush privileges;
【注】dockerfile、setup.sh、schema.sql、privileges.sql这四个文件都放在主目录下的mysql文件下
创建镜像
sudo docker build -t lrmysql .
执行docker images查看该镜像是否构建成功:
创建成功。
启动lrmysql容器并将端口映射到本地的3306端口:
sudo docker run --name lrmysql -p 3306:3306 -d lrmysql
容器启动成功。
查看容器的日志记录,启动过程是否与启动脚本setup.sh中的步骤一致:
验证结果:
执行exec命令进入lrmysql容器:
sudo docker exec -it lrmysql /bin/bash
使用lrong用户登录数据库:
mysql -u lrong -p
输入密码666通过验证
查看数据库:
show databases
切换至docker_mysql数据库:
use docker_mysql
查询user表并退出mysql
select * from user
查看lrmysql容器配置:
sudo docker inspect [容器ID]
[
{
"Id": "a1eb1491689b515e4496ae1b105b2edbf647a8efbe1f3798b0c36f97f7a61667",
"Created": "2020-04-23T04:23:27.565452688Z",
"Path": "docker-entrypoint.sh",
"Args": [
"sh",
"/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": "/lrmysql",
"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
}
}
}
}
]
实现过程中遇到的问题及解决方案
实现nginx容器服务中遇到的boss
遇到的问题1.在自定义nginx镜像构建成功后,打不开nginx网页
解决方案:因为在配置文件default.conf中改变了根目录,所以要在dockerfile中添加一条命令COPY index.html /home/zlr/myweb
实现mysql容器服务中遇到的boss
遇到的问题2:在自定义mysql镜像构建成功后,执行exec时但无法进入lrmysql容器,显示如下报错内容:container is not running(当时忘记截图了)
解决方案:执行docker logs lrmysql命令查看哪里出错了,是不是容器启动过程出现了错误,果然在这里出现了错误,如下图:
然后我根据报错内容进行了查错修改,问题出在了schema.sql文件上,于是修正过来,删除构建好的lrmysql,重新进行构建,之后就没有出现问题,所以查看docker logs这一步很重要,尽量查看一下是否报错。