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

网络和连通性命令
Docker 的network 一般为:null,host,bridgeDocker启动容器的时候,默认使用 bridge 网络

命令 描述
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

共享数据卷命令

共享数据卷命令,主要作用是能够让不同的容器可以共享数据。默认情况下,Docker为每个容器生成一个随机名字的目录来存放容器的数据,这样就不能实现容器之间的共享。使用 volume 命令,可以指定容器的数据目录,这样就可以做到多个容器之间数据共享

命令 描述
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. 查看本地创建了哪些容器
  6. 交互方式进入mysql2容器的shell环境,创建一个数据库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)

推荐阅读更多精彩内容

  • 摘要:继续docker的学习之旅,今天练习一些常用的命令: 一、镜像相关 1.1 列出本机所有镜像 docker ...
    暖夏未眠丶阅读 382评论 0 0
  • 今天我吃的可多了,我吃的有火龙果,而且是红心的,我还喝了一碗很健康的米糊,我还喝了果汁,最重要的是我吃了妈妈亲手做...
    王佑滋阅读 40评论 0 0
  • 1 昨天早上,想起好久没吃牛肉面了,特别想吃,我和同事相邀就到了单位旁边的早餐店。 没想到平常冷清的小店却热闹非凡...
    静听心音_beee阅读 301评论 0 4
  • 张大千--中国泼墨画家,书法家,中国花鸟画的泰斗,一生画荷无数。 飘逸的荷叶千姿百态,传神的墨色浓淡深浅,荷梗刚劲...
    兰郡子阅读 167评论 0 0
  • 我不知道世间有什么是确定不变的,我只知道,只要一看到星星,我就会开始做梦。 ...
    古城写诗人阅读 182评论 6 4