其他分享
首页 > 其他分享> > 一看就会的Docker基本操作

一看就会的Docker基本操作

作者:互联网

目录

查看docker版本

[root@localhost docker]# docker version 
Client: Docker Engine - Community
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:03:45 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:02:21 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

一、镜像篇

1.1 搜索镜像

#docker search 镜像
[root@localhost docker]# docker search nginx

1.2 下载镜像

#docker pull 镜像
[root@localhost docker]# docker pull nginx

1.3 查看镜像

[root@localhost docker]# docker images
REPOSITORY#实例          TAG#标签                 IMAGE ID#镜像ID            CREATED             SIZE
nginx               latest              bc9a0695f571        16 hours ago        133MB

1.4 镜像导入和导出

#导出:docker save -o 保存位置 镜像名称
[root@localhost ~]# docker save -o /opt/nginx nginx:latest
[root@localhost ~]# ls /opt/
containerd  nginx  rh
#导入:docker locad < 文件位置
[root@localhost ~]# docker load < /opt/nginx
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        37 hours ago        133MB

1.5 修改镜像标签

#docker tag nginx:latest 仓库名:镜像名
[root@localhost ~]# docker tag nginx:latest nginx:web
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        37 hours ago        133MB
nginx               web                 bc9a0695f571        37 hours ago        133MB

1.6 删除镜像

#docker rmi 镜像名称
[root@localhost docker]# docker rmi nginx:latest 
[root@localhost docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

二、容器篇

2.1 创建容器

#docker create -it nginx /bin/bash
[root@localhost ~]# docker create -it nginx /bin/bash

2.2 查看容器

[root@localhost ~]# docker ps -a

2.3 运行容器

#docker start 容器ID
[root@localhost ~]# docker start 3f45a5de7329
#一条龙运行如下
[root@localhost ~]# docker run -itd nginx /bin/bash

2.4 进入容器

#docker exec -it 容器ID bash
[root@localhost ~]# docker exec -it 8f3366c3e223 bash
root@8f3366c3e223:/# 

2.5 停止容器

#docker stop 容器ID
[root@localhost ~]# docker stop 8f3366c3e223

2.6 删除容器

#docker rm 容器ID
[root@localhost ~]# docker rm 8f3366c3e223

2.7 容器导入和导出

#导出:docker export 容器ID > 文件位置
[root@localhost ~]# docker export 318b52c90953 > /opt/nginx_c
[root@localhost ~]# ls /opt/
containerd  nginx  nginx_c  rh
#导入:cat 文件位置 | docker import - 镜像名称
#会生成镜像,而不会创建容器
[root@localhost ~]# cd /opt/
[root@localhost opt]# cat nginx_c | docker import - nginx:web
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost opt]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 4466c31a3cdb        55 seconds ago      134MB

2.8 批量删除容器

[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
0d7e6040ef42        centos:7            "/bin/bash"         2 seconds ago        Up 1 second                             reverent_poitras
5963507a0178        centos:7            "/bin/bash"         45 seconds ago       Up 44 seconds                           boring_mendel
85bdbb157bb2        nginx:web           "/bin/bash"         About a minute ago   Up About a minute                       sad_dhawan
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
Error response from daemon: You cannot remove a running container 0d7e6040ef4280393e7858306d4656a632190633f6d55565084cd7c2d48e7f56. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 5963507a01785d12650ef5c1036dea343706d2b35902fa967e7b07870a0af664. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 85bdbb157bb2ae7effc7027d84073aa10abef8d103837e66b306e1806f39c8a0. Stop the container before attempting removal or force remove
#这时候我们会发现报错了,提示我们无法删除正在运行的容器,我们应该先停止容器才可以删除容器
[root@localhost opt]# docker stop 0d7e6040ef42
0d7e6040ef42
[root@localhost opt]# docker stop 5963507a0178
5963507a0178
[root@localhost opt]# docker stop 85bdbb157bb2
85bdbb157bb2
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
0d7e6040ef42
5963507a0178
85bdbb157bb2
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

标签:容器,一看,nginx,镜像,基本操作,Docker,root,docker,localhost
来源: https://blog.csdn.net/m0_48760948/article/details/110152440