2020系统综合实践2 使用Docker部署Nginx和MySQL容器
目录
实现一个自定义的web容器服务
拉取nginx镜像并运行
sudo su
docker pull nginx
docker run --name some-nginx -d -p 8000:80 nginx
更改默认Web目录的思路
尝试使用了官方readme中提供的
docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx
但是似乎没啥用(也可能是我写错了),菜鸟教程也没有,就去网上找了篇教程:更改nginx网站根目录 - irisrain - 博客园。
根据教程,只要修改/usr/local/nginx/conf/nginx.conf
中指定的路径就可以了。但在Dockerfile中修改文件似乎有点麻烦。于是想了个办法:把配置文件复制出来,在外面改好了再用COPY
命令直接覆盖原文件。
提取nginx配置文件
用容器中复制文件的命令:docker cp container:filepath destpath
,尝试复制
docker cp some-nginx:/usr/local/nginx/conf/nginx.conf .
然而报错了
Error: No such container:path: some-nginx:/usr/local/nginx/conf/nginx.conf
用docker exec -it some-nginx /bin/bash
进容器看了下,确实不存在这个文件……找了半天才发现是在/etc/nginx
下
然而这个文件里面并没有server{}
。但是最后一行里有个include
,去这里看看。
这个文件里包含了server{}
,那它就是我们的目标了,把他复制到宿主机里。
docker cp some-nginx:/etc/nginx/conf.d/default.conf .
listen
监听端口值,这里修改为2420
root
为Web默认目录,这里修改为/usr/share/nginx/my_web_dir
index
访问容器时默认展示的页面
Dockerfile for my-nginx
FROM nginx
COPY default.conf /etc/nginx/conf.d/ # 替换nginx配置文件
COPY my_web_dir/ /usr/share/nginx/my_web_dir/ # 复制Web文件到容器中
EXPOSE 8000 # 声明对外暴露的端口号
- 项目目录结构
构建并运行
docker rm --force some-nginx # 之前的容器还占用着8000端口,先把它停掉
docker build -t mynginx . # 构建镜像
docker run --name myweb -d -p 8000:2420 mynginx # 运行容器
在浏览器访问localhost:8000
实现一个自定义的数据库容器服务
参考让docker中的mysql启动时自动执行sql文件 - 我是13 - 博客园
Dockerfile for my-mysql
FROM mysql:5.7
MAINTAINER qyanzh
# 不允许免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
# 将所需文件放到容器中
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.修改密码完毕....'
#sleep 3
echo `service mysql status`
echo `mysql容器启动完毕,且数据导入成功`
tail -f /dev/null
schema.sh
-- 创建数据库
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
(2420,'qyanzh');
privileges.sql
use mysql;
select host, user from user;
-- 因为mysql版本是5.7,因此新建用户为如下命令:
create user qyanzh identified by '123456';
-- 将docker_mysql数据库的权限授权给创建的qyanzh用户,密码为123456:
grant all on docker_mysql.* to qyanzh@'%' identified by '123456' with grant option;
-- 这一条命令一定要有:
flush privileges;
运行效果
查看容器内的配置信息
docker inspect mss
[
{
"Id": "384133522ada061cc695597a4a674339adcd5207d4b433b99d969410b66d5e58",
"Created": "2020-04-18T14:43:09.6751744Z",
"Path": "docker-entrypoint.sh",
"Args": [
"sh",
"/mysql/setup.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 581,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-18T14:43:10.2132393Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:7ed1a15f8feb10ed0be85f2139f9ffe9193dcfc886dceab1c5e0a184863a4649",
"ResolvConfPath": "/var/lib/docker/containers/384133522ada061cc695597a4a674339adcd5207d4b433b99d969410b66d5e58/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/384133522ada061cc695597a4a674339adcd5207d4b433b99d969410b66d5e58/hostname",
"HostsPath": "/var/lib/docker/containers/384133522ada061cc695597a4a674339adcd5207d4b433b99d969410b66d5e58/hosts",
"LogPath": "/var/lib/docker/containers/384133522ada061cc695597a4a674339adcd5207d4b433b99d969410b66d5e58/384133522ada061cc695597a4a674339adcd5207d4b433b99d969410b66d5e58-json.log",
"Name": "/mss",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3336"
}
]
},
"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/04b271fc23e13d2275b126520f7d52f8d333ac39a7e22d93797a2e2a9eb0a42b-init/diff:/var/lib/docker/overlay2/21de45ad1e19ec22aef42a9318554fbe567e8c247df370d2bacbf18dc8bac924/diff:/var/lib/docker/overlay2/d0476a686f412858249a88d83f6d7bd5424a2f83abf096588bbb1be8238736a0/diff:/var/lib/docker/overlay2/c1ca47f90218f847fe3de7e1dcba38507bef87570079b64783d7a507fd3a7239/diff:/var/lib/docker/overlay2/73854aef4ee16ce8e7038b6143c5c14e474c01dc78489ef99b7b31f99382016c/diff:/var/lib/docker/overlay2/45234d83fc887db136d2e3818096d6f6d45c1ad830ba62fa37a165d8b168df1b/diff:/var/lib/docker/overlay2/4db6b6e68b1000aa915bd35985813193a2f6d785beb51670c661101300070216/diff:/var/lib/docker/overlay2/5618db8f94dc8740b1c3aa5c8d53d13645323230c954c1675a0c731e521edc0a/diff:/var/lib/docker/overlay2/d55ba343e590ec6b202a195a4d341401e76440d3236a0b062e7f07abcf018a5e/diff:/var/lib/docker/overlay2/46b954f945dcb531acde41ab237218511055f1cb27c4155f61e295ed878bb6ae/diff:/var/lib/docker/overlay2/0bc4172736ed65014a07abd3e4b2bd69d0665c16bfe39942d1ff0204da362d35/diff:/var/lib/docker/overlay2/0e2e49ff21fe23409a99fe2f0d814201df03c558b502ffcebc4081c520baa316/diff:/var/lib/docker/overlay2/23954cbffe5a21c3dd070a0a50a1196c8338df4bb15a7a69fff82ad3d8705581/diff:/var/lib/docker/overlay2/d504dbaa6b36ff01d546387ee685b0440266538e30093d9e521ddbe144f4a954/diff:/var/lib/docker/overlay2/7f88102ac1267fb09f4e2499feb85ce1422e5fcd2e5781ae602efec3161d47d9/diff",
"MergedDir": "/var/lib/docker/overlay2/04b271fc23e13d2275b126520f7d52f8d333ac39a7e22d93797a2e2a9eb0a42b/merged",
"UpperDir": "/var/lib/docker/overlay2/04b271fc23e13d2275b126520f7d52f8d333ac39a7e22d93797a2e2a9eb0a42b/diff",
"WorkDir": "/var/lib/docker/overlay2/04b271fc23e13d2275b126520f7d52f8d333ac39a7e22d93797a2e2a9eb0a42b/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "02de0adc806ff40073116c59b0afb0049f5997d099d8e20fb2af2fa6ee5a1c62",
"Source": "/var/lib/docker/volumes/02de0adc806ff40073116c59b0afb0049f5997d099d8e20fb2af2fa6ee5a1c62/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "384133522ada",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"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"
],
"Cmd": [
"sh",
"/mysql/setup.sh"
],
"Image": "ms",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "c5ef3c225645b27b2feb397a81b528bdc20916ca89e5f35036cd52d4d7c1114a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3336"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/c5ef3c225645",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e288fe965a7c681cc6920e5325d339cdc03d300245c1ce108ab2abedcb89f636",
"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": "5181b4543c9e27bac077f9ba1fb4514574945ab8b3542aa3085b8e8ed4f7d982",
"EndpointID": "e288fe965a7c681cc6920e5325d339cdc03d300245c1ce108ab2abedcb89f636",
"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
}
}
}
}
]
碎碎念
又做了一整天,太难了,要抑郁了。