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