1.实现一个自定义的web容器服务
获取Nginx镜像
- sudo docker pull nginx
查看是否已安装了 nginx
- sudo docker image
运行nginx容器并查看
- sudo docker run --name nginx-test -p 8080:80 -d nginx 运行容器
- sudo docker container ps 查看正在运行中的容器
浏览器访问8080端口查看
- 在浏览器输入localhost:8080
查看原来docker的配置信息,用于后面的docker配置。记得复制
- sudo docker exec -it 9fa0577f4261 /bin/bash
- cd /etc/nginx/conf.d
- cat default.conf
创建dockerfile文件夹,并在里面添加三个文件,最终结果如下
-
default.conf文件内容
-
dockerfile文件内容
-
index.html文件内容
在dockerfile目录下构建镜像,并查看 . 不要忘记了最后的那个.
- sudo docker build -t frxnginx .
- sudo docker ps查看运行中的容器
浏览器打开4399端口查看
实现一个自定义的数据库容器服务
总的操作如下,有一些重复的不用管
-
vim dockerfile
-
vim setup.sh
-
vim schema.sql
-
vim privileges.sql
从镜像库中获取mysql:5.7镜像
- sudo docker pull mysql:5.7
启动并登陆mysql 密码为之前设的123456
查看数据库
查看容器配置信息
- sudo docker inspect 42c626319dd7
[
{
"Id": "42c626319dd762fcf6fe9191f3db9af7807d6f66484e0297cc3b153f3489b57d",
"Created": "2020-04-24T14:55:14.97083877Z",
"Path": "docker-entrypoint.sh",
"Args": [
"sh",
"/mysql/setup.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 27765,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-24T14:55:19.363204015Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:80d7096de93b7dffc4be3536dfc8b630b535fa3607b95ef09dd5ff367dcef5b9",
"ResolvConfPath": "/var/lib/docker/containers/42c626319dd762fcf6fe9191f3db9af7807d6f66484e0297cc3b153f3489b57d/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/42c626319dd762fcf6fe9191f3db9af7807d6f66484e0297cc3b153f3489b57d/hostname",
"HostsPath": "/var/lib/docker/containers/42c626319dd762fcf6fe9191f3db9af7807d6f66484e0297cc3b153f3489b57d/hosts",
"LogPath": "/var/lib/docker/containers/42c626319dd762fcf6fe9191f3db9af7807d6f66484e0297cc3b153f3489b57d/42c626319dd762fcf6fe9191f3db9af7807d6f66484e0297cc3b153f3489b57d-json.log",
"Name": "/frxmysql",
"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": "13306"
}
]
},
"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/3ba93095cbb8f89b4fdff6789fc1d16700831c8360b97cd8604573afcf841b3e-init/diff:/var/lib/docker/overlay2/907d072d3db1ec71d56afb75980b4ca63930b24fbed40536a657cd2df24c1185/diff:/var/lib/docker/overlay2/b880834a2feab9fc6514b30c4ed1d94f20ae542ff54020721d35b1f4f488483f/diff:/var/lib/docker/overlay2/0275d239c2dedb306a9a9b2376d6c6a4d231739e52609da222c826acd21a6dcb/diff:/var/lib/docker/overlay2/8677aa25a8565a173294822b5d5e1f33d3b9a3aa97ba0f95c3cf8e33e118e9a6/diff:/var/lib/docker/overlay2/4c2dea19b7c4d2e910a08b5cc2c648533e36d0b957470073b0545dade434f65a/diff:/var/lib/docker/overlay2/ac89b64f05967a4602a703dcb120d977a501593fb2f06b5328831b1f3457a594/diff:/var/lib/docker/overlay2/91fd59fe379fdfd61920ee06e5c6a35e45233428f307eb77d5aad47b33143a64/diff:/var/lib/docker/overlay2/7eb2948fb702216aeb5158a20292cd42ac6021a9786e7b816a587ee2c46893fa/diff:/var/lib/docker/overlay2/fcff0282615055aef0f60be2728483fa4bc2f8c18c95c5225879df70f146e4ab/diff:/var/lib/docker/overlay2/b0dc85566782f7d58612294ed37b35124e2cdd4f42e8f2831d66fb96d22fa259/diff:/var/lib/docker/overlay2/afa841a3a2e1b43bad1d59c989fd28afe6ac6b317046ba85afb37fe68c69e037/diff:/var/lib/docker/overlay2/27a7720b76da88629a7798e733cad2cc76d12bc90ece288249d985328b2e2f65/diff:/var/lib/docker/overlay2/22b6b9c34d3d59c2931942dfc31ef6ac2b7a3e6119752b109cbaab3cad41b61f/diff:/var/lib/docker/overlay2/717ddbcb22e7ad457801a740d7d4423e647c64a456b3aaa2736c5602d3399ace/diff",
"MergedDir": "/var/lib/docker/overlay2/3ba93095cbb8f89b4fdff6789fc1d16700831c8360b97cd8604573afcf841b3e/merged",
"UpperDir": "/var/lib/docker/overlay2/3ba93095cbb8f89b4fdff6789fc1d16700831c8360b97cd8604573afcf841b3e/diff",
"WorkDir": "/var/lib/docker/overlay2/3ba93095cbb8f89b4fdff6789fc1d16700831c8360b97cd8604573afcf841b3e/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "699b34d936e76acce362d8eb9051d3d9afb13ef313621ed73463be3090a328ef",
"Source": "/var/lib/docker/volumes/699b34d936e76acce362d8eb9051d3d9afb13ef313621ed73463be3090a328ef/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "42c626319dd7",
"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": "frxmysql",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "9b403cf73f9226f1d7a573ca6904e69de4aff9645d86f40346715db8f3c43b93",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "13306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/9b403cf73f92",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "aeb281be69dd5dd5794c3727b577b05857c1f5ec7577d9045f0eac52a919e689",
"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": "8def21b9be1c0338e8145e6f287d511dc9d2fd31db49be4173b3787de62defcf",
"EndpointID": "aeb281be69dd5dd5794c3727b577b05857c1f5ec7577d9045f0eac52a919e689",
"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
}
}
}
}
]
3.问题与心得体会
- 本次实验在做之前仔细阅读了文档,做起来还算顺利。就是每次在使用docker的时候,前面一定要加入sudo,我看有的同学不加也可以,不知道是什么原因。
- 还有比较重要的一点,在创建docker容器的时候,一定一定要看清楚指令后面需要有【 .】不然不会成功。类似这样