Docker ClI -常用命令使用

docker help 命令帮助

[root@localhost ~]# docker help

docker 版本

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.4
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        9013bf583a
 Built:             Fri Oct 18 15:52:22 2019
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost ~]# 
[root@localhost ~]# docker -v
Docker version 19.03.4, build 9013bf583a

docker 启动

[vagrant@localhost ~]$ sudo systemctl start docker
[vagrant@localhost ~]$ 

docker 版本

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.4
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        9013bf583a
 Built:             Fri Oct 18 15:52:22 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.4
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       9013bf583a
  Built:            Fri Oct 18 15:50:54 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
[root@localhost ~]# 
从上面发现,有一个client端
有一个server 服务端

docker 默认远程仓库

https://hub.docker.com/

docker search 查看当前image的有些版本

docker search [image-name]

[root@localhost ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8848                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3105                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   655                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   460                 [OK]                
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   64                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   56                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      35                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   28                                      [OK]
prom/mysqld-exporter                                                              23                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   22                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   17                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   16                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   13                                      
arey/mysql-client                 Run a MySQL client from a docker container      12                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   4                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  3                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
[root@localhost ~]# 

docker images=docker image ls 列出本地所有的镜像

[vagrant@localhost ~]$ sudo docker images
REPOSITORY          TAG                         IMAGE ID            CREATED             SIZE
tomcat              jdk8-adoptopenjdk-hotspot   50d274ef4fb3        10 days ago         324MB
[vagrant@localhost ~]$

docker pull [image-name] 拉取远程镜像

1.这里注意下,默认拉取的镜像是是来自镜像仓库:https://hub.docker.com/
2.默认拉取最新的镜像
[vagrant@localhost ~]$ sudo docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Already exists 
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[vagrant@localhost ~]$

docker rmi -f [image-name] 删除镜像

[vagrant@localhost ~]$ sudo docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
[vagrant@localhost ~]$ 
[vagrant@localhost ~]$

docker run [iamge-name] 运行image

会先查看本地是否已经存在当前要运行的image
如果有直接运行
如果没有,则先去远端去拉取 docker pull [image-name]
[vagrant@localhost ~]$ sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[vagrant@localhost ~]$ 

docker run 多个参数 显示测试

[vagrant@localhost ~]$ sudo docker run -d --name firsttomcat -p 9090:8080 tomcat
ff066ca987a108d29ffdb3ede1ae214316f2c8a9f3722b6ddc89b41532c423ad
  

说明

-d 后台运行

--name 指定Container的名字

-p 指定端口  第一个9090是虚拟机里容器的端口,8080 是映射的主机的端口

访问

http://192.168.1.7:9090/

虚拟机的ip+端口

在这里插入图片描述

docker ps 查看运行的Container

[vagrant@localhost ~]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
ff066ca987a1        tomcat              "catalina.sh run"   2 minutes ago       Up 2 minutes        0.0.0.0:9090->8080/tcp   firsttomcat
[vagrant@localhost ~]$ 

docker 进入指定的Container 目录[进入容器]

[vagrant@localhost ~]$ sudo docker exec -it first-tomcat /bin/bash
root@bab8ba192e90:/usr/local/tomcat# ls -la
total 124
drwxr-sr-x. 1 root staff    42 Oct 19 02:25 .
drwxrwsr-x. 1 root staff    20 Oct 19 02:18 ..
-rw-r--r--. 1 root root  19318 Oct  7 13:33 BUILDING.txt
-rw-r--r--. 1 root root   5407 Oct  7 13:33 CONTRIBUTING.md
-rw-r--r--. 1 root root  57011 Oct  7 13:33 LICENSE
-rw-r--r--. 1 root root   1726 Oct  7 13:33 NOTICE
-rw-r--r--. 1 root root   3255 Oct  7 13:33 README.md
-rw-r--r--. 1 root root   7136 Oct  7 13:33 RELEASE-NOTES
-rw-r--r--. 1 root root  16262 Oct  7 13:33 RUNNING.txt
drwxr-xr-x. 2 root root   4096 Oct 19 02:26 bin
drwxr-sr-x. 1 root root     22 Nov 19 09:39 conf
drwxr-sr-x. 2 root staff    78 Oct 19 02:25 include
drwxr-xr-x. 2 root root   4096 Oct 19 02:25 lib
drwxrwxrwx. 1 root root    177 Nov 19 09:39 logs
drwxr-sr-x. 3 root staff   151 Oct 19 02:25 native-jni-lib
drwxrwxrwx. 2 root root     30 Oct 19 02:25 temp
drwxr-xr-x. 7 root root     81 Oct  7 13:31 webapps
drwxrwxrwx. 1 root root     22 Nov 19 09:39 work
root@bab8ba192e90:/usr/local/tomcat# 

docker rm -f 删除所有的Container

[root@localhost ~]# docker ps -aq
3a0199a0c54c
43093d522f46
bab8ba192e90
728f3cbbe2ab
ff066ca987a1
3b12849c7511
68c3dcd62dcd
76c903c5eb6e
[root@localhost ~]#
  

docker ps 参数介绍

-a :显示所有的容器,包括未运行的。

-q :静默模式,只显示容器编号。

其他参数介绍参考

https://www.runoob.com/docker/docker-ps-command.html

开始删除

[root@localhost ~]# docker rm -f $(docker ps -aq)
3a0199a0c54c
43093d522f46
bab8ba192e90
728f3cbbe2ab
ff066ca987a1
3b12849c7511
68c3dcd62dcd
76c903c5eb6e
[root@localhost ~]

注意

1.这里要注意,一旦这里删除,容器就不存在了,也就意味着容器里面的服务都不能访问;

docker login 登录你初始暗转docker时设置的镜像仓库

[root@localhost ~]# 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: gaoxinfu
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

docker login

docker login --username=XXXXX 地址 登录其他的镜像仓库

[root@localhost ~]# docker login --username=XXXXX https://vuwks9eu.mirror.aliyuncs.com
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@localhost ~]# 

docker tag 对镜像image进行打版本

[root@localhost ~]# docker tag demo-tool-docker gaoxinfu/demo-tool-docker:v1.0.0
[root@localhost ~]# docker images
REPOSITORY                  TAG                         IMAGE ID            CREATED             SIZE
gaoxinfu/demo-tool-docker   v1.0.0                      79e08d8bb28b        26 hours ago        506MB
demo-tool-docker            latest                      79e08d8bb28b        26 hours ago        506MB
openjdk                     8                           09df0563bdfc        3 days ago          488MB
tomcat                      jdk8-adoptopenjdk-hotspot   50d274ef4fb3        2 weeks ago         324MB
tomcat                      latest                      882487b8be1d        5 weeks ago         507MB
hello-world                 latest                      fce289e99eb9        11 months ago       1.84kB

docker push 对镜像往仓库中推送

[root@localhost ~]# docker push gaoxinfu/demo-tool-docker:v1.0.0
The push refers to repository [docker.io/gaoxinfu/demo-tool-docker]
90416f43051e: Pushed 
2ee490fbc316: Pushed 
b18043518924: Pushed 
9a11244a7e74: Pushed 
5f3a5adb8e97: Pushed 
73bfa217d66f: Pushed 
91ecdd7165d3: Pushed 
e4b20fcc48f4: Pushed 
v1.0.0: digest: sha256:83cc20142ff6f628cbef49b5d2fe09a16d53b3ce5b24ebf5b8e6fafb1bec2f55 size: 2007

docker 网络介绍

宿主主机网络[本地机器]

localhost:~ gaoxinfu$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
	options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
	inet 127.0.0.1 netmask 0xff000000 
	inet6 ::1 prefixlen 128 
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
	nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 38:f9:d3:69:5b:c8 
	inet6 fe80::148e:8661:e8fe:212e%en0 prefixlen 64 secured scopeid 0x4 
	inet 192.168.1.6 netmask 0xffffff00 broadcast 192.168.1.255
	inet6 2408:8025:2aa:31f0:c5d:b715:a4ba:46b4 prefixlen 64 autoconf secured 
	inet6 2408:8025:2aa:31f0:3866:eabf:de36:7e42 prefixlen 64 autoconf temporary 
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=460<TSO4,TSO6,CHANNEL_IO>
	ether 82:4f:5d:81:54:01 
	media: autoselect <full-duplex>
	status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=460<TSO4,TSO6,CHANNEL_IO>
	ether 82:4f:5d:81:54:00 
	media: autoselect <full-duplex>
	status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=63<RXCSUM,TXCSUM,TSO4,TSO6>
	ether 82:4f:5d:81:54:01 
	Configuration:
		id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
		maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
		root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
		ipfilter disabled flags 0x2
	member: en1 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 5 priority 0 path cost 0
	member: en2 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 6 priority 0 path cost 0
	nd6 options=201<PERFORMNUD,DAD>
	media: <unknown type>
	status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
	options=400<CHANNEL_IO>
	ether 0a:f9:d3:69:5b:c8 
	media: autoselect
	status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
	options=400<CHANNEL_IO>
	ether a6:5d:97:7b:ac:6f 
	inet6 fe80::a45d:97ff:fe7b:ac6f%awdl0 prefixlen 64 scopeid 0x9 
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
llw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether a6:5d:97:7b:ac:6f 
	inet6 fe80::a45d:97ff:fe7b:ac6f%llw0 prefixlen 64 scopeid 0xa 
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
	inet6 fe80::84ec:7f74:7cda:da2b%utun0 prefixlen 64 scopeid 0xb 
	nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
	inet6 fe80::3ed1:5e7:19d2:95b6%utun1 prefixlen 64 scopeid 0xc 
	nd6 options=201<PERFORMNUD,DAD>
en5: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=40b<RXCSUM,TXCSUM,VLAN_HWTAGGING,CHANNEL_IO>
	ether 00:0e:c6:c1:e1:75 
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect (<unknown type>)
	status: inactive
localhost:~ gaoxinfu$ 

备注

1.从上面我们可以看到,我们本地机器的ip:192.168.1.6

虚拟机网络

我们登陆进去我们通过Vagrant创建的虚拟机centos7
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 62988sec preferred_lft 62988sec
    inet6 fe80::5054:ff:fe8a:fee6/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:cc:9d:4d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.7/24 brd 192.168.1.255 scope global noprefixroute dynamic eth1
       valid_lft 258009sec preferred_lft 258009sec
    inet6 fe80::a00:27ff:fecc:9d4d/64 scope link 
       valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:f6:8f:53:c0 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:f6ff:fe8f:53c0/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

备注

1.可以看到我们上面有四个网卡
2.eth1:网卡ip 192.168.1.7,这个主要是跟宿主主机去尽心通信的一个网卡(宿主主机的IP:192.168.1.63.docker0:网卡172.17.0.1,这个主要是跟docker0中启动的容器Container去通信的一个网卡,因为实际上对于
  容器,有的时会生成网卡的,如下:

登录tomcat查看其ip

[root@localhost ~]# docker run -d --name first-tomcat -p 9090:8080 tomcat
f077cd16b0517d188ae8bac12376d110b775dd527340006665c4a550ce41d597
[root@localhost ~]# docker exec -it first-tomcat /bin/bash
root@f077cd16b051:/usr/local/tomcat# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
root@f077cd16b051:/usr/local/tomcat# 

备注

1.从上面我们可以看到tomcat 启动的内部的ip是 172.17.0.2
2.这里要注意,宿主主机是无法访问这个ip,但是虚拟机centos7这个是可以ping通的

直接访问一下,可以出现页面

[root@localhost ~]# curl 172.17.0.2:8080

<!DOCTYPE html>
<html lang="en">
    <head>
。。。。。

在这里插入图片描述

发布了261 篇原创文章 · 获赞 37 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/u014636209/article/details/103137326