目录
前言
一、Docker镜像操作命令
1、镜像搜索:docker search
2、镜像获取:docker pull
3、镜像信息查看:docker images
4、镜像标签操作:docker tag
5、删除镜像:docker rmi
6、存出镜像:docker save
7、载入镜像:docker load
8 、查看信息
9、查看docker 版本
二、Docker容器操作命令
1、概述
2、容器的创建
3、容器的启动
4、容器运行与终止
5、容器的进入
6、容器的导出和导入
7、容器的删除
总结
上篇文章介绍了有关Docker的基础与Linux下docker的安装,本文主要讲解安装docker后的基础使用方法以及命令的介绍,主要是docker镜像操作及容器操作命令。
命令描述:从Docker Hub中搜索并且显示你所需要的镜像
命令格式:docker search [options] term
[root@docker ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 12968 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1772 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 764 [OK]
...//省略部分内容
[root@docker ~]# docker search -s 100 nginx
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 12968 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1772 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 764 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 104
简要说明:
命令描述:从镜像仓库中拉取(形象的说法嘛)或者更新指定镜像
命令格式:docker pull [OPTIONS] NAME[:TAG|@DIGEST]
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:lates
命令描述:列出本地镜像
命令格式:docker images [OPTIONS] [REPOSITORY[:TAG]]
OPTIONS说明:
-a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);
--digests :显示镜像的摘要信息;
-f :显示满足条件的镜像;
--format :指定返回值的模板文件;
--no-trunc :显示完整的镜像信息;
-q :只显示镜像ID。
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 6 days ago 127MB
#等价于下面的命令
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 6 days ago 127MB
简要说明:
命令描述:标记本地镜像,将其归入某一仓库
命令格式:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
[root@docker ~]# docker tag nginx:latest nginx:lokott
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 6 days ago 127MB
nginx lokott ed21b7a8aee9 6 days ago 127MB
命令描述:删除本地一个或多个镜像
命令格式:docker rmi [OPTIONS] IMAGE [IMAGE…]
[root@docker ~]# docker rmi nginx:lokott
Untagged: nginx:lokott
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 6 days ago 127MB
[root@docker ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Deleted: sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291
Deleted: sha256:8a305f371a6c3c445a1dfc500c1364743868a269ab8cdaf95902692e82168352
Deleted: sha256:d079ef06ec1f10a8050887365f9a940b39547ba6bcc46b16a463e740984f3223
Deleted: sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45
命令描述:将指定镜像保存成 tar 归档文件
命令格式:docker save [OPTIONS] IMAGE [IMAGE…]
[root@docker ~]# docker save -o /opt/nginx nginx:latest
[root@docker ~]# ll /opt/
总用量 127560
drwx--x--x. 4 root root 28 4月 6 09:45 containerd
-rw-------. 1 root root 130618880 4月 6 14:04 nginx
drwxr-xr-x. 2 root root 6 10月 31 2018 rh
命令描述:载入镜像文件
命令格式:docker load [OPTIONS]
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#一:
[root@docker ~]# docker load < /opt/nginx
c3a984abe8a8: Loading layer 72.48MB/72.48MB
99134ec7f247: Loading layer 58.11MB/58.11MB
d37eecb5b769: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 6 days ago 127MB
#二:从tar存档文件中读取,而不是从STDIN中读取
[root@docker ~]# docker load --input /opt/nginx #或者使用-i代替--input也是一样的
c3a984abe8a8: Loading layer 72.48MB/72.48MB
99134ec7f247: Loading layer 58.11MB/58.11MB
d37eecb5b769: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@sys ~]# docker info
Containers: 8
Images: 18
Storage Driver: devicemapper
Pool Name: docker-8:3-918403-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: extfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 804.9 MB
Data Space Total: 107.4 GB
Data Space Available: 18.16 GB
Metadata Space Used: 1.716 MB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.146 GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.117-RHEL6 (2016-04-01)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 2.6.32-642.el6.x86_64
Operating System:
CPUs: 1
Total Memory: 980.6 MiB
Name: sys.kanfanews.com
ID: HFOV:3GDL:KISC:YJPU:PONC:7JPU:NNTV:M675:G25U:KAQZ:U2CP:CEWO
You have new mail in /var/spool/mail/root
[root@sys ~]# docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64
首先,容器简单而言,是基于镜像的一个运行实例,是独立运行的一个或一组应用以及其所必需的运行环境,包括文件系统、系统类库、shell环境等。镜像是只读模板,而容器会给这个只读模板一个额外的可写层。
容器创建命令:docker create
命令格式:docker create [OPTIONS] IMAGE [COMMAND] [ARG…]
[root@docker ~]# docker create -it nginx:latest /bin/bash
a1a2e8468189b146b0215971cf76947987d8fc3f42c58e77371e8d48afb030d1
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1a2e8468189 nginx:latest "/bin/bash" 7 seconds ago Created gallant_feynman
相关说明:
CONTAINER ID:容器ID号
IMAGE:使用的镜像
COMMAND :
CREATED:显示操作时间
STATUS:显示状态,created表示已创建
PORTS:端口 信息和使用的连接类型(tcp/udp)
NAMES:自动分配容器的名称
命令:docker start
命令格式:docker start [OPTIONS] CONTAINER [CONTAINER…]
[root@docker ~]# docker start a1a2e8468189
a1a2e8468189
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1a2e8468189 nginx:latest "/bin/bash" 10 minutes ago Up 2 seconds 80/tcp gallant_feynman
命令格式:docker stop [OPTIONS] CONTAINER [CONTAINER…]
[root@docker ~]# docker stop a1a2e8468189
a1a2e8468189
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4835176a7d8e centos "/usr/bin/bash -c ls…" 9 minutes ago Exited (0) 9 minutes ago affectionate_keldysh
a1a2e8468189 nginx:latest "/bin/bash" 36 minutes ago Exited (0) 3 seconds ago gallant_feynman
命令格式:docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
[root@docker ~]# docker ps -a #检查容器是否是运行状态(up)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4835176a7d8e centos "/usr/bin/bash -c ls…" 12 minutes ago Exited (0) 12 minutes ago affectionate_keldysh
a1a2e8468189 nginx:latest "/bin/bash" 38 minutes ago Up 4 seconds 80/tcp gallant_feynman
[root@docker ~]# docker exec -it a1a2e8468189 /bin/bash #进入伪终端
root@a1a2e8468189:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@a1a2e8468189:/# exit #使用exit退出伪终端
exit
容器导出命令:docker export
命令格式:docker export [OPTIONS] CONTAINER
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4835176a7d8e centos "/usr/bin/bash -c ls…" 21 minutes ago Exited (0) 21 minutes ago affectionate_keldysh
a1a2e8468189 nginx:latest "/bin/bash" 48 minutes ago Up 9 minutes 80/tcp gallant_feynman
运行中的容器导出:
[root@docker ~]# docker export a1a2e8468189 > nginx_contain
[root@docker ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx nginx_contain 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@docker ~]# ll
总用量 253448
-rw-------. 1 root root 1954 4月 6 08:49 anaconda-ks.cfg
-rw-r--r--. 1 root root 2002 4月 6 08:50 initial-setup-ks.cfg
-rw-------. 1 root root 130618880 4月 6 14:04 nginx #镜像存出的文件
-rw-r--r--. 1 root root 128899072 4月 6 16:04 nginx_contain #容器导出文件 二者权限不同
终止的容器导出:
[root@docker ~]# docker export 4835176a7d8e > centos_contain
[root@docker ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx_contain 模板 图片 下载 桌面
centos_contain nginx 公共 视频 文档 音乐
容器导入命令:docker import
命令格式:docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
[root@docker ~]# docker import centos_contain centos_new:54
sha256:f32b5354c81de72a71af50468d09803aa9e62e08741e3cd3e84c10c9a74e2204
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_new 54 f32b5354c81d 10 seconds ago 0B
nginx latest ed21b7a8aee9 6 days ago 127MB
centos latest 470671670cac 2 months ago 237MB
容器的删除:docker rm
命令格式:docker rm [OPTIONS] CONTAINER [CONTAINER…]
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4835176a7d8e centos "/usr/bin/bash -c ls…" 31 minutes ago Exited (0) 31 minutes ago affectionate_keldysh
a1a2e8468189 nginx:latest "/bin/bash" 58 minutes ago Up 19 minutes 80/tcp gallant_feynman
删除已终止的容器:
[root@docker ~]# docker rm 4835176a7d8e
4835176a7d8e
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1a2e8468189 nginx:latest "/bin/bash" 59 minutes ago Up 20 minutes 80/tcp gallant_feynman
删除还在运行中的容器:
[root@docker ~]# docker rm a1a2e8468189 -f
a1a2e8468189
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
本文讲述的是docker基础命令(镜像和容器)的操作,通过实实在在的实际案例方便理解。