一、docker 基于容器创建本地web容器化镜像
1、启动指定buysbox 镜像
bash
docker run --name b1 -it busybox:latest
2、创建目录,并创建html
bash
mkdir -p /data/html
vi index.html
内容自定义例如:<h1>welcome to busybox<h1>
3、新增窗口,创建镜像
bash
docker commit -a "user<6666@qq.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html/"]' -p b1 rep/user:v3.0
命令解析:
bash
[root@localhost ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
-a 是设置作者的名字和邮箱
-c 是指定 dockerfile 的配置。例如上面是执行CMD里面的命令。让他启动容器时候就运行
/bin/httpd -f -h /data/html/ ,这样,启动容器化后就可以访问到html index.html 页面
-p 是新建容器,暂定容器
b1 是启动的容器化的命名
查看镜像
bash
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
rep/user v3.0 ac68fbf602d6 11 seconds ago 1.24MB
rep/tag v3.0 d9b9295a2e80 19 minutes ago 1.24MB
rep/tag v2.0 89014fda351b 51 minutes ago 1.24MB
res/wuhuhuan v1.1.1 89014fda351b 51 minutes ago 1.24MB
busybox latest 16ea53ea7c65 2 years ago 1.24MB
quay.io/coreos/flannel v0.14.0-arm64 85fc911ceba5 2 years ago 67.2MB
nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
查看配置是否更改
bash
[root@localhost ~]# docker inspect rep/user:v3.0
[
{
"Id": "sha256:ac68fbf602d6e2271e6c96bfea658899acbb7ba74a2e83044b90e50666b48e78",
"RepoTags": [
"rep/user:v3.0"
],
"RepoDigests": [],
"Parent": "sha256:16ea53ea7c652456803632d67517b78a4f9075a10bfdc4fc6b7b4cbf2bc98497",
"Comment": "",
"Created": "2024-02-08T08:35:17.347089055Z",
"Container": "b59d841d65102533088e703838bd3df3ab582a816644c369b703fd03f60b4d4a",
"ContainerConfig": {
"Hostname": "b59d841d6510",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"sh"
],
"Image": "busybox",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "20.10.8",
"Author": "user<6666@qq.com>",
"Config": {
"Hostname": "b59d841d6510",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/httpd",
"-f",
"-h",
"/data/html/"
],
"Image": "busybox",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1235989,
"VirtualSize": 1235989,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/1d3ee61fa87959f86ffcdc962b53b312f4990382fb2c800708019c8472c8041a/diff",
"MergedDir": "/var/lib/docker/overlay2/156150a591d65edb67085d724f3aca64803258c434ced16eba09255477f75907/merged",
"UpperDir": "/var/lib/docker/overlay2/156150a591d65edb67085d724f3aca64803258c434ced16eba09255477f75907/diff",
"WorkDir": "/var/lib/docker/overlay2/156150a591d65edb67085d724f3aca64803258c434ced16eba09255477f75907/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:cfd97936a58000adc09a9f87adeeb7628a2c71d11c4998e6e7f26935fa0cd713",
"sha256:58e02db36919351af6ebda7ac2644efa7cafdb3608e78d67fe63290a38990707"
]
},
"Metadata": {
"LastTagTime": "2024-02-08T03:35:17.3524158-05:00"
}
}
]
4、启动镜像
bash
[root@localhost ~]# docker run --name t6 rep/user:v3.0
5、查看启动容器ip
bash
[root@localhost ~]# docker inspect t6
[
{
"Id": "33ec9c548bfd0e6b439ae45dfde4b57e7c7b69b71a32f94ef5554b9e0df9c6ef",
"Created": "2024-02-08T08:37:33.895339149Z",
"Path": "/bin/httpd",
"Args": [
"-f",
"-h",
"/data/html/"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3113,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-02-08T08:37:34.38201913Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:ac68fbf602d6e2271e6c96bfea658899acbb7ba74a2e83044b90e50666b48e78",
"ResolvConfPath": "/var/lib/docker/containers/33ec9c548bfd0e6b439ae45dfde4b57e7c7b69b71a32f94ef5554b9e0df9c6ef/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/33ec9c548bfd0e6b439ae45dfde4b57e7c7b69b71a32f94ef5554b9e0df9c6ef/hostname",
"HostsPath": "/var/lib/docker/containers/33ec9c548bfd0e6b439ae45dfde4b57e7c7b69b71a32f94ef5554b9e0df9c6ef/hosts",
"LogPath": "/var/lib/docker/containers/33ec9c548bfd0e6b439ae45dfde4b57e7c7b69b71a32f94ef5554b9e0df9c6ef/33ec9c548bfd0e6b439ae45dfde4b57e7c7b69b71a32f94ef5554b9e0df9c6ef-json.log",
"Name": "/t6",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"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/32516427ad632cfc78a84d39ba0b17551a3242cbc8fdb3c7a32a264dd28ca05e-init/diff:/var/lib/docker/overlay2/156150a591d65edb67085d724f3aca64803258c434ced16eba09255477f75907/diff:/var/lib/docker/overlay2/1d3ee61fa87959f86ffcdc962b53b312f4990382fb2c800708019c8472c8041a/diff",
"MergedDir": "/var/lib/docker/overlay2/32516427ad632cfc78a84d39ba0b17551a3242cbc8fdb3c7a32a264dd28ca05e/merged",
"UpperDir": "/var/lib/docker/overlay2/32516427ad632cfc78a84d39ba0b17551a3242cbc8fdb3c7a32a264dd28ca05e/diff",
"WorkDir": "/var/lib/docker/overlay2/32516427ad632cfc78a84d39ba0b17551a3242cbc8fdb3c7a32a264dd28ca05e/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "33ec9c548bfd",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/httpd",
"-f",
"-h",
"/data/html/"
],
"Image": "rep/user:v3.0",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "19fd1b6c96beb856720a7308137b8a68811cadaf9381ee964ded4341cec32b73",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/19fd1b6c96be",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "b0e8a95b41d4362018942961d47615ab6cad056565b95f5eba4a2f7b433937d3",
"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": "1dacca69e1651440892c19ca1297000fdf80ae51f33c02ec5f53d20753b1ebb0",
"EndpointID": "b0e8a95b41d4362018942961d47615ab6cad056565b95f5eba4a2f7b433937d3",
"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
}
}
}
}
]
6、curl 容器ip地址,可以看到配置生效
bash
[root@localhost ~]# curl 172.17.0.2
<h1>welcome to busybox<h1>
二、给容器打标签
bash
[root@localhost ~]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
1、用法 docker tag 源标签 新标签
如果没有标签,源标签用IMAGE ID如下图
2、通过image_id命令来打标签
bash
docker tag 160a180ce384 rep/user1:v4.0
3. 通过标签,替换标签命令如下
bash
docker tag rep/user1:v4.0 rep/user2:v5.0