【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用
环境
docker-ce-19.03.1-3.el7.x86_64
CentOS 7
一、查找、拉取镜像、启动容器
1、查找镜像-docker search
默认查找Docker Hub上的镜像,举例:Docker安装nginx
[root@node105 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS…
bitnami/nginx Bitnami nginx Docker Image [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho…
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… [OK]
crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s…
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. [OK]
centos/nginx--centos7 Platform for running nginx 1.8 or building n…
centos/nginx--centos7 Platform for running nginx 1.12 or building …
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… [OK]
1science/nginx Nginx Docker images that include Consul Temp… [OK]
mailu/nginx Mailu nginx frontend [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… [OK]
travix/nginx NGinx reverse proxy [OK]
centos/nginx--centos7 Platform for running nginx 1.10 or building …
wodby/nginx Generic nginx [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX [OK]
[root@node105 ~]#
2、拉取镜像-docker pull
[root@node105 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete
a17e64cfe253: Pull complete
e1288088c7a8: Pull complete
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@node105 ~]#
3、列出本地镜像-docker images
下载完成后,在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像
[root@node105 ~]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b days ago 126MB
[root@node105 ~]#
4、根据镜像启动一个Nginx容器实例-docker run
[root@node105 ~]# docker run --name nginx-test -d -p 8081:80 nginx
db8b3e2f1c647bc2589f04c3984374625455434449ee01e37f0b21163362b052
执行成功返回一行字符串,表示容器ID
--name 为容器设置一个名字
-d 后台运行
-p 端口进行映射,将本地8081端口映射到容器内部的80端口(:左侧是本地端口 右侧是容器内端口)
-P 容器内部端口随机映射到主机的高端口
另外:docker create创建容器但不启动
eg:
按镜像java创建容器myjava3
$ docker create --name myjava3 -it java /bin/bash
启动myjava3
$ docker start myjava3
以交互方式连接myjava3容器 然后执行/bin/bash
$ docker exec -it 容器ID /bin/bash
5、查看容器运行情况-docker ps
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up minutes 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#
6、查看网络端口-docker port
[root@node105 ~]# docker port nginx-test-web
/tcp -> 0.0.0.0:
[root@node105 ~]#
6、查看网络端口-docker port
[root@node105 ~]# docker port nginx-test-web
/tcp -> 0.0.0.0:
[root@node105 ~]#
7、查看应用程序容器的进程-docker top
[root@node105 www]# docker top nginx-test-web
UID PID PPID C STIME TTY TIME CMD
root : ? :: nginx: master process nginx -g daemon off;
: ? :: nginx: worker process
8、查看应用程序日志-docker logs
[root@node105 www]# docker logs -f nginx-test-web
9、检查应用程序-docker inspect 返回一个 JSON 文件记录着 Docker 容器的配置和状态信息
[root@node105 ~]# docker inspect nginx-test-web
[
{
"Id": "398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140",
"Created": "2019-08-25T02:20:55.894180986Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": ,
"ExitCode": ,
"Error": "",
"StartedAt": "2019-08-25T02:20:56.331564845Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5a3221f0137beb960c34b9cf4455424b6210160fd618c5e79401a07d6e5a2ced",
"ResolvConfPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/hostname",
"HostsPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/hosts",
"LogPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140-json.log",
"Name": "/nginx-test-web",
"RestartCount": ,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/root/nginx/www:/usr/share/nginx/html",
"/root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf",
"/root/nginx/logs:/var/log/nginx"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": ""
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount":
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": ,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": ,
"Runtime": "runc",
"ConsoleSize": [
, ],
"Isolation": "",
"CpuShares": ,
"Memory": ,
"NanoCpus": ,
"CgroupParent": "",
"BlkioWeight": ,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": ,
"CpuQuota": ,
"CpuRealtimePeriod": ,
"CpuRealtimeRuntime": ,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": ,
"KernelMemoryTCP": ,
"MemoryReservation": ,
"MemorySwap": ,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": ,
"CpuPercent": ,
"IOMaximumIOps": ,
"IOMaximumBandwidth": ,
"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/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197-init/diff:/var/lib/docker/overlay2/c4db9bb89c2305db1a0e7cf606393ae448936abe940f6718c49b676a39bfd960/diff:/var/lib/docker/overlay2/379e82acb36eb864e2ec8dc5df0bbce1faaac0d124b4d0f573474465713898d5/diff:/var/lib/docker/overlay2/3689b1b87fe3ce1d82df9b91396f9b6f40db29310589016185d9c1f9b118a082/diff",
"MergedDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/merged",
"UpperDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/diff",
"WorkDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/root/nginx/www",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/root/nginx/conf/nginx.conf",
"Destination": "/etc/nginx/nginx.conf",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/root/nginx/logs",
"Destination": "/var/log/nginx",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "398ec11f39b4",
"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.3",
"NJS_VERSION=0.3.5",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b0edc9c3923fb5aa35c6809212b5d4ac5732344bbf05b94aa45dc9b10312f35d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": ,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": ""
}
]
},
"SandboxKey": "/var/run/docker/netns/b0edc9c3923f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"IPAddress": "172.17.0.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "888aa5a2f0c6e980f85c259b7833c883f358a0a0db907e1d2f4459cffee7d551",
"EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
10、关闭容器-docker stop
[root@node105 ~]# docker stop nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node105 ~]#
11、启动容器-docker start
[root@node105 ~]# docker start nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up seconds 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#
12、重启容器
[root@node105 ~]# docker restart nginx-test
nginx-test
[root@node105 ~]# docker kill -s HUP nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up seconds 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#
-s HUP 向容器发送HUP信号,表示重新加载
13、移除应用容器,容器必须是关闭的
[root@node105 ~]# docker rm nginx-test-web
nginx-test-web
二、容器使用
举例:nginx部署
1、将容器内文件拷贝出来
[root@node105 nginx]# docker cp db8b3e2f1c64:/etc/nginx/nginx.conf ~/nginx/conf
2、在本机新建部署nginx目录
[root@node105 ~]# mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
[root@node105 ~]# ll
total
-rw-------. root root Aug : anaconda-ks.cfg
drwxr-xr-x. root root Aug : nginx
www: 目录将映射为nginx容器配置的虚拟目录。
logs: 目录将映射为nginx容器的日志目录。
conf: 目录里的配置文件将映射为nginx容器的配置文件。
3、部署命令
[root@node105 ~]# docker run -d -p 8082:80 --name nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
398ec11f39b4 nginx "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp nginx-test-web
[root@node105 ~]#
-v 将冒号左侧本机目录挂载到冒号右侧容器的目录,创建映射。
4、测试
进入 ~/nginx/www 目录,创建 index.html 文件,内容如下:
[root@node105 ~]# cd ./nginx/www
[root@node105 www]# vi index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>docker nginx部署测试</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>
参考:
CentOS Docker 安装
https://www.runoob.com/docker/centos-docker-install.html
Windows Docker 安装
https://www.runoob.com/docker/windows-docker-install.html
Docker 命令大全
https://www.runoob.com/docker/docker-command-manual.html
Docker 资源汇总
https://www.runoob.com/docker/docker-resources.html
【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用的更多相关文章
- Docker学习笔记二:Docker常用命令及提升拉取镜像的速度
一.Docker命令: 1.docker images //命令用来查看docker中所包含的镜像信息 2.docker ps -a //命令用来查看docker中所包含所有容器信息(运行状 ...
- 使用 docker 拉取镜像和创建容器-nginx
本文主要分享通过从docker hub上获取nginx镜像 1.首先查找nginx镜像 [root@node1 ~]# docker search nginx NAME DESCRIPTION ST ...
- Docker学习之Docker Registry
前言 前面已经学习了怎样通过Dockerfile来构建我们自己的镜像了,但是一个系统可能包含着很多个微服务即有很多个镜像,当镜像越来越多的时候,就必须得有一个地方来管理这些镜像,Docker官方提供了 ...
- 配置 Containerd 在 harbor 私有仓库拉取镜像
官方文档地址:https://github.com/containerd/cri/blob/master/docs/registry.md 严格来说,这个具体可分为两部分 1.在k8s中使用Conta ...
- 【docker】【mysql】docker安装mysql,阿里云docker镜像加速器,docker搜索查看远程仓库上的镜像,docker拉取镜像,查看本地所有镜像,查看容器的运行状况,查看容器的详细信息
在docker上安装mysql有两种方式 1.通过Dockerfile构建 2.直接在docker hub上拉取镜像安装 =================本篇采用方法2=============== ...
- 怎么解决docker pull拉取镜像速度过慢的问题
在我们安装了docker之后,在利用docker pull下载镜像的时候,由于国内的源会出现的问题就是速度真的很慢,可以用龟速来形容,最痛苦的是当你耐心的等待几个小时之后,出现unexpected E ...
- docker拉取镜像报错:net/http: TLS handshake timeout.
docker拉取镜像报错:net/http: TLS handshake timeout. 启动一个后台的busybox容器 [yunva@node1 network-scripts]$ docker ...
- Docker拉取镜像时错误解决办法
拉取docker中的镜像时报错: [root@master ~]# docker pull docker.io/centos:latest Trying to pull repository dock ...
- Docker拉取镜像加速
关于Docker拉取镜像加速 打开桌面 docker 小图标 选中框框 根据下图 添加国内的加速源即可 Docker加速源 #网易 http://hub-mirror.c.163.com #Docke ...
随机推荐
- 20180530模拟赛T2——绀碧之棺
题目背景 qiancl 得到了一张藏宝图,上面写了一道谜题. 题目描述 定义\(F(n)\)为 n 在十进制下各个数位的平方和,求区间\([a,b]\)中有多少\(n\)满足\(k\times F(n ...
- 20180520模拟赛T3——chess
[问题描述] 小美很喜欢下象棋. 而且她特别喜欢象棋中的马. 她觉得马的跳跃方式很独特.(以日字格的方式跳跃) 小芳给了小美一张很大的棋盘,这个棋盘是一个无穷的笛卡尔坐标. 一开始\(time=0\) ...
- 20、Python常用模块sys、random、hashlib、logging
一.sys运行时环境模块 sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. 用法: sys.argv:命令行参数List,第一个元素是程序本身 ...
- 磁盘提示“X:拒绝访问”问题解决
cacls "D:\*.*" /T /E /G Administrators:F cacls "D:\*.*" /T /E /G Users:F cacls & ...
- 使用VSCode开发Flutter
前言 为什么使用VSCode? flutter官方推荐的编辑器有IDEA/Android Studio和VSCode, 之前开发Flutter用的IDEA, 不过IDEA始终比较重,于是换用VSCod ...
- matlab 只安装部分产品
- [Cqoi2016]K远点对 K-Dtree
4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...
- 【CSP膜你赛】柠檬的密码(manacher 二分 单调性 st表)
题目描述 Lemon觉得他需要一个复杂的密码来保证他的帐号的安全.他经过多日思考,决定使用一个长度为奇数的回文串来作为他的密码. 但是这个回文串太长了,Lemon记不住,于是Lemon决定把它记在本 ...
- .htaccess tricks总结
目录 .htaccess tricks总结 一.什么是.htaccess 二.利用条件 三.利用方式 && tricks 1.将指定后缀名的文件当做php解析 2.php_value利 ...
- Gamma阶段第三次scrum meeting
每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...