Dockerfile语法说明
| 命令 | 用途 |
| FROM | base image |
| RUN | 执行命令 |
| ADD | 添加文件 |
| COPY | 拷贝文件 |
| CMD | 执行命令 |
| EXPOSE | 暴露端口 |
| WORKDIR | 指定路劲 |
| MAINTAINER | 维护者 |
| ENV | 设定环境变量 |
| ENTRYPOINT | 容器入口 |
| USER | 指定用户 |
| VOLUME | mount point |
Dockerfile中的每一行都产生一个新层
镜像分层
container layer RW CMD echo "hello world" RO MAINTAINER RO FROM alpine:latest RO
Volume
提供独立于容器之外的持久化存储
docker run -d --name nginx -v /usr/share/nginx html nginx
#另外一个挂在命令
docker create -v $PWD/data/:/var/mydata --name data_container ubuntu
docker run -it --volumes-from data_container ubuntu /bin/bash
#检查
docker inspect nginx
[root@localhost nginx]# docker run -d --name nginx -v /usr/share/nginx html nginx
Unable to find image 'html:latest' locally
docker: Error response from daemon: pull access denied for html, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
[root@localhost nginx]# usermod -G docker root
[root@localhost nginx]# docker run -d --name nginx -v /usr/share/nginx html nginx
Unable to find image 'html:latest' locally
docker: Error response from daemon: pull access denied for html, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
[root@localhost nginx]# ##报错的推测
[root@localhost nginx]# ###1、可能是因为root没有进入docker组?
[root@localhost nginx]# ###2、nginx没有加入到一个系统里面
[root@localhost nginx]# ls
index.html
[root@localhost nginx]# cd ..
[root@localhost Dockerfile]# ls
app.py Dockerfile git.nowtest.top.crt git.nowtest.top.key requirements.txt
docker-compose.yml docker-machine git.nowtest.top.csr nginx VirtualBox-6.0-6.0.14_133895_el7-1.x86_64.rpm
[root@localhost Dockerfile]# mkdir ubuntu_nginx
[root@localhost Dockerfile]# cd ubuntu_nginx/
[root@localhost ubuntu_nginx]# ls
[root@localhost ubuntu_nginx]# vim Dockerfile
FROM ubuntu
MAINTAINER xbf
RUn sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nginx
COPY index.html /var/www/html
ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]
EXPOSE 80
进入 Ex 模式。输入 "visual" 回到正常模式。
:x
"Dockerfile" 8L, 243C 已写入
[root@localhost ubuntu_nginx]# vim index.html
今天是周末~
进入 Ex 模式。输入 "visual" 回到正常模式。
:x
"index.html" [新] 1L, 17C 已写入
[root@localhost ubuntu_nginx]# docker build -t w11930879/hello-nginx .
Successfully built 9038bad99662
Successfully tagged w11930879/hello-nginx:latest
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# docker run -d -p 80:80 w11930879/hello-nginx
a90a2f149bfd36865566b5a2f2ebc523e0247def5eff7440fe22b9ffa799ef26
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# curl http://localhost
今天是周末~
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# docker run -d --name nginx -v /usr/share/nginx/html nginx
f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# #检查
[root@localhost ubuntu_nginx]# docker inspect nginx
[
{
"Id": "f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e",
"Created": "2019-12-26T03:08:18.048231424Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5364,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-12-26T03:08:18.411570889Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:231d40e811cd970168fb0c4770f2161aa30b9ba6fe8e68527504df69643aa145",
"ResolvConfPath": "/var/lib/docker/containers/f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e/hostname",
"HostsPath": "/var/lib/docker/containers/f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e/hosts",
"LogPath": "/var/lib/docker/containers/f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e/f57e74290920f027a904d9933b380e98a244ecdbb7d36cd801ccea0e17de0a6e-json.log",
"Name": "/nginx",
"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,
"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/1e8250114e3d0fe7538f3a795d593b50ebc4b9702947d010846e57e8350cfb7b-init/diff:/var/lib/docker/overlay2/8d2e2c062882eae2f6ca1c8e295bc99cb55b603537b1578fac3a3fe9f2337f50/diff:/var/lib/docker/overlay2/9d0db16f8b34a57aeca4d4500ed34e3619aa665c18ac4d9741a732421949d886/diff:/var/lib/docker/overlay2/f5cae2a9984a0b662db94f25b84f3a601960e2500501b91c179816ff0ac23ba7/diff",
"MergedDir": "/var/lib/docker/overlay2/1e8250114e3d0fe7538f3a795d593b50ebc4b9702947d010846e57e8350cfb7b/merged",
"UpperDir": "/var/lib/docker/overlay2/1e8250114e3d0fe7538f3a795d593b50ebc4b9702947d010846e57e8350cfb7b/diff",
"WorkDir": "/var/lib/docker/overlay2/1e8250114e3d0fe7538f3a795d593b50ebc4b9702947d010846e57e8350cfb7b/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "4cd0329e0e2f3f42f7b96c9042103ff5621d831c8f3881f1b253b2b3808ca543",
"Source": "/var/lib/docker/volumes/4cd0329e0e2f3f42f7b96c9042103ff5621d831c8f3881f1b253b2b3808ca543/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "f57e74290920",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.6",
"NJS_VERSION=0.3.7",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": {
"/usr/share/nginx/html": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e425db4e2c349478aa8af7073702cf34f87e4c85bbe702698592fe70580088c7",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/e425db4e2c34",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3e36d5f35f16a08820fbadd4b71645d78fd55050cdacde85031c7d3dbf4f3473",
"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": "78500c7255efa674072c68b269141f7d084c105144ce73e154783f3b9e56ebc1",
"EndpointID": "3e36d5f35f16a08820fbadd4b71645d78fd55050cdacde85031c7d3dbf4f3473",
"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
}
}
}
}
]
[root@localhost ubuntu_nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f57e74290920 nginx "nginx -g 'daemon of…" 34 minutes ago Up 34 minutes 80/tcp nginx
a90a2f149bfd w11930879/hello-nginx "/usr/sbin/nginx -g …" 44 minutes ago Up 44 minutes 0.0.0.0:80->80/tcp hungry_proskuriakova
[root@localhost ubuntu_nginx]# ls /var/lib/docker/volumes/4cd0329e0e2f3f42f7b96c9042103ff5621d831c8f3881f1b253b2b3808ca543/_data
50x.html index.html
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# docker exec -it nginx /bin/bash
root@f57e74290920:/# ls /usr/share/nginx/html/
50x.html index.html
root@f57e74290920:/# exit
exit
[root@localhost ubuntu_nginx]# #另外一个挂在命令
[root@localhost ubuntu_nginx]# docker create -v $PWD/data/:/var/mydata --name data_container ubuntu
9a35ebc19b2be775898d0e100b5f3eac87314b8b9544f1e3e4758ad94e1ecf28
[root@localhost ubuntu_nginx]# ls
Dockerfile index.html
[root@localhost ubuntu_nginx]# mkdir data
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# docker run -it --volumes-from data_container ubuntu /bin/bash
root@cd21a0fe4d8a:/#
root@cd21a0fe4d8a:/#
root@cd21a0fe4d8a:/#
root@cd21a0fe4d8a:/# mount | grep mydata
/dev/mapper/centos-root on /var/mydata type xfs (rw,relatime,attr2,inode64,noquota)
root@cd21a0fe4d8a:/# cd /var/mydata/
root@cd21a0fe4d8a:/var/mydata# ls
root@cd21a0fe4d8a:/var/mydata# touch whataever.txt
root@cd21a0fe4d8a:/var/mydata# exit
exit
[root@localhost ubuntu_nginx]# ls data
whataever.txt
[root@localhost ubuntu_nginx]#
[root@localhost ubuntu_nginx]# docker inspect data_container | grep mydata
"/Dockerfile/ubuntu_nginx/data/:/var/mydata"
"Destination": "/var/mydata",
Registry 说明
| English | 中文 |
| host | 宿主机 |
| image | 镜像:nginx |
| container | 容器:运行环境(系统) |
| registry | 仓库:云,过个镜像组成的仓库 |
| daemon | 守护程序 |
| client | 客户端 |
操作命令
docker search whalesay
docker pull whalesay
docker push myname/whalesay
国内的一些仓库
因为直接从国外下载很慢,所以产生了国内网址下载
daocloud
时速云
aliyun

