Docker 常用命令


1. 在宿主机中查看 docker容器的ip地址

[root@hadoop mydockerbuild]# docker inspect docker_test1 --format '{{.NetworkSettings.IPAddress}}'
172.17.0.2

此处 docker_test1 为docker容器的name

 

2. 在安装docker的虚拟机上 可以使用 如下命令查看docker的安装版本信息

[root@hadoop mydockerbuild]# docker -v
Docker version 1.13.0, build 49bf474

使用这个命令时,不需要docker的守护进程已经启动,但docker的其他命令都需要docker的守护进程启动。

3. 使用docker version查看docker的详细信息

[root@hadoop mydockerbuild]# docker version
Client:
 Version:      1.13.0
 API version:  1.25
 Go version:   go1.7.3
 Git commit:   49bf474
 Built:        Tue Jan 17 09:55:28 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.13.0
 API version:  1.25 (minimum version 1.12)
 Go version:   go1.7.3
 Git commit:   49bf474
 Built:        Tue Jan 17 09:55:28 2017
 OS/Arch:      linux/amd64
 Experimental: false

返回的是docker client和docker服务端信息,如果docker的守护进程没有启动,之返回client信息。server信息不会显示,会提示无法连接到 docker daemon。

 4. 进入到Container内部:

docker exec -it website /bin/bash

 不推荐使用 attach ,使用attach 在退出Container时 Container会挂掉

因为使用的时Centos作为基础镜像,所以进入Container后就有了Centos的所有功能

进入容器后可以使用 supervisorctl查看所有已启动的服务

可以使用exit 退出容器

5.退出docker容器可以使用快捷键 Ctrl+P + Ctrl+Q

[root@hadoop mydockerbuild]# docker exec -it docker_test1 /bin/bash
root@test1:/# [root@hadoop mydockerbuild]# 
[root@hadoop mydockerbuild]# 

6.查看docker的状态

[hadoop@hadoop mydockerbuild]$ service docker status
Redirecting to /bin/systemctl status  docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-02-27 16:11:47 CST; 1 day 19h ago #状态为 running
     Docs: https://docs.docker.com
 Main PID: 27046 (dockerd)
   Memory: 648.1M
   CGroup: /system.slice/docker.service
           ├─ 6530 docker-containerd-shim 8e98459a34fa3d79fcb9124f38c58c72aac...
           ├─15283 docker-containerd-shim 8e98459a34fa3d79fcb9124f38c58c72aac...
           ├─15474 docker-containerd-shim 8e98459a34fa3d79fcb9124f38c58c72aac...
           ├─27046 /usr/bin/dockerd
           └─27056 docker-containerd -l unix:///var/run/docker/libcontainerd/...
[hadoop@hadoop mydockerbuild]$ 

网上所也可以使用 sudo status docker 查看docker的守护进程是否存在,试了不起作用,可能是因为 在Red Hat和Fedora中。

7. 同样使用service 可以完成docker的启动和停止

停止docker

hadoop@hadoop mydockerbuild]$ sudo service docker stop #停止docker
Redirecting to /bin/systemctl stop  docker.service
[hadoop@hadoop mydockerbuild]$ service docker status  #查看docker状态
Redirecting to /bin/systemctl status  docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2017-03-01 11:21:26 CST; 9s ago  #这里Active为 dead
     Docs: https://docs.docker.com
 Main PID: 27046 (code=exited, status=0/SUCCESS)
[hadoop@hadoop mydockerbuild]$ 

启动docker 并查看状态

[hadoop@hadoop mydockerbuild]$ service docker start
Redirecting to /bin/systemctl start  docker.service
[hadoop@hadoop mydockerbuild]$ service docker status
Redirecting to /bin/systemctl status  docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2017-03-01 11:24:54 CST; 9s ago
     Docs: https://docs.docker.com
 Main PID: 16032 (dockerd)
   Memory: 21.7M
   CGroup: /system.slice/docker.service
           ├─16032 /usr/bin/dockerd
           └─16042 docker-containerd -l unix:///var/run/docker/libcontainerd/...
[hadoop@hadoop mydockerbuild]$ 

8. 查看docker的环境信息

docker info 此命令需要docker daemon已经启动

[hadoop@hadoop mydockerbuild]$ sudo docker info
Containers: 11
 Running: 0
 Paused: 0
 Stopped: 11
Images: 6
Server Version: 1.13.0
Storage Driver: overlay
 Backing Filesystem: xfs
 Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 03e5862ec0d8d3b3f750e19fca3ee367e13c090e
runc version: 2f7393a47307a16f8cee44a37b262e8b81021e3e
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.441 GiB
Name: hadoop
ID: Z2IN:M5GX:54NG:VYBH:MDLI:QIST:SHGB:LZUK:2ZJR:RI3M:P6M5:ZRMK
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
[hadoop@hadoop mydockerbuild]$ 

9. 查询docker主机上已有的docker镜像

[hadoop@hadoop mydockerbuild]$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              0ef2e08ed3fa        31 hours ago        130 MB
ubuntu              14.04               7c09e61e9035        31 hours ago        188 MB
docker-whale        latest              17104b3a899f        42 hours ago        256 MB
hello-world         latest              48b5124b2768        6 weeks ago         1.84 kB
docker/whalesay     latest              6b362a9f73eb        21 months ago       247 MB
[hadoop@hadoop mydockerbuild]$ 

本机的所有镜像都保存在 本机的 /var/lib/docker 目录下。

10. 查询docker主机上已经创建的容器

查询所有已启动的容器

[hadoop@hadoop mydockerbuild]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

查询所有的容器,包括启动和未启动的

11. 使用docker search 查找镜像

[hadoop@hadoop mydockerbuild]$ sudo docker search centos
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                 The official build of CentOS.                   3130      [OK]       
jdeathe/centos-ssh                     CentOS-6 6.8 x86_64 / CentOS-7 7.3.1611 x8...   60                   [OK]
jdeathe/centos-ssh-apache-php          CentOS-6 6.8 x86_64 - Apache / PHP-FPM / P...   25                   [OK]
nimmis/java-centos                     This is docker images of CentOS 7 with dif...   23                   [OK]
consol/centos-xfce-vnc                 Centos container with "headless" VNC sessi...   22                   [OK]
million12/centos-supervisor            Base CentOS-7 with supervisord launcher, h...   13                   [OK]
nickistre/centos-lamp                  LAMP on centos setup                            10                   [OK]
torusware/speedus-centos               Always updated official CentOS docker imag...   8                    [OK]
egyptianbman/docker-centos-nginx-php   A simple and highly configurable docker co...   6                    [OK]
nathonfowlie/centos-jre                Latest CentOS image with the JRE pre-insta...   5                    [OK]
centos/mariadb55-centos7                                                               4                    [OK]
harisekhon/centos-scala                Scala + CentOS (OpenJDK tags 2.10-jre7 - 2...   2                    [OK]
centos/tools                           Docker image that has systems administrati...   2                    [OK]
centos/redis                           Redis built for CentOS                          2                    [OK]
harisekhon/centos-java                 Java on CentOS (OpenJDK, tags jre/jdk7-8)       2                    [OK]
freenas/centos                         Simple CentOS Linux interactive container       1                    [OK]
darksheer/centos                       Base Centos Image -- Updated hourly             1                    [OK]
timhughes/centos                       Centos with systemd installed and running       1                    [OK]
blacklabelops/centos                   CentOS Base Image! Built and Updates Daily!     1                    [OK]
vcatechnology/centos                   A CentOS Image which is updated daily           0                    [OK]
smartentry/centos                      centos with smartentry                          0                    [OK]
januswel/centos                        yum update-ed CentOS image                      0                    [OK]
otagoweb/centos                        Apache (with PHP7), built on CentOS 7           0                    [OK]
termbox/centos                         CentOS                                          0                    [OK]
repositoryjp/centos                    Docker Image for CentOS.                        0                    [OK]
[hadoop@hadoop mydockerbuild]$ 

12.拉取一个镜像

[hadoop@hadoop mydockerbuild]$ sudo docker pull centos:7.1

如果这里不写 :7.1 则会默认拉取最新的镜像

[hadoop@hadoop mydockerbuild]$ sudo docker pull centos
Using default tag: latest

这里拉取镜像从Docker Hub 跟网络有关系,如果网络不通畅,有可能拉取不成功。

13.将镜像转载为容器 使用 docker run

有了镜像之后,可以使用 docker run 将镜像变成容器:

docker run [-it|-d] -p 2222:22 --name base csphere/centos:7.1

-it 采用交互的方式

-d 把Container在后台启动以后,Container会返回一个ContainerID

-P 22 设置这个参数后 表明容器暴露的端口为22 宿主机会随机的找一个没有被使用的 端口号和 22 这个端口进行映射(假如随机2222:22) 即可以在外部直接使用 2222 端口直接访问容器,在宿主机重启后又会找一个随机未被使用的端口号进行映射

-p 2222:22 不管是宿主机重启还是Container重启,这个2222一直会给22端口映射,除非这个Container删掉以后会把这个2222给其他的Container

--name 通过docker镜像生成docker 容器,给docker容器起一个名字以方便以后去查找

使用 csphere/centos:7.1 来创建叫做base的docker容器

14. 删除一个容器

[hadoop@hadoop dockerJavaTomcat]$ sudo docker rm /base
/base

/base 为容器名称

 

15. 删除一个镜像 test/centos 为镜像名称

hadoop@hadoop dockerJavaTomcat]$ sudo docker rmi test/centos
Untagged: test/centos:latest
Deleted: sha256:1c1e0afd4637f9f6d0eeef78b962f553909b656a585451cc72d2b7709a1301b6
Deleted: sha256:3849143870100cd42cbe9c9d7d57b86f770eb1562cba0137a1ccbd125356c6b7
Deleted: sha256:2901facb33c9e596228d32cd3833911d45473ac1b1917d0b67d2feafd9ae8011
Deleted: sha256:de37cfdfa4eb377c944cb94a15474a1c4ab24577d596779e145bdff890e4c376
Deleted: sha256:dffb0316ca99c427eb8e7fcec81b8b8f2f2139e70ffe5b017e535b8c927a3678
Deleted: sha256:bf9b522b6a825775eec04fabc700759394ed7e5ed284bb764df8cbff5da6c3de
Deleted: sha256:8d44342286e2f23644f3dd49d35b8f6913720383cbb126def144945859ba415c
Deleted: sha256:52fba5ea150c68a6325cb59386678f2a0e5e3f33b3450d15ac55662b19d04cc4
Deleted: sha256:aa497055a9f4f9ca607de6865e47e5c342728bdb7a359a2a3879b9e01e9705f5
Deleted: sha256:12bf021320b738dfab4c1052e508b3cde752af333dd047a503146e37a57d27c3
Deleted: sha256:25dfd5409299e45a52204719788a64f4d6ec42b94ed1c1c52b444dffe1617530
Deleted: sha256:2a8f3b2595c0d7bbd96c8311aafd38a19ee38dfd7db10f5f143e12a488453a97
Deleted: sha256:f8ac2fbb89387ed573175a3207a2a7228bacee098e98d859ef5cc69280c634bd
Deleted: sha256:ca6ee1356e1919580f56aba2416d558358f13e0fd4af63fc1316a08b3b6cf556
Deleted: sha256:9fcaf31daa347938f67d3d57510ac2167b1943ab3046d2edc80025b3e78db016
Deleted: sha256:c2cfaed91fa85a1e25a93902ccf7b0adfd6d11a96ad60e05c5743deac4561a0d
Deleted: sha256:5179194bb64957b92d747d0ef8658bd2c94e14724184f84bba5f6a97a3b52600
Deleted: sha256:3c5d03deca832571ebaf26e19cafcbb06b5770339dbd669bd938930942ca9ac8
[hadoop@hadoop dockerJavaTomcat]$ 

16.docker tag imageid name:tag

使用 docker tag 可以修改image 的名称

[root@hadoop dockerPubProgram]# docker tag 6564d0f11d31 js/centos:oracle-package

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2020 CODEPRJ.COM