3.Docker 的常用命令

概況

  • 如何獲得docker命令的幫助
  • docker 的常用命令

Docker命令使用幫助

Docker Client
Docker 命令行參考手冊

Docker 管理命令

命令 描述
dockerd Launch the Docker daemon
info Display system-wide information
inspect Return low-level information on a container or image
version Show the Docker version information

Docker Image 命令

命令 描述
build Build an image from a Dockerfile
commit Create a new image from a container’s changes
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
load Load an image from a tar archive or STDIN
rmi Remove one or more images
save Save images to a tar archive
tag Tag an image into a repository

Docker Container 命令

命令 描述
attach Attach to a running container
cp Copy files/folders from a container to a HOSTDIR or to STDOUT
create Create a new container
diff Inspect changes on a container’s filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container’s filesystem as a tar archive
kill Kill a running container
logs Fetch the logs of a container
pause Pause all processes within a container
port List port mappings or a specific mapping for the container
ps List containers
rename Rename a container
restart Restart a running container
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop a running container
top Display the running processes of a container
unpause Unpause all processes within a container
update Update configuration of one or more containers
wait Block until a container stops, then print its exit code

Docker Registry 命令

命令 描述
login Register or log in to a Docker registry
logout Log out from a Docker registry
pull Pull an image or a repository from a Docker registry
push Push an image or a repository to a Docker registry
search Search the Docker Hub for images

網(wǎng)絡和連通性命令
Docker 的network 一般為:null,host,bridge。Docker啟動容器的時候,默認使用 bridge 網(wǎng)絡

命令 描述
network connect Connect a container to a network
network create Create a new network
network disconnect Disconnect a container from a network
network inspect Display information about a network
network ls Lists all the networks the Engine daemon knows about
network rm Removes one or more networks

共享數(shù)據(jù)卷命令

共享數(shù)據(jù)卷命令,主要作用是能夠讓不同的容器可以共享數(shù)據(jù)。默認情況下,Docker為每個容器生成一個隨機名字的目錄來存放容器的數(shù)據(jù),這樣就不能實現(xiàn)容器之間的共享。使用 volume 命令,可以指定容器的數(shù)據(jù)目錄,這樣就可以做到多個容器之間數(shù)據(jù)共享

命令 描述
volume create Creates a new volume where containers can consume and store data
volume inspect Display information about a volume
volume ls Lists all the volumes Docker knows about
volume rm Remove one or more volumes

常用命令

[root@localhost ~]# docker help
Usage: docker [OPTIONS] COMMAND [arg...]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:

  --config=~/.docker              Location of client config files
  -D, --debug                     Enable debug mode
  -H, --host=[]                   Daemon socket(s) to connect to
  -h, --help                      Print usage
  -l, --log-level=info            Set the logging level
  --tls                           Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem    Path to TLS certificate file
  --tlskey=~/.docker/key.pem      Path to TLS key file
  --tlsverify                     Use TLS and verify the remote
  -v, --version                   Print version information and quit

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Export a container's filesystem as a tar archive
    history   Show the history of an image
    images    List images
    import    Import the contents from a tarball to create a filesystem image
    info      Display system-wide information
    inspect   Return low-level information on a container, image or task
    kill      Kill one or more running containers
    load      Load an image from a tar archive or STDIN
    login     Log in to a Docker registry.
    logout    Log out from a Docker registry.
    logs      Fetch the logs of a container
    network   Manage Docker networks
    node      Manage Docker Swarm nodes
    pause     Pause all processes within one or more containers
    port      List port mappings or a specific mapping for the container
    ps        List containers
    pull      Pull an image or a repository from a registry
    push      Push an image or a repository to a registry
    rename    Rename a container
    restart   Restart a container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save one or more images to a tar archive (streamed to STDOUT by default)
    search    Search the Docker Hub for images
    service   Manage Docker services
    start     Start one or more stopped containers
    stats     Display a live stream of container(s) resource usage statistics
    stop      Stop one or more running containers
    swarm     Manage Docker Swarm
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within one or more containers
    update    Update configuration of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.
[root@localhost ~]# docker version
Client:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:
 OS/Arch:      linux/amd64
[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 2
Server Version: 1.12.2
Storage Driver: devicemapper
 Pool Name: docker-253:0-67182400-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 472.6 MB
 Data Space Total: 107.4 GB
 Data Space Available: 17.06 GB
 Metadata Space Used: 1.184 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.146 GB
 Thin Pool Minimum Free Space: 10.74 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: null bridge overlay host
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 3.10.0-327.36.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.522 GiB
Name: localhost.localdomain
ID: VFSO:QWR6:ICKT:QGXY:UQXT:5VKN:L6E3:7JBE:OBVO:SQFK:PU2V:IBMS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8

實際示例流程:

  1. 從默認的倉庫拉取mysql鏡像
  2. 從國內第三方時速云拉取wordpress鏡像
  3. 查看本地有哪些鏡像
  4. 運行mysql鏡像生成名字叫做mysql2的容器,映射主機端口是13306,root密碼是123456
  5. 查看本地創(chuàng)建了哪些容器
  6. 交互方式進入mysql2容器的shell環(huán)境,創(chuàng)建一個數(shù)據(jù)庫testdb
[root@localhost ~]# docker pull mysql
[root@localhost ~]# docker pull index.tenxcloud.com/huangqg/wordpress
[root@localhost ~]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
mysql                                   latest              eda6a4884645        2 days ago          383.4 MB
hello-world                             latest              c54a2cc56cbb        3 months ago        1.848 kB
index.tenxcloud.com/huangqg/wordpress   latest              88bc02275ad7        12 months ago       485.5 MB

[root@localhost ~]# docker run -d -p 13306:3306  --name mysql2 -v /opt/mydata/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
01ff7db79a2bba578f4cef7024cd1e02bfec1084478e7835a79aeed317278b03
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
01ff7db79a2b        mysql:latest        "docker-entrypoint.sh"   29 seconds ago      Up 27 seconds       0.0.0.0:13306->3306/tcp   mysql2
[root@localhost ~]# docker exec -it mysql2 /bin/bash
root@01ff7db79a2b:/# mysql --version
mysql  Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using  EditLine wrapper
root@01ff7db79a2b:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)


主機(Win10中文版)訪問虛擬機MySQL的 Docker 容器 mysql2
看到了 testdb 的存在

D:\>mysql -h 192.168.1.161 -P 13306 -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 轉載自 http://blog.opskumu.com/docker.html 一、Docker 簡介 Docke...
    極客圈閱讀 10,728評論 0 120
  • 摘要:繼續(xù)docker的學習之旅,今天練習一些常用的命令: 一、鏡像相關 1.1 列出本機所有鏡像 docker ...
    暖夏未眠丶閱讀 844評論 0 0
  • 0x01 核心概念 Docker鏡像類似于虛擬機鏡像,可以理解為一個面向Docker引擎的只讀模板,包含了文件系統(tǒng)...
    閑云逸心閱讀 4,955評論 0 9
  • Docker — 云時代的程序分發(fā)方式 要說最近一年云計算業(yè)界有什么大事件?Google Compute Engi...
    ahohoho閱讀 15,828評論 15 147
  • 今天我吃的可多了,我吃的有火龍果,而且是紅心的,我還喝了一碗很健康的米糊,我還喝了果汁,最重要的是我吃了媽媽親手做...
    王佑滋閱讀 197評論 0 0

友情鏈接更多精彩內容