Docker入門(二)基本操作
1、docker客戶端命令。輸入docker查看docker用法的語法和相關的命令。
[root@localhost ~]# docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: config Manage Docker configs container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container‘s changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container‘s filesystem events Get real time events from the server exec Run a command in a running container export Export a container‘s filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run ‘docker COMMAND --help‘ for more information on a command.
2、啟動docker。
[root@localhost ~]# docker run ubuntu /bin/echo "hello docker" ###基本運行,打印後退出 hello docker [root@localhost ~]# docker run -i -t -h centosv2 centos /bin/bash ###交互式創建容器後進入容器並命名為centosv2 [root@localhost ~]# docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" ###後臺運行 5011198d1704af7eac453f80e58fa4950e6455c4454b28fa8a877a5f69cdb8fc
3、docker端口的映射
[root@localhost ~]#docker run -t -P httpd ---指定容器綁定的網絡地址,比如綁定 127.0.0.1。 [root@localhost ~]#docker run -t -p 8888:80 httpd ---指定容器80端口通過主機端口地址8888來進行訪問 [root@localhost ~]#docker run -t -p 172.20.66.150:8888:80 httpd ---指定容器綁定的網絡地址默認tcp端口 [root@localhost ~]#docker run -t -p 172.20.66.150:8888:80/udp httpd ---指定udp端口 -P :是容器內部端口隨機映射到主機的高端口。 -p : 是容器內部端口綁定到指定的主機端口。 8888:80(主機端口:容器的端口)
4、查看docker容器。
[root@localhost ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don‘t truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5011198d1704 ubuntu "/bin/sh -c ‘while t…" 6 minutes ago Up 6 minutes laughing_mirzakhani
5f55b9949e7b centos "/bin/bash" 18 minutes ago Up 18 minutes centosv3
5、停止docker容器。docker stop CONTAINER或ID
[root@localhost ~]# docker stop --help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
[root@localhost ~]# docker stop centosv3
centosv3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5011198d1704 ubuntu "/bin/sh -c ‘while t…" 8 minutes ago Up 8 minutes laughing_mirzakhani
6、啟動docker.
[root@localhost ~]# docker start nginxv2
nginxv2
7、刪除docker容器.刪除容器時,容器必須是停止狀態,否則會報如下錯誤
[root@localhost ~]# docker rm 2c1aed05442e
2c1aed05442e
[root@localhost ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
8、查看docker日誌
[root@localhost ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
[root@localhost ~]# docker logs -f 5011198d1704
hello world
9、檢查docker應用程序的進程
[root@localhost ~]# docker top nginxv2
UID PID PPID C STIME TTY TIME CMD
root 22446 22428 0 17:56 ? 00:00:00 nginx: master process nginx -g daemon off;
101 22490 22446 0 17:56 ? 00:00:00 nginx: worker process
10、查看docker底層信息,它會返回一個 JSON 文件記錄著 Docker 容器的配置和狀態信息。
[root@localhost ~]# docker inspect nginxv2
[
{
"Id": "51fa7e93382191d3d52dfd8231baa47438f30472a9ddb432cac95f2cab09c377",
"Created": "2018-10-23T07:35:16.522767558Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 22446,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-10-24T09:56:50.60288752Z",
"FinishedAt": "2018-10-23T07:35:20.710607429Z"
},
"Image": "sha256:dbfc48660aeb7ef0ebd74b4a7e0822520aba5416556ee43acb9a6350372e516f",
"ResolvConfPath": "/var/lib/docker/containers/51fa7e93382191d3d52dfd8231baa47438f30472a9ddb432cac95f2cab09c377/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/51fa7e93382191d3d52dfd8231baa47438f30472a9ddb432cac95f2cab09c377/hostname",
"HostsPath": "/var/lib/docker/containers/51fa7e93382191d3d52dfd8231baa47438f30472a9ddb432cac95f2cab09c377/hosts",
"LogPath": "/var/lib/docker/containers/51fa7e93382191d3d52dfd8231baa47438f30472a9ddb432cac95f2cab09c377/51fa7e93382191d3d52dfd8231baa47438f30472a9ddb432cac95f2cab09c377-json.log",
"Name": "/nginxv2",
"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,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"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,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/asound",
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/e5db810fb9cc01e72997ab28548f8c6afdf288ad68b67526b8a08db21f11cc4d-init/diff:/var/lib/docker/overlay2/9aa976c9f96e990dc230b219d4c004c40b83c018a2f4f5c7e8aeeddce78d56f9/diff:/var/lib/docker/overlay2/aeb079bd298ea309e97c1ca135f5cedb5832a4222477479f00c04e20eac5bda0/diff:/var/lib/docker/overlay2/a69b7899845f36bdc82307abbec8b0150892f4f30ba2f59fedd076b5ba84cd46/diff",
"MergedDir": "/var/lib/docker/overlay2/e5db810fb9cc01e72997ab28548f8c6afdf288ad68b67526b8a08db21f11cc4d/merged",
"UpperDir": "/var/lib/docker/overlay2/e5db810fb9cc01e72997ab28548f8c6afdf288ad68b67526b8a08db21f11cc4d/diff",
"WorkDir": "/var/lib/docker/overlay2/e5db810fb9cc01e72997ab28548f8c6afdf288ad68b67526b8a08db21f11cc4d/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "51fa7e933821",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.15.5-1~stretch",
"NJS_VERSION=1.15.5.0.2.4-1~stretch"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <[email protected]>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "8fb89b8da81ef113f293fff99eba54a8dddc8027a47ec5e3094b0c50023cb9c7",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/8fb89b8da81e",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c6f8a321d3fb49fa82d27e1a430639fd1617df75b86641e8deb720c0ce57989b",
"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": "56fd3fad5cf9211915ead97854e1bf8ef6e061ea1409264cb92c78296cc92314",
"EndpointID": "c6f8a321d3fb49fa82d27e1a430639fd1617df75b86641e8deb720c0ce57989b",
"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
}
}
}
}
]
(11)連接到正在運行中的容器。docker exec|docker attach |nsenter
attach命令:docker attach name | id
docker exec :在運行的容器中執行命令(運行中容器啟動新進程)
[root@localhost ~]# cat /home/docker_pid.sh
#!/bin/bash
docker_in(){
NAME_ID=$1
PID=$(docker inspect -f "{{ .State.Pid }}" $NAME_ID)
nsenter -t $PID -m -u -i -n -p
}
docker_in $1
[root@localhost ~]# docker attach nginxv2
[root@localhost ~]# docker exec -i -t nginxv2 /bin/bash
root@51fa7e933821:/#
[root@localhost ~]# sh /home/docker_pid.sh nginxv2
root@51fa7e933821:/#
(二)docker鏡像的使用
1、docker鏡像的使用
[root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don‘t truncate output
-q, --quiet Only show numeric IDs
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ea4c82dcd15a 6 days ago 85.8MB
grafana/grafana latest 7038dbc9a50c 8 days ago 223MB
nginx latest dbfc48660aeb 8 days ago 109MB
centos latest 75835a67d134 2 weeks ago 200MB
REPOSITORY:表示鏡像的倉庫源
TAG:鏡像的標簽
IMAGE ID:鏡像ID
CREATED:鏡像創建時間
SIZE:鏡像大小
2、搜索鏡像
[root@localhost ~]# docker search zabbix
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 136 [OK]
zabbix/zabbix-agent Zabbix agent with TLS encryption support 83 [OK]
monitoringartist/zabbix-3.0-xxl Please use our better compatible Zabbix imag… 75 [OK]
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 74 [OK]
berngp/docker-zabbix Runs Zabbix Server and Zabbix Web UI on a Ce… 72 [OK]
monitoringartist/zabbix-xxl Zabbix 3.x - server, web, proxy, java gatewa… 67 [OK]
zabbix/zabbix-server-pgsql Zabbix server with PostgreSQL database suppo… 33 [OK]
zabbix/zabbix-web-nginx-pgsql Zabbix frontend based on Nginx with PostgreS… 20 [OK]
zabbix/zabbix-appliance Zabbix appliance with MySQL database support… 18 [OK]
zabbix/zabbix-web-apache-mysql Zabbix frontend based on Apache web-server w… 17 [OK]
zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 16 [OK]
zabbix/zabbix-proxy-sqlite3 Zabbix proxy with SQLite3 database support 15 [OK]
zabbix/zabbix-java-gateway Zabbix Java Gateway 14 [OK]
bhuisgen/docker-zabbix-coreos Zabbix agent for CoreOS server 14 [OK]
monitoringartist/zabbix-db-mariadb Docker image of MariaDB optimized for Zabbix 11 [OK]
million12/zabbix-agent Zabbix Agent - running in foreground. 7 [OK]
zabbix/zabbix-snmptraps Receiving SNMP traps to Zabbix server or Zab… 7 [OK]
zabbix/zabbix-web-apache-pgsql Zabbix frontend based on Apache web-server w… 3 [OK]
digiapulssi/docker-zabbix-agent Dockerized Zabbix agent for host and contain… 2 [OK]
openshifttools/oso-centos7-zabbix-server Zabbix server container using external SQL d… 2
blackcobra1973/zabbix-db-postgresql PostgreSQL on CentOS 7.x with Zabbix 3.0 dat… 2 [OK]
monitoringartist/zabbix-2.4 Dockerized Zabbix 2.4 - server, web UI - dep… 1 [OK]
qk4l/zabbix-cachet Python script for Zabbix to Cachet integrati… 0 [OK]
signnow/zabbix-proxy-mysql Zabbix Proxy (MySQL version) with Python 3.5… 0
openshifttools/oso-centos7-zabbix-web Zabbix web container to handle web view into… 0
NAME:鏡像倉庫源的名稱
DESCRIPTION:鏡像的描述
OFFICIAL:是否docker官方發布
3、獲取鏡像
[root@localhost ~]# docker pull zabbix/zabbix-server-mysql
Using default tag: latest
latest: Pulling from zabbix/zabbix-server-mysql
d6a5679aa3cf: Pull complete
b5f72c9bbb93: Pull complete
e901ef2029a4: Pull complete
9ba83f201d25: Pull complete
Digest: sha256:f385294e950ec855338df5e6bec41ed708cc6cbd00e1ba266d1c425d0ed9fd1e
Status: Downloaded newer image for zabbix/zabbix-server-mysql:latest
4、查看鏡像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ea4c82dcd15a 6 days ago 85.8MB
grafana/grafana latest 7038dbc9a50c 8 days ago 223MB
nginx latest dbfc48660aeb 8 days ago 109MB
centos latest 75835a67d134 2 weeks ago 200MB
zabbix/zabbix-server-mysql latest efffb1148f04 2 weeks ago 62.6MB
5、更新鏡像
[root@localhost ~]# docker run -t -i ubuntu /bin/bash
root@a9247c958d01:/# apt-get upgrade
Reading package lists... Done
Get:16 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [2975 B]
Fetched 15.0 MB in 6s (2714 kB/s)
Reading package lists... Done
root@a9247c958d01:/# apt-get install vim -y
Reading package lists... Done
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn‘t exist
Processing triggers for libc-bin (2.27-3ubuntu1) ...
root@a9247c958d01:/# hostname
a9247c958d01
root@a9247c958d01:/# exit;
exit
[root@localhost ~]# docker commit -m="apt-get update" -a=lqb a9247c958d01 ubuntu/lqbv2
sha256:61e3695d2d9fd6c5800fba1119335c5f146bfa859a35591d29c725e6fdaafe07
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu/lqbv2 latest 61e3695d2d9f 4 minutes ago 169MB
各個參數說明:
-m:提交的描述信息
-a:指定鏡像作者
a9247c958d01:容器ID
runoob/ubuntu:v2:指定要創建的目標鏡像名
創建更新鏡像的相關說明:
首先創建用ubuntu鏡像創建容器。
運行的容器內使用 apt-get update 命令進行更新。
在完成操作之後,輸入 exit命令來退出這個容器。
此時ID為a9247c958d01的容器,是按我們的需求更改的容器。我們可以通過命令 docker commit來提交容器副本
6、構建鏡像。通過docker build從零開始來創建一個新的鏡像。為此,我們需要創建一個 Dockerfile 文件,其中包含一組指令來告訴 Docker 如何構建我們的鏡像。
[root@localhost ~]# mkdir /opt/dockerfile
[root@localhost dockerfile]# mkdir centos
[root@localhost dockerfile]# cd centos/
[root@localhost centos]# vim Dockfile
[root@localhost centos]# vim Dockerfile
FROM centos
MAINTAINER lqb
RUN yum install vim -y
RUN yum install tree -y
RUN /bin/echo ‘root:123456‘ |chpasswd
RUN useradd lqb
RUN /bin/echo ‘lqb:123456‘ |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
[root@localhost centos]# docker build -t centos/lqb:lqbv2 .
Sending build context to Docker daemon 2.048kB
Step 1/11 : FROM centos
---> 75835a67d134
Step 2/11 : MAINTAINER lqb
---> Using cache
---> 386b6f69c473
Step 3/11 : RUN yum install vim -y
---> Using cache
---> 745792f01962
Step 4/11 : RUN yum install tree -y
---> Using cache
---> 2bae22865096
Step 5/11 : RUN /bin/echo ‘root:123456‘ |chpasswd
---> Using cache
---> f554337a5fee
Step 6/11 : RUN useradd lqb
---> Using cache
---> 7f08790bd10d
Step 7/11 : RUN /bin/echo ‘lqb:123456‘ |chpasswd
---> Using cache
---> 40a26046eb5a
Step 8/11 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
---> Using cache
---> af9ca664315f
Step 9/11 : EXPOSE 22
---> Using cache
---> e24bed6f62fe
Step 10/11 : EXPOSE 80
---> Using cache
---> 938e49766589
Step 11/11 : CMD /usr/sbin/sshd -D
---> Using cache
---> 466ccf2de68e
Successfully built 466ccf2de68e
Successfully tagged centos/lqb:lqbv2
[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/lqb lqbv2 466ccf2de68e 8 minutes ago 455MB
<none> <none> 9d09c724aa58 2 hours ago 200MB
每一個指令都會在鏡像上創建一個新的層,每一個指令的前綴都必須是大寫的。
第一條FROM,指定使用哪個鏡像源
RUN 指令告訴docker 在鏡像內執行命令,安裝了什麽。
ADD添加相應的文件,會自動解壓
WORKDIR設置當前的工作目錄
VOLUME設置卷,掛載主機目錄
EXPOSE指定對外的端口
CMD指定容器啟動後要幹的事情
參數說明:
-t :指定要創建的目標鏡像名
. :Dockerfile 文件所在目錄,可以指定Dockerfile 的絕對路徑
7、設置鏡像標簽
[root@localhost centos]# docker tag 9d09c724aa58 centoslqb1:lqbcentosv1
[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/lqb lqbv2 466ccf2de68e 23 minutes ago 455MB
centoslqb1 lqbcentosv1 9d09c724aa58 3 hours ago 200MB
8、刪除鏡像。通過docker rmi來進行刪除。
[root@localhost centos]# docker rmi 9d09c724aa58
Untagged: centoslqb1:lqbcentosv1
Deleted: sha256:9d09c724aa58ee7bf22e1b3ed7aaa53437c4e57eaa1ed47b66fbde62d3946a75
[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/lqb lqbv2 466ccf2de68e 28 minutes ago 455MB
(三)容器的連接
通過一個端口實現連接一個docker容器
1、網絡端口的映射-----隨機映射。docker run -P 。-P :是容器內部端口隨機映射到主機的高端口。
[root@localhost centos]# docker run -t -i -d -P httpd /bin/bash
273c5df97d67e8bd256deb22b3b325f56a3c7b25cd113509cfffe3346ceaa285
[root@localhost centos]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
273c5df97d67 httpd "/bin/bash" 4 seconds ago Up 4 seconds 0.0.0.0:32773->80/tcp quirky_stonebraker
51fa7e933821 nginx "nginx -g ‘daemon of…" 2 days ago Up 21 hours 80/tcp nginxv2
2、網絡端口的映射-----指定映射 docker run -d -p 。
[root@localhost centos]# docker run -t -p 8888:80 httpd
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName‘ directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName‘ directive globally to suppress this message
[Thu Oct 25 08:02:42.006060 2018] [mpm_event:notice] [pid 1:tid 140406403273920] AH00489: Apache/2.4.37 (Unix) configured -- resuming normal operations
[Thu Oct 25 08:02:42.006225 2018] [core:notice] [pid 1:tid 140406403273920] AH00094: Command line: ‘httpd -D FOREGROUND‘
172.20.18.255 - - [25/Oct/2018:08:02:51 +0000] "GET / HTTP/1.1" 200 45
3、指定ip地址的映射
[root@localhost centos]# docker run -t -p 172.20.66.113:8888:80 httpd
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName‘ directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName‘ directive globally to suppress this message
[Thu Oct 25 08:03:44.093560 2018] [mpm_event:notice] [pid 1:tid 140221092869312] AH00489: Apache/2.4.37 (Unix) configured -- resuming normal operations
[Thu Oct 25 08:03:44.093720 2018] [core:notice] [pid 1:tid 140221092869312] AH00094: Command line: ‘httpd -D FOREGROUND‘
4、指定相關的傳輸協議的映射
[root@localhost centos]# docker run -t -p 172.20.66.113:8000:80/udp httpd
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.3. Set the ‘ServerName‘ directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.3. Set the ‘ServerName‘ directive globally to suppress this message
[Thu Oct 25 08:05:33.630321 2018] [mpm_event:notice] [pid 1:tid 139959673029824] AH00489: Apache/2.4.37 (Unix) configured -- resuming normal operations
[Thu Oct 25 08:05:33.630494 2018] [core:notice] [pid 1:tid 139959673029824] AH00094: Command line: ‘httpd -D FOREGROUND‘
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
433ec39bebfe httpd "httpd-foreground" 31 seconds ago Up 31 seconds 80/tcp, 172.20.66.113:8000->80/udp fervent_edison
27161fa1179a httpd "httpd-foreground" 2 minutes ago Up 2 minutes 172.20.66.113:8888->80/tcp vigilant_spence
備註:
-p hostPort:containerPort
-p ip:hostPort:containerPort
-p ip::containerPort
-p hostPort:containerPort:udp
-p 81:80 –p 443:443
(四)、數據卷的掛載
1、掛載默認路徑的卷
[root@localhost centos]# docker run -d --name nginx-volume -v /data nginx
36195574d6f468d008197c81066893b99e9459079ee6953c768a3dd2f2732b4c
[root@localhost centos]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
36195574d6f4 nginx "nginx -g ‘daemon of…" 9 seconds ago Up 8 seconds 80/tcp nginx-volume
27161fa1179a httpd "httpd-foreground" 11 minutes ago Up 11 minutes 172.20.66.113:8888->80/tcp vigilant_spence
[root@localhost centos]# sh /home/docker_pid.sh 36195574d6f4
root@36195574d6f4:/# hostname
36195574d6f4
root@36195574d6f4:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 50G 8.4G 42G 17% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 8.4G 42G 17% /data
shm 64M 0 64M 0% /dev/shm
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /proc/scsi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
root@36195574d6f4:/# touch /data/2.log
root@36195574d6f4:/# ls /data/
1.log 2.log test
[root@localhost data]# docker inspect -f {{.Mounts}} nginx-volume
[{volume b989bef7efdc81fe63c76914f5f2e4621db327a5bf5136fab5b9dd09f994b23c /var/lib/docker/volumes/b989bef7efdc81fe63c76914f5f2e4621db327a
[root@localhost data]# cd /var/lib/docker/volumes/b989bef7efdc81fe63c76914f5f2e4621db327a5bf5136fab5b9dd09f994b23c/_data/
1.log test/
[root@localhost data]# cd /var/lib/docker/volumes/b989bef7efdc81fe63c76914f5f2e4621db327a5bf5136fab5b9dd09f994b23c/_data/
[root@localhost _data]# ls
[root@localhost _data]# ls
1.log 2.log test
所有的文件都是掛載到主機var/lib/docker/volumes/b989bef7efdc81fe63c76914f5f2e4621db327a5bf5136fab5b9dd09f994b23c/_data /下
2、指定好相關路徑的數據卷
[root@localhost _data]# mkdir /data/volumnes
[root@localhost _data]# docker run -d --name nginx-volumes2 -v /data/volumnes/:/data/ nginx
5a4e5e692732368888f3b9c4ce920b8281ca23b0eaf01f3ba93392d84e63c3ff
[root@localhost _data]# docekr ps
-bash: docekr: command not found
[root@localhost _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a4e5e692732 nginx "nginx -g ‘daemon of…" 19 seconds ago Up 18 seconds 80/tcp nginx-volumes2
36195574d6f4 nginx "nginx -g ‘daemon of…" 15 minutes ago Up 15 minutes 80/tcp nginx-volume
27161fa1179a httpd "httpd-foreground" 26 minutes ago Up 26 minutes 172.20.66.113:8888->80/tcp vigilant_spence
[root@localhost _data]# sh /home/docker_pid.sh 5a4e5e692732
mesg: ttyname failed: No such file or directory
root@5a4e5e692732:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 50G 8.4G 42G 17% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 8.4G 42G 17% /data
shm 64M 0 64M 0% /dev/shm
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /proc/scsi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
root@5a4e5e692732:/# cd /data/
root@5a4e5e692732:/data# ls
root@5a4e5e692732:/data# touch lqb.log
## 2、在容器裏進行查看
root@5a4e5e692732:/data# cat yz.txt
11111111
2222222222
root@5a4e5e692732:/data# ls
lqb.log yz.txt
## 1、在宿主機上創建文件
[root@localhost centos]# vim /data/volumnes/yz.txt
11111111
2222222222
3、掛載數據卷容器(一個容器可以掛載另一個容器所使用的卷共享存儲)
[root@localhost centos]# docker run -it --name volumes3 --volumes-from nginx-volume centos /bin/bash
[root@0cee278bb97a /]# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 50G 8.4G 42G 17% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 8.4G 42G 17% /data
shm 64M 0 64M 0% /dev/shm
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /proc/scsi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
[root@0cee278bb97a /]# cd /data/
[root@0cee278bb97a data]# ls
1.log 2.log test
[root@0cee278bb97a data]# cat 1.log
11112
Docker入門(二)基本操作