Docker ClI -常用命令使用

网友投稿 248 2022-10-18

Docker ClI -常用命令使用

文章目录

​​docker help 命令帮助​​​​docker 版本​​​​docker 启动​​​​docker 版本​​​​docker 默认远程仓库​​​​docker search 查看当前image的有些版本​​​​docker images=docker image ls 列出本地所有的镜像​​​​docker pull [image-name] 拉取远程镜像​​​​docker rmi -f [image-name] 删除镜像​​​​docker run [iamge-name] 运行image​​

​​docker run 多个参数 显示测试​​

​​docker ps 查看运行的Container​​​​docker 进入指定的Container 目录[进入容器]​​​​docker rm -f 删除所有的Container​​​​docker login 登录你初始暗转docker时设置的镜像仓库​​

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

​​docker tag 对镜像image进行打版本​​​​docker push 对镜像往仓库中推送​​​​docker 网络介绍​​

​​宿主主机网络[本地机器]​​​​虚拟机网络​​

​​登录tomcat查看其ip​​

docker help 命令帮助

[root@localhost ~]# docker help

docker 版本

[root@localhost ~]# docker versionClient: 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: falseCannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?[root@localhost ~]#

[root@localhost ~]# docker -vDocker version 19.03.4, build 9013bf583a

docker 启动

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

docker 版本

[root@localhost ~]# docker versionClient: 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: falseServer: 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 默认远程仓库

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

docker search [image-name]

[root@localhost ~]# docker search mysqlNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmysql 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 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 imagesREPOSITORY TAG IMAGE ID CREATED SIZEtomcat jdk8-adoptopenjdk-hotspot 50d274ef4fb3 10 days ago 324MB[vagrant@localhost ~]$

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

1.这里注意下,默认拉取的镜像是是来自镜像仓库:~]$ sudo docker pull hello-worldUsing default tag: latestlatest: Pulling from library/hello-world1b930d010525: Already exists Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5fStatus: Downloaded newer image for hello-world:latestdocker.io/library/hello-world:latest[vagrant@localhost ~]$

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

[vagrant@localhost ~]$ sudo docker rmi -f hello-worldUntagged: hello-world:latestUntagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5fDeleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e[vagrant@localhost ~]$ [vagrant@localhost ~]$

docker run [iamge-name] 运行image

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

[vagrant@localhost ~]$ sudo docker run hello-worldHello 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 bashShare images, automate workflows, and more with a free Docker ID: more examples and ideas, visit: ~]$

docker run 多个参数 显示测试

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

说明

-d 后台运行--name 指定Container的名字-p 指定端口 第一个9090是虚拟机里容器的端口,8080

访问

​​ps 查看运行的Container

[vagrant@localhost ~]$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESff066ca987a1 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/bashroot@bab8ba192e90:/usr/local/tomcat# ls -latotal 124drwxr-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.txtdrwxr-xr-x. 2 root root 4096 Oct 19 02:26 bindrwxr-sr-x. 1 root root 22 Nov 19 09:39 confdrwxr-sr-x. 2 root staff 78 Oct 19 02:25 includedrwxr-xr-x. 2 root root 4096 Oct 19 02:25 libdrwxrwxrwx. 1 root root 177 Nov 19 09:39 logsdrwxr-sr-x. 3 root staff 151 Oct 19 02:25 native-jni-libdrwxrwxrwx. 2 root root 30 Oct 19 02:25 tempdrwxr-xr-x. 7 root root 81 Oct 7 13:31 webappsdrwxrwxrwx. 1 root root 22 Nov 19 09:39 workroot@bab8ba192e90:/usr/local/tomcat#

docker rm -f 删除所有的Container

[root@localhost ~]# docker ps -aq3a0199a0c54c43093d522f46bab8ba192e90728f3cbbe2abff066ca987a13b12849c751168c3dcd62dcd76c903c5eb6e[root@localhost ~]#

docker ps 参数介绍

-a :显示所有的容器,包括未运行的。-q :静默模式,只显示容器编号。

其他参数介绍参考

​​~]# docker rm -f $(docker ps -aq)3a0199a0c54c43093d522f46bab8ba192e90728f3cbbe2abff066ca987a13b12849c751168c3dcd62dcd76c903c5eb6e[root@localhost ~]

注意

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

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

[root@localhost ~]# docker loginLogin with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to to create one.Username: gaoxinfuPassword: WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. SeeSucceeded

docker login

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

[root@localhost ~]# docker login --username=XXXXX WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. SeeSucceeded[root@localhost ~]#

docker tag 对镜像image进行打版本

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

docker push 对镜像往仓库中推送

[root@localhost ~]# docker push gaoxinfu/demo-tool-docker:v1.0.0The 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$ ifconfiglo0: flags=8049 mtu 16384 options=1203 inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 nd6 options=201gif0: flags=8010 mtu 1280stf0: flags=0<> mtu 1280en0: flags=8863 mtu 1500 options=400 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 media: autoselect status: activeen1: flags=8963 mtu 1500 options=460 ether 82:4f:5d:81:54:01 media: autoselect status: inactiveen2: flags=8963 mtu 1500 options=460 ether 82:4f:5d:81:54:00 media: autoselect status: inactivebridge0: flags=8863 mtu 1500 options=63 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 ifmaxaddr 0 port 5 priority 0 path cost 0 member: en2 flags=3 ifmaxaddr 0 port 6 priority 0 path cost 0 nd6 options=201 media: status: inactivep2p0: flags=8843 mtu 2304 options=400 ether 0a:f9:d3:69:5b:c8 media: autoselect status: inactiveawdl0: flags=8943 mtu 1484 options=400 ether a6:5d:97:7b:ac:6f inet6 fe80::a45d:97ff:fe7b:ac6f%awdl0 prefixlen 64 scopeid 0x9 nd6 options=201 media: autoselect status: activellw0: flags=8863 mtu 1500 options=400 ether a6:5d:97:7b:ac:6f inet6 fe80::a45d:97ff:fe7b:ac6f%llw0 prefixlen 64 scopeid 0xa nd6 options=201 media: autoselect status: activeutun0: flags=8051 mtu 1380 inet6 fe80::84ec:7f74:7cda:da2b%utun0 prefixlen 64 scopeid 0xb nd6 options=201utun1: flags=8051 mtu 2000 inet6 fe80::3ed1:5e7:19d2:95b6%utun1 prefixlen 64 scopeid 0xc nd6 options=201en5: flags=8863 mtu 1500 options=40b ether 00:0e:c6:c1:e1:75 nd6 options=201 media: autoselect () status: inactivelocalhost:~

备注

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

虚拟机网络

我们登陆进去我们通过Vagrant创建的虚拟机centos7

[root@localhost ~]# ip a1: lo: 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 forever2: eth0: 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 forever3: eth1: 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 forever4: docker0: 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.6 )3.docker0:网卡172.17.0.1,这个主要是跟docker0中启动的容器Container去通信的一个网卡,因为实际上对于 容器,有的时会生成网卡的,如下:

登录tomcat查看其ip

[root@localhost ~]# docker run -d --name first-tomcat -p 9090:8080 tomcatf077cd16b0517d188ae8bac12376d110b775dd527340006665c4a550ce41d597[root@localhost ~]# docker exec -it first-tomcat /bin/bashroot@f077cd16b051:/usr/local/tomcat# ip a1: lo: 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 forever7: eth0@if8: 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 foreverroot@f077cd16b051:/usr/local/tomcat#

备注

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

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

[root@localhost ~]# curl 172.17.0.2:8080

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 常见排序算法代码分享
下一篇:Docker之Docker Swarm介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~