Docker-基本操作

镜像操作

镜像操作基本命令:

[root@KVM02 docker]# docker image --help

Usage:	docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile   
              #通过Dockerfile文件构建镜像
  history     Show the history of an image       
              #显示镜像的历史信息
  import      Import the contents from a tarball to create a filesystem image
              #从一个保存的tar文件导入一个镜像
  inspect     Display detailed information on one or more images
  			  #查看镜像的详细信息
  load        Load an image from a tar archive or STDIN
  			  #加载一个镜像
  ls          List images
              #镜像列表
  prune       Remove unused images
              #移除没有使用的镜像
  pull        Pull an image or a repository from a registry
              #从仓库中下载一个镜像
  push        Push an image or a repository to a registry
              #把镜像上传到仓库
  rm          Remove one or more images
              #移除一个或多个镜像
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
              #保存镜像
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
              #给镜像打标签
1、 搜索官方仓库镜像
[root@KVM02 docker]# docker search nginx
NAME (镜像名字)             DESCRIPTION (描述信息)                          STARS (星级)    OFFICIAL          AUTOMATED
nginx                       Official build of Nginx.                        13923            [OK]                
jwilder/nginx-proxy         Automated Nginx reverse proxy for docker con…   1900                                    [OK]
richarvey/nginx-php-fpm     Container running Nginx + PHP-FPM capable of…   791                                     [OK]
2、 拉取镜像
[root@KVM02 docker]# docker pull busybox   
#拉取得为最新版
[root@KVM02 docker]# docker pull nginx:1.14-alpine
#拉取指定版本
[root@KVM02 docker]# docker pull Ubuntu
3、查看当前主机镜像列表
[root@KVM02 docker]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              d70eaf7277ea        4 days ago          72.9MB
busybox             latest              f0b02e9d092d        2 weeks ago         1.23MB
4、登录
方法一:
[root@KVM02 docker]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxvv01
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
#登陆成功

方法二:
[root@KVM02 docker]# docker login -u  **  -p **
-u  用户名
-p  密码
5、登出
[root@KVM02 docker]# docker logout 
Removing login credentials for https://index.docker.io/v1/
创建仓库

点击Create Repository按钮
在这里插入图片描述
编译仓库名–选择私有还是公有–点击Create创建
在这里插入图片描述
点击这个按钮查看
在这里插入图片描述

6、上传镜像–前提:打标签
[root@KVM02 docker]# docker tag --help
Usage:	docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
例:
[root@KVM02 docker]# docker tag busybox:latest xxvv01/xx:busybox.v1
#打标签
[root@KVM02 docker]# docker push xxvv01/xx:busybox.v1
#上传镜像
The push refers to repository [docker.io/xxvv01/dockertest/busybox]
The push refers to repository [docker.io/xxvv01/xx]
d2421964bad1: Pushed 
busybox.v1: digest: sha256:c9249fdf56138f0d929e2080ae98ee9cb2946f71498fc1484288e6a935b5e5bc size: 527
7、导出镜像
[root@KVM02 docker]# docker image save --help
格式:	docker image save [OPTIONS] IMAGE [IMAGE...]
  -o,--output string   Write to a file, instead of STDOUT

方法一:重定向
[root@KVM02 ~]# docker image save busybox:latest > busybox1.tar
方法二:-o
[root@KVM02 ~]# docker image save busybox:latest -o busybox2.tar 

注:两者权限不同
[root@KVM02 ~]# ll -h
-rw-r--r--. 1 root root 1.4M 10月 28 22:47 busybox1.tar
-rw-------. 1 root root 1.4M 10月 28 22:48 busybox2.tar
8、删除镜像
方法一:
[root@KVM02 ~]# docker image rm busybox:latest 
Untagged: busybox:latest
Untagged: busybox@sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
方法二:
[root@KVM02 ~]# docker rmi busybox:latest 
9、加载镜像
[root@KVM02 ~]# docker image load -i busybox1.tar 
Loaded image: busybox:latest
10、导入镜像
[root@KVM02 ~]# docker image import --help
格式:	docker image import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

例:
[root@KVM02 ~]# docker image import busybox1.tar (镜像没有名称)
注:删除可以用ID号删除
11、查看镜像的详细信息
[root@KVM02 ~]# docker image inspect ubuntu
镜像的保存对比

docker save与docker export的区别:
1、save/load ; export /import成对使用
2、docker save保存的是镜像,docker export保存的是容器。

[root@KVM02 ~]# docker save --help
Usage:	docker save [OPTIONS] IMAGE [IMAGE...]
Options:
  -o, --output string   Write to a file, instead of STDOUT

[root@KVM02 ~]# docker save  busybox:latest -o busybox.tar

[root@KVM02 ~]# docker import  --help
Usage:	docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

[root@KVM02 ~]# docker export busybox -o busybox2.tar

将busybox,busybox2下载到windows桌面查看其区别:
在这里插入图片描述
busybox只有一层镜像
在这里插入图片描述
busybox2储存的是linux文件系统

镜像的加载对比

load与import的区别
**
1、docker load用来载入镜像包,docker import用来载入容器包,但两者都会恢复镜像。
2、docker load不能对载入的镜像重命名,而docker import 可以为镜像指定新的名称。
**
用load加载镜像

[root@KVM02 ~]# docker load  --help
Usage:	docker load [OPTIONS]
Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

[root@KVM02 ~]# docker image load -i busybox1.tar 
Loaded image: busybox:latest
[root@KVM02 ~]# docker image ls busybox
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              f0b02e9d092d        2 weeks ago         1.23MB

用import加载镜像

[root@KVM02 ~]# docker import --help
Usage:	docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

方法一:直接加载,镜像没有标签
[root@KVM02 ~]# docker image import busybox2.tar 
sha256:5789f0eb51eadb68e873b42e21450cb39fdcdbf9783d03dee0f2859d21c1e532
[root@KVM02 ~]# docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              5789f0eb51ea        9 seconds ago       1.23MB
给镜像打标签
[root@KVM02 ~]# docker tag 5789f0eb51ea busybox:latest 
[root@KVM02 ~]# docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
busybox             latest              5789f0eb51ea        About a minute ago   1.23MB

方法二:加载镜像并且给镜像打标签
[root@KVM02 ~]# cat busybox2.tar | docker import -  busybox:latest
sha256:976accbdf961003b3e49638623d60f4cc0341e79ff1ea91795ecba2908407cd9
[root@KVM02 ~]# docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              976accbdf961        22 seconds ago      1.23MB

容器操作

1、创建容器
[root@KVM02 ~]# docker create busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
9758c28807f2: Pull complete 
Digest: sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
Status: Downloaded newer image for busybox:latest
2、查看容器
方法一:
[root@KVM02 ~]# docker ps 
#查看正在运行中的容器
[root@KVM02 ~]# docker ps -a
#查看全部容器
方法二:
[root@KVM02 ~]# docker container ls
#查看运行中的容器
3、启动容器
方法一:
[root@KVM02 ~]# docker start busybox
方法二:
[root@KVM02 ~]# docker run 
-t :打开一个终端,像使用交换机一样使用容器
-i: 交互式访问
--name:容器名字
--network:指定网络
--rm:容器一停,自动删除
-d:剥离与当前终端的关系;否则会一直占据着终端
-p:端口映射,将容器内服务的端口映射在宿主机的指定端口

例:
[root@KVM02 ~]# docker run -d  nginx:1.14-alpine 

如何让-it退出时,保持容器运行:ctrl+P,Q
[root@KVM02 ~]# docker run -it nginx:1.14-alpine  /bin/sh
/ # [root@KVM02 ~]#

--name
[root@KVM02 ~]# docker run -d --name nginx1 nginx:1.14-alpine
#给容器命名
--rm
[root@KVM02 ~]# docker run --rm -it nginx:1.14-alpine /bin/sh
#退出即删除
4、停止运行中的容器
方法一:
[root@KVM02 ~]# docker stop nginx1
#停止容器
方法二:
[root@KVM02 ~]# docker kill nginx1
-s:指定信号,和kill 用法一样;-9 强制停止容器
5、激活关闭的容器
[root@KVM02 ~]# docker start  nginx1 
-a:附加到当前终端
-i:交互式
6、查看容器的详细信息
[root@KVM02 ~]# docker inspect nginx1
7、删除容器
[root@KVM02 ~]# docker rm nginx1 
#删除容器
[root@KVM02 ~]# docker ps -aq
f421cbd6ae40
#根据ID号查看若有容器

[root@KVM02 ~]# docker rm -f `docker ps -aq`
#删除所有容器,默认只能删未运行的容器
-f 强制删除
8、对运行的容器执行指定的命令
[root@KVM02 ~]# docker exec -it nginx1 /bin/sh
-d:在后台运行命令
-e:设置环境变量
-i:交互式
-t:打开一个终端
-u:用户名或UID
9、查询容器内部日志
[root@KVM02 ~]# docker exec nginx1 ip a
#查看容器的IP地址
[root@KVM02 ~]# curl 172.17.0.3
#访问容器
[root@KVM02 ~]# docker logs nginx1 
172.17.0.1 - - [29/Oct/2020:14:34:32 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
#查看日志

猜你喜欢

转载自blog.csdn.net/m0_46289868/article/details/109345754