前言
上一次作业同学们实现了docker环境的安装和基本配置,已经能够从docker的官方镜像仓库中拉取镜像,初步实现了入门。然而,从官方仓库拉取的镜像是最简单的镜像,在实际应用中经常需要根据需求额外修改镜像,这就涉及到了镜像的定制。在docker中我们可以使用Dockerfile文件实现自定义镜像,本次作业需要完成2个Dockerfile编写实践,掌握编写Dockerfile文件的基本方法
(1) 实现一个自定义的web容器服务
选择使用nginx来解决问题
获取nginx镜像
查看镜像情况
安装完成后,执行命令运行
$ docker run --name nginx-test -p 8080:80 -d nginx
通过浏览器访问8080端口查看
创建自己的文件夹,并且把三个文件放入其中,分别是Dockerfile default.conf 和index
先查看nginx的配置信息
sudo docker exec -it 9fa0577f4261 /bin/bash
cd /etc/nginx/conf.d
cat default.conf
复制信息,并且将端口和根目录部分修改
此处我改成自己的5038端口 可以改不同的
用vim写index文件
用vim写Dockerfile文件
放在自己上次创建的文件夹里面
三个文件都写好了之后,在该目录下运行构建镜像
docker build -t cghnginx .
运行容器
docker run --name lnginx -d -p 5038:5038 cghnginx
就可以查看结果了
(2) 实现一个自定义的数据库容器服务
先拉取镜像
sudo docker pull mysql:5.7
查看镜像拉取情况
开始写配置文件
同样创建一个该有的文件夹,用vim写配置文件
写Dockerfile文件(纯文本复制上博客会字体异常,目前还没解决,只能放图了)
写privileges.sql
写schema.sql
写setup.sh
配置文件写好了,就可以创建镜像了
创建镜像
sudo docker build -t cghmysql .
运行容器 ,将端口映射到3306
docker run --name twomysql -d -p 3306:3306 cghmysql
可以查看一下容器,看看是否正确运行
sudo docker ps
运行成功之后执行exec进入容器
sudo docker exec -it be30f8159b6b /bin/bash
那个一串的ID就是容器ID
使用docker用户登陆
mysql -u docker -p (这里的docker和你配置文件privileges.sql中的名字相关,不一定是docker)
输入密码通过验证
输入 use docker_mysql
查看数据库
select * from testone;
输入exit 退出
查看容器配置信息
docker inspect be30f8159b6b
[
{
"Id": "be30f8159b6b92b43319a35f1197c4afb1dc5a8e4addcee2bfc02d345adf3a75",
"Created": "2020-04-23T09:06:30.327859918Z",
"Path": "docker-entrypoint.sh",
"Args": [
"sh",
"/usr/mysql/setup.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 10297,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-23T09:06:31.433344974Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:c99839aad857c1ce1d1dab89598553b9ec25c33f89f780df774642139c9b5fcb",
"ResolvConfPath": "/var/lib/docker/containers/be30f8159b6b92b43319a35f1197c4afb1dc5a8e4addcee2bfc02d345adf3a75/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/be30f8159b6b92b43319a35f1197c4afb1dc5a8e4addcee2bfc02d345adf3a75/hostname",
"HostsPath": "/var/lib/docker/containers/be30f8159b6b92b43319a35f1197c4afb1dc5a8e4addcee2bfc02d345adf3a75/hosts",
"LogPath": "/var/lib/docker/containers/be30f8159b6b92b43319a35f1197c4afb1dc5a8e4addcee2bfc02d345adf3a75/be30f8159b6b92b43319a35f1197c4afb1dc5a8e4addcee2bfc02d345adf3a75-json.log",
"Name": "/twomysql",
"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/d5f4a56017937d632d95beeca92366bfff226e92de7124b29eef9cd4617ae661-init/diff:/var/lib/docker/overlay2/a9ca2914311e50be534fb9deb6a4f2d853a1f9b1f64d5459556454810854913f/diff:/var/lib/docker/overlay2/6534681dcb7780e7d3813301ead14cb82fc54d29dbe75a70e5cf0689f962226b/diff:/var/lib/docker/overlay2/b259467fd3fd0f8cf3c85a1ac4ccc32c32166f7b1878372404c1ea9c03eb1d33/diff:/var/lib/docker/overlay2/e48ce7649f6e5db7af9c9853b40a9b0f2e69f2213e4e8bc652fb35a30a28771f/diff:/var/lib/docker/overlay2/3ea5ed62640e9df3117ab69aedcfd1c4f1918045062e20519e3e9e3879f4407f/diff:/var/lib/docker/overlay2/5642c4011e2f205d027563a2c8b2261f3bbbb5e5077121a82ad50360edaf08c8/diff:/var/lib/docker/overlay2/6e4102b1244235f128d3989a682db10d435ab95707111f992f53636fa352b132/diff:/var/lib/docker/overlay2/3034676b5b526fbdbb4dc2d25b168037698a25cfc601c11df18a935065fa909f/diff:/var/lib/docker/overlay2/0e79d11f8311b1bda7a2f597c5b2f9ee37eab5b305834d4963b4a38bb90e70bd/diff:/var/lib/docker/overlay2/50e579ef6a494e96743985d4e433cd2c6796a3e4879ff48a2401da82d1b08582/diff:/var/lib/docker/overlay2/1c3945507798ae598f5f459e9218a64fd8afbba8d7d66a802a5de4ed2836d843/diff:/var/lib/docker/overlay2/614f7eaa8e94fade0a53d0d218896300ed18739d0b63b193dcfc302f0d115945/diff:/var/lib/docker/overlay2/bd1ae560ddb008d05bab710f50c7f9ef7c40b331255ec5472549cba8332228a7/diff:/var/lib/docker/overlay2/bd57a9450bdfe3316cea94b9fc88b847934d5f0ae316c315ee25f980d4948a1e/diff",
"MergedDir": "/var/lib/docker/overlay2/d5f4a56017937d632d95beeca92366bfff226e92de7124b29eef9cd4617ae661/merged",
"UpperDir": "/var/lib/docker/overlay2/d5f4a56017937d632d95beeca92366bfff226e92de7124b29eef9cd4617ae661/diff",
"WorkDir": "/var/lib/docker/overlay2/d5f4a56017937d632d95beeca92366bfff226e92de7124b29eef9cd4617ae661/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "4bac3ab2a7d8815e46157b72744493f21121f9cfd3b9eeeed88be686f773fe0e",
"Source": "/var/lib/docker/volumes/4bac3ab2a7d8815e46157b72744493f21121f9cfd3b9eeeed88be686f773fe0e/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "be30f8159b6b",
"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",
"/usr/mysql/setup.sh"
],
"Image": "cghmysql",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ea653fae778383bfef1058644250e76c90dde4e4719d0ceb35638e55bf9bea37",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/ea653fae7783",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "71b2f58a29788ad440ce6b73a7c91d7d03cb40757ae41cc94254123a2c247e19",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "043d4fb154b3b0cbfe988233c6458be454bbcaeba7cddeff3805bace764484d7",
"EndpointID": "71b2f58a29788ad440ce6b73a7c91d7d03cb40757ae41cc94254123a2c247e19",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
(3)途中遇到的问题
没有遇到特别的大的问题,有问题的话,基本前面人的博客就可以解决了!(多谢前面的大佬们!)
可能遇到的路径配置问题算一点吧,不过也挺快解决的
(4)总结
感觉越来越难了,不过对Docker的理解貌似也逐渐变好,但还是感觉吃力,辛亏有前辈们
我的理解大概就是,写好配置文件,利用Dockerfile去写好待会儿我要怎么对这些配置文件去操作,之后,再构建镜像,运行容器
难点就是配置文件,我大多只能照猫画虎,百度配置好的,并没有太多自己的理解,道阻且长啊。