(三)Docker的常用命令

自主学习:Docker

笔记编写者:房佳亮

学习视频:Bilibili遇见狂神说

Docker的常用命令

1、帮助命令

docker version    #显示docker的版本信息。
docker info       #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令

帮助文档的地址:https://docs.docker.com/engine/reference/commandline/build/

2、镜像命令

docker images #查看所有本地主机上的镜像 可以使用docker image ls代替

docker search 搜索镜像

docker pull 下载镜像 docker image pull

docker rmi 删除镜像 docker image rm
(1)docker images
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB


可选项Options:
  -a, --all             Show all images (default hides intermediate images) #列出所有镜像
  -q, --quiet           Only show numeric IDs # 只显示镜像的id

# 解释
#REPOSITORY			# 镜像的仓库源
#TAG				# 镜像的标签
#IMAGE ID			# 镜像的id
#CREATED			# 镜像的创建时间
#SIZE				# 镜像的大小

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker images -aq
bf756fb1ae65
(2)docker search 搜索镜像
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker search mysql
NAME           DESCRIPTION       STARS          OFFICIAL            AUTOMATED
mysql          ……                10139               [OK]                
mariadb        ……                3734                [OK]     

# --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
      
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker search mysql --filter=STARS=3000
NAME                DESCRIPTION                                     STARS               OFFICIAL     
mysql               MySQL is a widely used, open-source relation…   9500                [OK]             
mariadb             MariaDB is a community-developed fork of MyS…   3444                [OK]
(2)docker pull 下载镜像
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker pull mysql
Using default tag: latest                # 如果不写tag,默认就是latest
latest: Pulling from library/mysql
bb79b6b2107f: Pull complete 						 # 分层下载,Docker images的核心 联合文件系统
49e22f6fb9f7: Pull complete 
842b1255668c: Pull complete 
9f48d1f43000: Pull complete 
c693f0615bce: Pull complete 
8a621b9dbed2: Pull complete 
0807d32aef13: Pull complete 
a56aca0feb17: Pull complete 
de9d45fd0f07: Pull complete 
1d68a49161cc: Pull complete 
d16d318b774e: Pull complete 
49e112c55976: Pull complete 
Digest: sha256:8c17271df53ee3b843d6e16d46cff13f22c9c04d6982eb15a9a47bd5c9ac7e2d   # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest           # 真实地址

# 这两个等价
docker pull docker.io/library/mysql:latest
docker pull mysql

# 指定版本下载
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
bb79b6b2107f: Already exists        # Already exists 表示以前已经有了不需要重新下载
49e22f6fb9f7: Already exists 
842b1255668c: Already exists 
9f48d1f43000: Already exists 
c693f0615bce: Already exists 
8a621b9dbed2: Already exists 
0807d32aef13: Already exists 
f15d42f48bd9: Pull complete 
098ceecc0c8d: Pull complete 
b6fead9737bc: Pull complete 
351d223d3d76: Pull complete 
Digest: sha256:4d2b34e99c14edb99cdd95ddad4d9aa7ea3f2c4405ff0c3509a29dc40bcb10ef
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 1b12f2e9257b        2 weeks ago         448MB
centos              latest              0d120b6ccaa8        3 months ago        215MB
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB
(3)docker rmi 删除镜像
docker rmi -f 镜像id #删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id 镜像id#删除指定的镜像
docker rmi -f $(docker images -aq) #删除全部的镜像

3、容器命令

说明:我们有了镜像才可以创建容器,Linux,下砸一个CentOS镜像来测试学习

docker pull centos
(1)新建容器并启动
docker run [可选参数] image

# 参数说明
--name="Name"    容器名字。tomcat01、tomcat02,用来区分容器。
-d               后台方式运行
-it              使用交互方式运行,进入容器查看内容

-p               指定容器端口  
		-p ip:主机端口:容器端口
		-p 主机端口:容器端口(常用)  -p  8080:8080
		-p 容器端口
		容器端口
		
-P               随机指定端口

# 测试、启动并进入容器
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker run -it centos /bin/bash
[root@665d5bc12263 /]# ls      # 查看容器内的centos,基础版本,很多命令都是不完善的
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@665d5bc12263 /]# exit    # 从容器中退回到主机
exit
[root@iZ2zeefmdpadf6fxudttltZ ~]# ls         # 由于我没有进入到根目录,所以ls显示空
[root@iZ2zeefmdpadf6fxudttltZ ~]# cd ../
[root@iZ2zeefmdpadf6fxudttltZ /]# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  patch  proc  root  run  sbin  srv  sys  tmp  usr  var  www
[root@iZ2zeefmdpadf6fxudttltZ /]# 


(2)列出所有运行中的容器
# docker ps 命令
     # 列出当前正在运行的容器
-a   # 列出当前正在运行的容器+顺带带出历史运行的
-n=? # 显示最近创建的容器
-q   # 只显示容器的编号

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@iZ2zeefmdpadf6fxudttltZ /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS          ……           
665d5bc12263        centos              "/bin/bash"         6 minutes ago       Exited (0) 3 minutes ago   
9b6018ba72d0        hello-world         "/hello"            20 hours ago        Exited (0) 20 hours ago   


[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps -a -n=1
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS          ……
665d5bc12263        centos              "/bin/bash"         29 hours ago        Exited (0) 28 hours ago   

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps -aq
665d5bc12263
9b6018ba72d0
(3)退出容器
exit      # 直接退出容器
Ctrl+P+Q  #容器不停止退出
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker run -it centos /bin/bash
[root@bbaf93b380da /]# (ctrl+p+q)
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               
bbaf93b380da        centos              "/bin/bash"         16 seconds ago      Up 15 seconds                     
[root@iZ2zeefmdpadf6fxudttltZ ~]# 
(4)删除容器
docker rm 容器id                     # 删除指定的容器、不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)       # 删除所有的容器
docker ps -a -q|xargs docker rm     # 删除所有的容器
(5)启动和停止容器的操作
docker start 容器id                 # 启动容器
docker restart 容器id               # 重启容器
docker stop 容器id									# 停止当前正在运行的容器
docker kill 容器id									# 强制停止当前容器

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker run -it centos /bin/bash
[root@baff0b154819 /]# exit
exit
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS             PORTS               
baff0b154819        centos              "/bin/bash"         13 seconds ago      Exited (0) 8 seconds ago 
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker start baff0b154819
baff0b154819
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
baff0b154819        centos              "/bin/bash"         39 seconds ago      Up 8 seconds           
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker stop baff0b154819
baff0b154819
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
[root@iZ2zeefmdpadf6fxudttltZ ~]# 

4、其他常用命令

(1)后台启动容器
# 命令 docker run -d 镜像名
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker run -d centos
d425572201807fb4cb2da091e2de3b5c56826767a85e0f4fd435d5cd08d8e1c1
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     
d42557220180        centos              "/bin/bash"         7 seconds ago       Exited (0) 7 seconds ago                       

# 问题:docker ps 发现 centos停止了???
# 常见的坑:docker 容器使用后台运行必须要有一个前台进程,docker发现没有应用,就会自动停止。
# 也就是说,容器启动后(例如nginx),发现自己没有提供服务,就会立刻停止,就是没有程序了。
(2)查看日志命令
docker logs ~
Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

docker logs -f -t --tail 10 7c412c900499    #没有日志

# 自己写一段shell脚本
docker run -d centos /bin/sh -c "while true;do echo fangjialiang; sleep 1;done"

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker run -d centos /bin/sh -c "while true;do echo fangjialiang; sleep 1;done"
9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              
9af89c717ff9        centos              "/bin/sh -c 'while t…"   6 seconds ago       Up 6 seconds                            
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker logs -f -t --tail 10 9af89c717ff9
2020-11-11T07:20:17.646834699Z fangjialiang
2020-11-11T07:20:18.649087238Z fangjialiang
2020-11-11T07:20:19.651266494Z fangjialiang
2020-11-11T07:20:20.653533031Z fangjialiang
2020-11-11T07:20:21.655860947Z fangjialiang
2020-11-11T07:20:22.658528811Z fangjialiang
2020-11-11T07:20:23.660688517Z fangjialiang
2020-11-11T07:20:24.662779325Z fangjialiang
2020-11-11T07:20:25.665054142Z fangjialiang
2020-11-11T07:20:26.667287709Z fangjialiang
2020-11-11T07:20:27.669623686Z fangjialiang
2020-11-11T07:20:28.671814425Z fangjialiang
2020-11-11T07:20:29.674567349Z fangjialiang
2020-11-11T07:20:30.676850880Z fangjialiang
2020-11-11T07:20:31.679119536Z fangjialiang
^C
[root@iZ2zeefmdpadf6fxudttltZ ~]# 
(3)查看容器中的进程信息
# top命令
docker top 容器id

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              
9af89c717ff9        centos              "/bin/sh -c 'while t…"   6 minutes ago       Up 6 minutes                            
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker top 9af89c717ff9
UID                 PID                 PPID                C                   STIME               TTY                 
root                27234               27218               0                   15:19               ?  
root                27682               27234               0                   15:25               ?       
(4)查看镜像元数据
docker inspect 容器id

[root@iZ2zeefmdpadf6fxudttltZ ~]# docker inspect --help

Usage:	docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker inspect 9af89c717ff9
[
    {
        "Id": "9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c",
        "Created": "2020-11-11T07:19:37.269768126Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo fangjialiang; sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 27234,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-11-11T07:19:37.550430854Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
        "ResolvConfPath": "/var/lib/docker/containers/9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c/hostname",
        "HostsPath": "/var/lib/docker/containers/9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c/hosts",
        "LogPath": "/var/lib/docker/containers/9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c/9af89c717ff9e459800279804d451f90ccc96b2ff2a4e23d9a65b351db9b332c-json.log",
        "Name": "/tender_raman",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "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/4bcb31a22c824bf86bf1a70aab191f1469904e9ff03dd738c9ee93021c6a25ee-init/diff:/var/lib/docker/overlay2/063c41145c586729778b2f65ac1bae13115380a06fadd5d07bf732aef40dee20/diff",
                "MergedDir": "/var/lib/docker/overlay2/4bcb31a22c824bf86bf1a70aab191f1469904e9ff03dd738c9ee93021c6a25ee/merged",
                "UpperDir": "/var/lib/docker/overlay2/4bcb31a22c824bf86bf1a70aab191f1469904e9ff03dd738c9ee93021c6a25ee/diff",
                "WorkDir": "/var/lib/docker/overlay2/4bcb31a22c824bf86bf1a70aab191f1469904e9ff03dd738c9ee93021c6a25ee/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "9af89c717ff9",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo fangjialiang; sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200809",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "cc6e02a6f37639e46567434ad86a1b5ce1a3de95e8ecfbd3ee1bdaf7e21db34e",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/cc6e02a6f376",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "37642f021c22ed92d68b4c86563fd5b374bf8926cb4b3b41e06c2ac15dad9aec",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "b50b9578b9ed6ca86f53868cd76320866249b53ef993edbf10b718b447df351d",
                    "EndpointID": "37642f021c22ed92d68b4c86563fd5b374bf8926cb4b3b41e06c2ac15dad9aec",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

在这里插入图片描述

(5)进入当前正在运行的容器
# 通常容器都是使用后台方式运行的,需要进入容器修改一些配置

# 方式一
docker exec -it 容器id bashshell

[root@iZ2zeefmdpadf6fxudttltZ home]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              
89cec0603d22        centos              "/bin/sh -c 'while t…"   5 seconds ago       Up 4 seconds   
[root@iZ2zeefmdpadf6fxudttltZ home]# docker exec -it 89cec0603d22 /bin/bash
[root@89cec0603d22 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@89cec0603d22 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 07:55 ?        00:00:00 /bin/sh -c while true;do echo fangjialiang; sleep 1;done
root        58     0  0 07:56 pts/0    00:00:00 /bin/bash
root        85     1  0 07:56 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root        86    58  0 07:56 pts/0    00:00:00 ps -ef

# 方式二
docker attach 容器id
[root@iZ2zeefmdpadf6fxudttltZ home]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              
[root@iZ2zeefmdpadf6fxudttltZ home]# docker attach 89cec0603d22
fangjialiang
fangjialiang
fangjialiang
fangjialiang
fangjialiang
fangjialiang
fangjialiang


# docker exec            # 进入容器后开启一个新的终端
# docker attach          # 进入容器正在执行的终端,不会启动新的进程
(6)从容器中拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径

# 查看当前主机目录
[root@iZ2zeefmdpadf6fxudttltZ home]# ls
admin  redis  www
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 1b12f2e9257b        2 weeks ago         448MB
centos              latest              0d120b6ccaa8        3 months ago        215MB
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker run -it centos /bin/bash
[root@dbd924a75c13 /]# (ctrl+p+q) 
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
dbd924a75c13        centos              "/bin/bash"         53 seconds ago      Up 53 seconds    
[root@iZ2zeefmdpadf6fxudttltZ ~]# cd /home

# 进入容器内部
[root@iZ2zeefmdpadf6fxudttltZ home]# docker attach dbd924a75c13
[root@dbd924a75c13 /]# cd /home
[root@dbd924a75c13 home]# ls

# 在容器中创建一个文件
[root@dbd924a75c13 home]# touch fangjialiang.java
[root@dbd924a75c13 home]# ls
fangjialiang.java
[root@dbd924a75c13 home]# exit
exit          
[root@iZ2zeefmdpadf6fxudttltZ home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       
dbd924a75c13        centos              "/bin/bash"              3 minutes ago       Exited (0) 19 

# 将文件拷贝出来到我们的主机上
[root@iZ2zeefmdpadf6fxudttltZ home]# docker cp dbd924a75c13:/home/fangjialiang.java /home
[root@iZ2zeefmdpadf6fxudttltZ home]# ls
admin  fangjialiang.java  redis  www
[root@iZ2zeefmdpadf6fxudttltZ home]# 

# 拷贝是一个手动过程,未来我们使用一个-v卷的技术,可以实现自动同步……

5、命令小结

在这里插入图片描述

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  # 当前shell下attach连接指定运行镜像
  build       Build an image from a Dockerfile
  # 通过Dockerfile定制镜像
  commit      Create a new image from a container·s changes
  # 提交当前容器为新的镜像
  cp          Copy files/folders between a container and the local filesystem
  # 从容器中拷贝指定的文件或者目录到宿主机中
  create      Create a new container
  # 创建一个新的容器,同run,但是不启动容器
  diff        Inspect changes to files or directories on a container·s filesystem
  # 查看docker容器变化
  events      Get real time events from the server
  # 从docker服务获取容器实时事件
  exec        Run a command in a running container
  # 在已存在的容器上运行命令
  export      Export a container's filesystem as a tar archive
  # 导出容器的内容流作为一个tar归档文件,对应[import]
  history     Show the history of an image
  # 展示一个镜像形成历史
  images      List images
  #列出系统当前镜像
  import      Import the contents from a tarball to create a filesystem image
  # 从tar包中的内容创建一个新的文件锡荣影响,对应[export]
  info        Display system-wide information
  # 显示系统相关信息
  inspect     Return low-level information on Docker objects
  # 查看容器详细信息
  kill        Kill one or more running containers
  # 杀死指定的容器
  load        Load an image from a tar archive or STDIN
  # 从一个tar包中加载一个镜像,对应[save]
  login       Log in to a Docker registry
  # 注册或登录一个docker源服务器
  logout      Log out from a Docker registry
  # 从当前Docker registry退出
  logs        Fetch the logs of a container
  # 输出当前容器日志
  pause       Pause all processes within one or more containers
	# 暂停容器
  port        List port mappings or a specific mapping for the container
  # 查看映射端口对应的容器内部源端口
  ps          List containers
  # 列出容器列表
  pull        Pull an image or a repository from a registry
  # 从docker镜像源服务器拉取指定镜像或者库镜像
  push        Push an image or a repository to a registry
  # 推送指定镜像或者库镜像至docker源服务器
  rename      Rename a container
  # 为容器重新命名
  restart     Restart one or more containers
  # 重启运行的容器
  rm          Remove one or more containers
  # 删除一个或多个容器
  rmi         Remove one or more images
  # 删除一个或多个镜像(无容器使用该镜像事才可删除,否则需删除相关容器才可继续或者使用-f强制删除)
  run         Run a command in a new container
  # 创建一个新的容器并运行一个命令
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  # 保存一个镜像为tar包,对应[load]
  search      Search the Docker Hub for images
  # 在docker hub中搜索镜像
  start       Start one or more stopped containers
  # 启动一个或多个已停止的容器
  stats       Display a live stream of container(s) resource usage statistics
  
  stop        Stop one or more running containers
  # 停止一个或多个正在运行的容器
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  # 给源中的镜像打一个标签
  top         Display the running processes of a container
  # 查看容器中运行的进程信息
  unpause     Unpause all processes within one or more containers
  # 取消暂停容器
  update      Update configuration of one or more containers
  # 
  version     Show the Docker version information
  # 查看docker的版本号
  wait        Block until one or more containers stop, then print their exit codes
  # 截取容器停止时的退出状态值

docker的命令是十分多的,上面我们学习的都是最常用的容器和镜像的命令,之后我们还会学习很多命令。

接下来就是一堆的练习

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值