Docker常用命令

帮助命令

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

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

镜像命令

docker images

docker images 查看所有本地的主机上的镜像

[root@localhost /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

#	可选项
  -a, --all             # 列出所有镜像
  -q, --quiet           # 只显示id

docker search 搜索镜像

[root@localhost /]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9822                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3586                [OK]                       

#	可选性
  -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
# 通过过滤来显示:显示STARS大于3000的镜像
[root@localhost /]# docker search mysql --filter=STARS=3000
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9822                [OK]                
mariadb             MariaDB is a community-developed fork of MyS…   3586                [OK] 
[root@localhost /]# docker search mysql --filter=STARS=5000
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9822                [OK]     

docker pull

docker pull 下载镜像

# 下载最新版
[root@localhost /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
bf5952930446: Pull complete 	# 分层下载 docker image的核心 
8254623a9871: Pull complete 
938e3e06dac4: Pull complete 
ea28ebf28884: Pull complete 
f3cef38785c2: Pull complete 
894f9792565a: Pull complete 
1d8a57523420: Pull complete 
6c676912929f: Pull complete 
ff39fdb566b4: Downloading [===============================================>   ]  106.6MB/112.5MB
ff39fdb566b4: Pull complete 
fff872988aba: Pull complete 
4d34e365ae68: Pull complete 
7886ee20621e: Pull complete 
Digest: sha256:c358e72e100ab493a0304bda35e6f239db2ec8c9bb836d8a427ac34307d074ed		# 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest		# 真实地址

# docker pull mysql 等价于 docker pull docker.io/library/mysql:latest
docker pull mysql
docker pull docker.io/library/mysql:latest

# 下载指定版本	docker pull mysql [:版本号]
[root@localhost /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
bf5952930446: Already exists 
8254623a9871: Already exists 
938e3e06dac4: Already exists 
ea28ebf28884: Already exists 
f3cef38785c2: Already exists 
894f9792565a: Already exists 
1d8a57523420: Already exists 
5f09bf1d31c1: Pull complete 
1b6ff254abe7: Pull complete 
74310a0bf42d: Pull complete 
d398726627fd: Pull complete 
Digest: sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

[root@localhost /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 718a6da099d8        6 days ago          448MB
mysql               latest              0d64f46acfd1        6 days ago          544MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

docker rmi

docker rmi 删除镜像

[root@localhost /]# docker rmi -f 718a6da099d8
Untagged: mysql:5.7
Untagged: mysql@sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Deleted: sha256:718a6da099d82183c064a964523c0deca80619cb033aadd15854771fe592a480
Deleted: sha256:058d93ef2bfb943ba6a19d8b679c702be96e34337901da9e1a07ad62b772bf3d
Deleted: sha256:7bca77783fcf15499a0386127dd7d5c679328a21b6566c8be861ba424ac13e49
Deleted: sha256:183d05512fa88dfa8c17abb9b6f09a79922d9e9ee001a33ef34d1bc094bf8f9f
Deleted: sha256:165805124136fdee738ed19021a522bb53de75c2ca9b6ca87076f51c27385fd7
[root@localhost /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              0d64f46acfd1        6 days ago          544MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
[root@localhost /]# 

# 删除指定镜像 docker rmi -f 镜像id
[root@localhost /]# docker rmi -f 718a6da099d8
# 删除多个镜像 docker rmi -f 镜像id 镜像id 镜像id 镜像id
[root@localhost /]# docker rmi -f 718a6da099d8 718a6da099d3 718a6da099d23
# 删除全部镜像 docker rmi -f $(docker images -qa)
[root@localhost /]# docker rmi -f $(docker images -qa)

容器命令

说明:有了镜像才可以创建容器,下载一个centos镜像来测试学习

docker pull centos

docker run

新建容器并启动

docker run[可选参数] image

# 参数说明
--name="name"	容器名字	tomcat01 tomcat02 用来区分容器
-d				后台方式运行
-it				使用交互方式运行,进入容器查看内容
-p				指定容器端口 -p 8080:8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
	容器端口
-P				随机指定端口

# 测试
[root@localhost /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        13 hours ago        215MB
mysql               latest              0d64f46acfd1        6 days ago          544MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
# 启动并进入容器	
[root@localhost /]# docker run -it centos /bin/bash
# 查看容器内的centos
[root@35b18e8a7511 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 退出容器---》退出并停止容器,若只退出容器,不停止使用快捷键 Ctrl + Q + P
[root@35b18e8a7511 /]# exit
exit
[root@localhost /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

docker ps

列出所有运行的容器

#docker ps
			# 列出当前正在运行的容器
	-a		# 列出当前正在运行的容器 + 带出历史运行的容器
	-n=?	# 显示最近创建的容器
	-q		#只显示容器的编号
    

# 测试
[root@f2abdaa7f711 /]# [root@localhost /]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS              NAMES
f2abdaa7f711        centos              "/bin/bash"         15 seconds ago      Up 14 seconds                        modest_bell 

[root@localhost /]# docker ps -a
CONTAINER ID      IMAGE           COMMAND          CREATED             STATUS                    PORTS           NAMES
f2abdaa7f711      centos          "/bin/bash"      3 minutes ago       Up 3 minutes                            modest_bell     35b18e8a7511      centos          "/bin/bash"      8 minutes ago       Exited (0) 6 minutes ago                stoic_mendel
fb32d5936b39      hello-world     "/hello"         4 hours ago         Exited (0) 4 hours ago                  hungry_faraday

[root@localhost /]# docker ps -a -n=1
CONTAINER ID      IMAGE            COMMAND           CREATED             STATUS              PORTS               NAMES
f2abdaa7f711      centos           "/bin/bash"       8 minutes ago       Up 8 minutes                            modest_bell

# 列出当前正在运行的容器 + 带出历史运行的容器的 id
[root@localhost /]# docker ps -aq
f2abdaa7f711	# 正在运行的容器
35b18e8a7511	# 历史运行的容器
fb32d5936b39	# 历史运行的容器

# 列出当前正在运行的容器 id
[root@localhost /]# docker ps -q
f2abdaa7f711

docker rm

删除容器

docker rm 容器id					# 删除指定容器---》不会删除正在运行的容器
docker rm $(docker ps -aq)		 # 删除所有容器---》不会删除正在运行的容器
docker rm -f 容器id				# 强制删除指定容器
docker rm -f $(docker ps -aq)	 # 强制删除所有容器

docker ps -a -q|xargs docker rm	 # 通过管道符删除所有的容器

# 测试

# 查询所有容器
[root@localhost /]# docker ps -aq	
f2abdaa7f711	# 正在运行的容器
35b18e8a7511	# 历史运行的容器
fb32d5936b39	# 历史运行的容器
# 删除所有容器 发现删除了历史运行的容器,正在运行的容器并没有删除
[root@localhost /]# docker rm $(docker ps -aq)
35b18e8a7511
fb32d5936b39
Error response from daemon: You cannot remove a running container f2abdaa7f7117a77d2656954ac983d1609d9e026e2eaae0639758ee0490df617. Stop the container before attempting removal or force remove
# 正在运行的容器不能删除,若要删,使用强制删除
[root@localhost /]# docker ps -aq
f2abdaa7f711
# 强制删除正在运行的容器
[root@localhost /]# docker rm -f $(docker ps -aq)
f2abdaa7f711
[root@localhost /]# docker ps -aq
[root@localhost /]# 

启动和停止容器

启动和停止容器

docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id


# 测试

# 没有正在运行的容器
[root@localhost /]# docker ps
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS              PORTS               NAMES
# 有一个历史运行的容器
[root@localhost /]# docker ps -a
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS              PORTS               NAMES
0ea9ccef9929     centos        "/bin/bash"         2 minutes ago       Exited (0) 9 seconds ago              inspiring_payne     
# 启动这个历史运行的容器
[root@localhost /]# docker start 0ea9ccef9929
0ea9ccef9929
# 发现这个历史运行的容器已经运行
[root@localhost /]# docker ps
CONTAINER ID     IMAGE            COMMAND          CREATED              STATUS              PORTS            NAMES
0ea9ccef9929     centos           "/bin/bash"      About a minute ago   Up 5 seconds                         inspiring_payne
# 停止这个容器
[root@localhost /]# docker stop 0ea9ccef9929
0ea9ccef9929
# 发现这个容器已经停止
[root@localhost /]# docker ps
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS              PORTS               NAMES
# 重新启动这个容器
[root@localhost /]# docker restart 0ea9ccef9929
0ea9ccef9929
[root@localhost /]# docker ps
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS              PORTS               NAMES
0ea9ccef9929     centos           "/bin/bash"      8 minutes ago       Up 9 seconds                            inspiring_payne
# 强制停止当前容器
[root@localhost /]# docker kill 0ea9ccef9929
0ea9ccef9929
[root@localhost /]# docker ps
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS              PORTS               NAMES
[root@localhost /]# docker ps -a
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS                     PORTS        NAMES
0ea9ccef9929     centos           "/bin/bash"     8 minutes ago       Exited (137) 10 seconds ago              inspiring_payne   

常用其他命令

开放端口

# 开放4433端口
[root@bogon home]#  firewall-cmd --zone=public --add-port=4433/tcp --permanent
success
# 重启防火墙
[root@bogon home]# firewall-cmd --reload
success
# 查看当前所有tcp端口·
[root@bogon home]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1031/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1201/master         
tcp6       0      0 :::4433                 :::*                    LISTEN      60497/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      1031/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1201/master    
# 查看所有4433端口使用情况·
[root@bogon home]# netstat -ntulp |grep 4433
tcp6       0      0 :::4433                 :::*                    LISTEN      60497/docker-proxy 

查看docker容器内部开放有哪些端口

# 不用进入容器bash内部,在linux命令行内输入docker port  容器id   即可查看

[root@localhost yin]# docker port b21706cf612e
8080/tcp -> 0.0.0.0:8080

后台启动容器的坑

# 命令 docker run -d 镜像名

# 测试

# 通过查看 没有容器
[root@localhost /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# 后台启动,不进入容器
[root@localhost /]# docker run -d centos
d8b368a5b4f89143b8b3655d93a18b15065285ef9b5851a3b1d6a9819a84d3f1
# 启动后发现没有正在运行的容器
[root@localhost /]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# 在历史记录里发现了我们运行的容器
[root@localhost /]# docker ps -a
CONTAINER ID     IMAGE            COMMAND          CREATED             STATUS                  PORTS            NAMES
d8b368a5b4f8     centos           "/bin/bash"      10 seconds ago      Exited (0) 9 seconds ago                 musing_bhabha   

# 常见的坑:docker 容器使用后台运行, 就必须要有一个前台进程(比如 -it 进入容器),docker发现没有引用,就会自动停止
# 容器启动后,发现自己没有提供服务,就会立即停止服务

查看日志命令

dockerlogs -tf --tail number 容器id
	-ft				#显示日志
	--tail number 	#要显示的日志条数
	

# 测试一

# 使用交互方式启动centos
[root@localhost /]# docker run -it centos /bin/bash
# docker ps不生效是因为 我们当前所在的位置是在我们的centos容器中,这个容器不支持这个命令
[root@a49c01697fa5 /]# docker ps      	          
bash: docker: command not found
[root@a49c01697fa5 /]# [root@localhost /]# docker ps
CONTAINER ID     IMAGE           COMMAND           CREATED             STATUS              PORTS               NAMES
a49c01697fa5     centos          "/bin/bash"       44 seconds ago      Up 43 seconds                           blissful_joliot
# 查看日志 
[root@localhost /]# docker logs -t -f --tail 10 a49c01697fa5
2020-08-11T08:30:01.439255660Z [root@a49c01697fa5 /]# docker ps                
2020-08-11T08:30:01.442523237Z bash: docker: command not found

# 测试二

# 自己编写一段shell脚本
[root@localhost /]# docker run -d centos /bin/sh -c "while true;do echo hello,docker;sleep 5;done"
e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55
[root@localhost /]# docker ps
CONTAINER ID      IMAGE             COMMAND                  CREATED             STATUS         PORTS    NAMES
e2b54342c047      centos            "/bin/sh -c 'while t…"   3 seconds ago       Up 2 seconds          beautiful_kowalevski     # 通过日志命令查看日志	
[root@localhost /]# docker logs -ft --tail 10 e2b54342c047
2020-08-11T09:11:51.601900929Z hello,docker
2020-08-11T09:11:56.608352606Z hello,docker


查看容器中进程命令

# 命令 docker top 容器id

# 测试
[root@localhost /]# docker top e2b54342c047
UID       PID     PPID    C    STIME   TTY   TIME       CMD
root     43072   43056    0    17:25   ?     00:00:00   /bin/sh -c while true;do echo hello,docker;sleep 5;done
root     43100   43072    0    17:25   ?     00:00:00   /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5

查看镜像源数据

# 命令
docker inspect 容器id

# 测试
[root@localhost /]# docker ps 
CONTAINER ID     IMAGE      COMMAND               CREATED          STATUS           PORTS            NAMES
e2b54342c047     centos    "/bin/sh -c 'while t…"  16 minutes ago   Up 3 minutes                  beautiful_kowalevski

[root@localhost /]# docker inspect e2b54342c047
[
    {
        "Id": "e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55",		# 容器id
        "Created": "2020-08-11T09:11:51.354568576Z",									# 创建时间
        "Path": "/bin/sh",																# 默认的控制台
        "Args": [																		# 参数
            "-c",
            "while true;do echo hello,docker;sleep 5;done"
        ],
        "State": {																		# 状态
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,	
            "Pid": 43072,																# 进程id
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-08-11T09:25:28.428312287Z",
            "FinishedAt": "2020-08-11T09:12:40.876021118Z"
        },
        "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",		# 所用镜像id
        "ResolvConfPath": "/var/lib/docker/containers/e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55/hostname",
        "HostsPath": "/var/lib/docker/containers/e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55/hosts",
        "LogPath": "/var/lib/docker/containers/e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55/e2b54342c047098fbdb4b1fb8598f0d6adcba9ea9791dfc590acf8b24f8adb55-json.log",
        "Name": "/beautiful_kowalevski",
        "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/0b10811247f0de22b5b639c3a2e7b2c49ae76313f32cee36dff5b7c3f257926c-init/diff:/var/lib/docker/overlay2/3578edcc08a6f5ea63f0f8aa1f906688ade08c594e1db4bd5cc348283dcfcafe/diff",
                "MergedDir": "/var/lib/docker/overlay2/0b10811247f0de22b5b639c3a2e7b2c49ae76313f32cee36dff5b7c3f257926c/merged",
                "UpperDir": "/var/lib/docker/overlay2/0b10811247f0de22b5b639c3a2e7b2c49ae76313f32cee36dff5b7c3f257926c/diff",
                "WorkDir": "/var/lib/docker/overlay2/0b10811247f0de22b5b639c3a2e7b2c49ae76313f32cee36dff5b7c3f257926c/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],									# 挂载
        "Config": {
            "Hostname": "e2b54342c047",
            "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 hello,docker;sleep 5;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": "da0d42e7d6b7403b56c55c2bdea59e2f812cc2a4d8282ac6d6194e868f007b1d",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/da0d42e7d6b7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "febc843ef875befd3694342e88757e08f1baf7841a15bd033fe748be147375ca",
            "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": "0b84c64e381f5ed84287c2b7ffcad61b9c23731cd6d9365466b2131dff1026a6",
                    "EndpointID": "febc843ef875befd3694342e88757e08f1baf7841a15bd033fe748be147375ca",
                    "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
                }
            }
        }
    }
]

进入当前运行的容器

# 我们通常容器都是使用后台方式运行的,需要进入容器 修改配置

# 方式一
# 命令 docker exec -it 容器id bashShell		# 进入容器后,打开一个新的终端,可以在里面操作

# 测试
[root@bogon ~]# docker ps 
CONTAINER ID     IMAGE          COMMAND                  CREATED          STATUS         PORTS         NAMES
e2b54342c047     centos     "/bin/sh -c 'while t…"   16 hours ago     Up 16 hours               beautiful_kowalevski
[root@bogon ~]# docker exec -it e2b54342c047 /bin/bash
[root@e2b54342c047 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 方式二
# 命令 docker attach 容器id					# 进入容器正在执行的终端,不会启动新的进程

# 测试
[root@bogon ~]# docker ps
CONTAINER ID      IMAGE        COMMAND                  CREATED          STATUS       PORTS           NAMES
e2b54342c047      centos     "/bin/sh -c 'while t…"   16 hours ago     Up 16 hours               beautiful_kowalevski
[root@bogon ~]# docker attach e2b54342c047
hello,docker		# 正在执行的代码
hello,docker
hello,docker

从容器中拷贝文件到主机

# 命令
docker cp 容器id:文件在容器中的位置 主机位置

# 测试

# 查看当前运行的容器
[root@bogon ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# 用交互模式启动centos容器
[root@bogon ~]# docker run -it centos /bin/bash
# 容器中的目录
[root@d1504ce8714d /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 进入容器中home目录创建test.java文件
[root@d1504ce8714d /]# cd home/
[root@d1504ce8714d home]# touch test.java
[root@d1504ce8714d home]# ls
test.java
# 退出容器后查看正在运行的容器
[root@d1504ce8714d home]# [root@bogon ~]# docker ps
CONTAINER ID      IMAGE          COMMAND         CREATED             STATUS              PORTS               NAMES
d1504ce8714d      centos        "/bin/bash"    37 seconds ago      Up 36 seconds                           agitated_dijkstra
# 停止当前运行的容器
[root@bogon ~]# docker stop d1504ce8714d
d1504ce8714d
[root@bogon ~]# docker ps
CONTAINER ID      IMAGE          COMMAND         CREATED             STATUS              PORTS               NAMES
[root@bogon ~]# docker ps -a
CONTAINER ID      IMAGE          COMMAND           CREATED                 STATUS              PORTS               NAMES
d1504ce8714d     centos      "/bin/bash"        About a minute ago   Exited (0) 20 seconds ago              agitated_dijkstra
# 拷贝容器中的文件到主机home目录下
[root@bogon ~]# docker cp d1504ce8714d:/home/test.java /home/
[root@bogon ~]# cd /home
[root@bogon home]# ls
test.java


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

思考:我们以后每次改动nginx配置文件都需要进入容器内部,十分麻烦!我们如果可以在容器外部提供一个映射路径。达到在容器外部修改文件,容器内部可以自动修改, -v 数据卷。

posted @ 2020-08-16 15:07  古城,老巷  阅读(208)  评论(0编辑  收藏  举报