Docker常用命令,看这篇文章就够了

前言

下面我就把Docker的常用命令都介绍完,方便大家学习和查阅


Docker version

查看docker的版本信息

实例:

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.10
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        9424aeaee9
 Built:             Thu May 28 22:18:06 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.10
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       9424aeaee9
  Built:            Thu May 28 22:16:43 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Docker login/logout

docker login : 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub

docker logout:登出一个Docker镜像仓库,默认为官方仓库Docker Hub

语法

docker login [OPTIONS] [SERVER]
docker logout [OPTIONS] [SERVER]

OPTIONS说明:

-u :登陆的用户名

-p :登陆的密码

SERVER说明:
仓库的地址,不填默认为 Docker Hub

实例:

#登录
docker login  -u 用户名 -p 密码  https://registry.hub.docker.com/
#登出
docker logout -u 用户名 -p 密码  https://registry.hub.docker.com/

Docker search

从Docker仓库查找镜像

语法:

docker search [OPTIONS] TERM

OPTIONS说明:

--automated :只列出 automated build类型的镜像;

--no-trunc :显示完整的镜像描述;

-f <过滤条件>:列出收藏数不小于指定值的镜像,需要使用=,不可以使用>、<

注意:docker search只支持一个OPTIONS参数

实例

[root@localhost ~]# docker search  -f stars=100 nginx
NAME                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                      Official build of Nginx.                        14547               [OK]                
jwilder/nginx-proxy        Automated Nginx reverse proxy for docker con…   1982                                    [OK]
richarvey/nginx-php-fpm    Container running Nginx + PHP-FPM capable of…   809                                     [OK]
jc21/nginx-proxy-manager   Docker container for managing Nginx proxy ho…   158                                     
linuxserver/nginx          An Nginx container, brought to you by LinuxS…   142                                     
tiangolo/nginx-rtmp        Docker image with Nginx using the nginx-rtmp…   115                                     [OK]

注:这里的 -f stars=100 就表示镜像的stars数需要大于等于100才会显示

返回参数说明:

NAME: 镜像仓库源的名称

DESCRIPTION: 镜像的描述

OFFICIAL: 是否 docker 官方发布

stars: 类似 Github 里面的 star,表示点赞、喜欢的意思

AUTOMATED: 是否是自动构建

Docker pull

从镜像仓库中拉取或者更新指定镜像

语法

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

OPTIONS说明:

-a :拉取所有 tagged 镜像

--disable-content-trust :忽略镜像的校验,默认开启

TAG:版本标志

实例:
#当我们没明确版本的时候,默认是最新版本,最新版本标识为latest

docker pull mysql 

等同于

docker pull mysql:latest

当我们需要更新某个标签的时候,也是使用docker pull,以下命令为更新latest标签的mysql镜像

docker pull mysql:latest

Docker push

将本地的镜像上传到镜像仓库,要先登陆到镜像仓库

实例:

docker push myimg:v1

Docker images

列出本地所有镜像

实例

#查看本地所有镜像
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              f877e80bb9ef        7 days ago          105MB
nginx               latest              9beeba249f3e        9 months ago        127MB
hello-world         latest              bf756fb1ae65        14 months ago       13.3kB
#查看本地nginx 的镜像
[root@localhost ~]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        9 months ago        127MB

Docker tag

给镜像打标签,用于标识某个版本

比如我现在有一个 nginx 镜像,我对其进行开发,开发了第一个版本,我就可以对这个版本打标签,打完标签后会生成新的镜像

例:

[root@localhost ~]# docker tag nginx:latest my/nginx:v1
#执行了上面的命令,就会生成一个my/nginx:v1的镜像,它相当于此时nginx的副本
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              9beeba249f3e        9 months ago        127MB
my/nginx            v1                  9beeba249f3e        9 months ago        127MB

Docker rmi

删除本地一个或多个镜像

语法

docker rmi [OPTIONS] IMAGE [IMAGE...]

OPTIONS说明:

-f :强制删除;

--no-prune :不移除该镜像的过程镜像,默认移除;

删除镜像可以使用他的镜像ID(IMAGE ID)或者名称(REPOSITORY )来指定

实例:

#使用镜像ID删除镜像
[root@localhost ~]# docker rmi -f  9beeba249f3e
Untagged: nginx:latest
Untagged: nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
Untagged: my/nginx:v1
Deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9


#使用镜像名称删除镜像
[root@localhost ~]# docker rmi -f  hello-world 
Untagged: hello-world:latest
Untagged: hello-world@sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b

Docker save

把镜像保存到tar文件中

语法

docker save [OPTIONS] IMAGE [IMAGE...]

OPTIONS 说明:

-o :输出到的文件。

实例:

[root@localhost ~]# docker save  -o hello.tar hello-world
#执行了上述命令,就会在当前目录下生成一个tar的镜像文件
[root@localhost ~]# ll
总用量 28
-rw-r--r--. 1 root root     0 3月  10 16:01 100
-rw-------. 1 root root  1198 5月  30 2020 anaconda-ks.cfg
-rw-------. 1 root root 24576 3月  10 20:25 hello.tar

Docker load

导入使用 docker save 命令导出的镜像

语法

docker load [OPTIONS]

OPTIONS 说明:

--input , -i : 指定导入的文件,代替 STDIN。

--quiet , -q : 精简输出信息。

实例:

#docker load < hello.tar 等同于 docker load --input  hello.tar
[root@localhost ~]# docker load < hello.tar 
Loaded image: hello-world:latest

Docker ps

列出所有在运行的容器信息

Docker ps -a:列出所有容器,包括未运行的

实例:

[root@localhost docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                       CREATED             STATUS                   PORTS                                  NAMES
9ba057b55387        redis               "docker-entrypoint.s…"   6 seconds ago       Up 5 seconds        0.0.0.0:6379->6379/tcp   redis-test

输出参数:

CONTAINER ID: 容器 ID

IMAGE: 使用的镜像

COMMAND: 启动容器时运行的命令

CREATED: 容器的创建时间

STATUS: 容器状态
状态有7种:
created(已创建)
restarting(重启中)
running(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)

PORTS: 容器的端口信息和使用的连接类型(tcp\udp)

NAMES: 容器的名称

Docker start

启动一个或多个已经被停止的容器,也就是说,如果使用这个命令,我们必须先要知道这个容器的ID、或者这个容器的名字,我们可以使用docker ps命令找到这个容器的信息。

实例:

#myredis为容器的名称,你也可以用容器ID来指定
docker start myredis

Docker run

创建一个新的容器并运行,docker run相当于执行了两步操作:将镜像放入容器中(docker create),然后将容器启动,使之变成运行时容器(docker start)。而docker start的作用是,重新启动已存在的镜像。

在这里插入图片描述

语法

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

常用参数:

-d: 后台运行容器,并返回容器ID;
-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
-P:(大写的P) 随机端口映射,容器内部端口随机映射到主机的端口
-p:(小写的p) 指定端口映射,格式为:主机(宿主)端口:容器端口
--name=xxx: 为容器指定一个名称,xxx为你指定的名称
-m :设置容器使用内存最大值
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

实例:

#后台运行一个端口号为6379的redis容器
[root@localhost ~]# docker run -d  -p 6378:6378 redis
d22bbce4902b05485d08d749bd250061cfebbc43936ac3672cb326cb9d218702

#使用镜像nginx:latest以交互模式启动一个容器,在容器内执行/bin/bash命令
runoob@runoob:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/# 

Docker stop

停止一个运行中的容器,可以使用容器ID和容器名来指定

实例:

[root@localhost ~]# docker stop 08a1d07d7bff
08a1d07d7bff

启动或者停止所有容器可以使用以下命令

docker start $(docker ps -a -q) // start启动所有停止的容器
docker stop $(docker ps -a -q) // stop停止所有容器

Docker restart

重启容器,可以使用容器ID和容器名来指定

Docker kill

杀死一个或多个正在运行的容器,向容器内部的主进程发送 SIGKILL 信号(默认),或使用 --signal 选项指定的信号

语法

docker kill [OPTIONS] CONTAINER [CONTAINER...]

OPTIONS说明:

-s :向容器发送一个信号,默认为kill

实例:

[root@localhost ~]# docker kill myredis
myredis

注:
docker kill myredis 等同于 docker kill -s KILL myredis

Docker create

创建一个新的容器但不启动它

实例

#创建一个名为redis2 的容器
[root@localhost ~]# docker create --name redis2 redis
0fd46f3c845417c46f6c39d4c66fe79000ca07ba6a31f4bd174c67878a44e0a7

Docker rm

删除一个或多个容器

语法

docker rm [OPTIONS] CONTAINER [CONTAINER...]

OPTIONS说明:

-f :通过 SIGKILL 信号强制删除一个运行中的容器

-l :移除容器间的网络连接,而非容器本身

-v :删除与容器关联的卷

Docker exec

在运行的容器中执行命令

常用参数:

-d : 在后台运行

-i :即使没有附加也保持STDIN 打开

-t :分配一个伪终端

输入exit退出交互

实例:打开redis连接并设值

[root@localhost ~]# docker exec -it myredis /bin/bash
root@8c0e7e937c80:/data# redis-cli
127.0.0.1:6379> set key1 1
OK

Docker inspect

获取容器/镜像的元数据,容器/镜像的完整信息

Docker logs

获取容器的日志

常用参数:

-f : 跟踪日志输出

--since :显示某个开始时间的所有日志

-t : 显示时间戳

--tail :仅列出最新N条容器日志

实例:

#跟踪查看myredis的日志
[root@localhost ~]# docker logs -f myredis
1:C 11 Mar 2021 13:08:56.339 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 11 Mar 2021 13:08:56.339 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 11 Mar 2021 13:08:56.339 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 11 Mar 2021 13:08:56.340 * monotonic clock: POSIX clock_gettime
1:M 11 Mar 2021 13:08:56.340 * Running mode=standalone, port=6379.
1:M 11 Mar 2021 13:08:56.340 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 11 Mar 2021 13:08:56.340 # Server initialized
1:M 11 Mar 2021 13:08:56.340 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

Docker commit

将容器打包成镜像,

常用参数:

-a :提交的镜像作者;

-c :使用Dockerfile指令来创建镜像;

-m :提交时的说明文字;

-p :在commit时,将容器暂停。

实例:

#将myredis容器打包成v1版本的newredis镜像
[root@localhost ~]# docker commit -a "m" -m "new redis" myredis newredis:v1 
sha256:3eb14b0b46c3d15647bc8e993fb38ec850a0ffbead67489f20226e313e082eab
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newredis            v1                  3eb14b0b46c3        6 seconds ago       105MB

Docker cp

用于容器与主机之间的数据拷贝

实例:

#将 /temp目录下的test.txt文件拷贝到myredis容器的root目录下
[root@localhost temp]# docker cp /temp/test.txt myredis:/root/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值