Docker 常用指令

命令格式:

docker的命令格式如下:
docker [OPTIONS] COMMAND [arg...]

OPTION(可選參數(shù)):

Options:

  --api-cors-header=                   Set CORS headers in the remote API
  -b, --bridge=                        Attach containers to a network bridge
  --bip=                               Specify network bridge IP
  -D, --debug=false                    Enable debug mode
  -d, --daemon=false                   Enable daemon mode
  --default-gateway=                   Container default gateway IPv4 address
  --default-gateway-v6=                Container default gateway IPv6 address
  --default-ulimit=[]                  Set default ulimits for containers
  --dns=[]                             DNS server to use
  --dns-search=[]                      DNS search domains to use
  -e, --exec-driver=native             Exec driver to use
  --exec-opt=[]                        Set exec driver options
  --exec-root=/var/run/docker          Root of the Docker execdriver
  --fixed-cidr=                        IPv4 subnet for fixed IPs
  --fixed-cidr-v6=                     IPv6 subnet for fixed IPs
  -G, --group=docker                   Group for the unix socket
  -g, --graph=/var/lib/docker          Root of the Docker runtime
  -H, --host=[]                        Daemon socket(s) to connect to
  -h, --help=false                     Print usage
  --icc=true                           Enable inter-container communication
  --insecure-registry=[]               Enable insecure registry communication
  --ip=0.0.0.0                         Default IP when binding container ports
  --ip-forward=true                    Enable net.ipv4.ip_forward
  --ip-masq=true                       Enable IP masquerading
  --iptables=true                      Enable addition of iptables rules
  --ipv6=false                         Enable IPv6 networking
  -l, --log-level=info                 Set the logging level
  --label=[]                           Set key=value labels to the daemon
  --log-driver=json-file               Default driver for container logs
  --log-opt=map[]                      Set log driver options
  --mtu=0                              Set the containers network MTU
  -p, --pidfile=/var/run/docker.pid    Path to use for daemon PID file
  --registry-mirror=[]                 Preferred Docker registry mirror
  -s, --storage-driver=                Storage driver to use
  --selinux-enabled=false              Enable selinux support
  --storage-opt=[]                     Set storage driver options
  --tls=false                          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=false                    Use TLS and verify the remote
  --userland-proxy=true                Use userland proxy for loopback traffic
  -v, --version=false                  Print version information and quit

COMMAND(命令)

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 from a container's filesystem to the host path
    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    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container or image
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    pause     Pause all processes within a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    rename    Rename an existing container
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image on the Docker Hub
    start     Start a stopped container
    stats     Display a stream of a containers' resource usage statistics
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code

arg... (命令參數(shù))

可以通過 docker COMMAND --help 查看命令支持的參數(shù)

[root@niloay ~]# docker ps --help

Usage: docker ps [OPTIONS]

List containers

  -a, --all=false       Show all containers (default shows just running)
  --before=             Show only container created before Id or Name
  -f, --filter=[]       Filter output based on conditions provided
  --help=false          Print usage
  -l, --latest=false    Show the latest created container, include non-running
  -n=-1                 Show n last created containers, include non-running
  --no-trunc=false      Don't truncate output
  -q, --quiet=false     Only display numeric IDs
  -s, --size=false      Display total file sizes
  --since=              Show created since Id or Name, include non-running

docker run 命令

 docker run [OPTIONS] IMAGE [COMMAND] [ARG...]  
  -a, --attach=[]            登錄容器(以docker run -d啟動的容器)  
  -c, --cpu-shares=0         設(shè)置容器CPU權(quán)重,在CPU共享場景使用  
  --cap-add=[]               添加權(quán)限,權(quán)限清單詳見:http://linux.die.net/man/7/capabilities  
  --cap-drop=[]              刪除權(quán)限,權(quán)限清單詳見:http://linux.die.net/man/7/capabilities  
  --cidfile=""               運行容器后,在指定文件中寫入容器PID值,一種典型的監(jiān)控系統(tǒng)用法  
  --cpuset=""                設(shè)置容器可以使用哪些CPU,此參數(shù)可以用來容器獨占CPU  
  -d, --detach=false         指定容器運行于前臺還是后臺   
  --device=[]                添加主機(jī)設(shè)備給容器,相當(dāng)于設(shè)備直通  
  --dns=[]                   指定容器的dns服務(wù)器  
  --dns-search=[]            指定容器的dns搜索域名,寫入到容器的/etc/resolv.conf文件  
  -e, --env=[]               指定環(huán)境變量,容器中可以使用該環(huán)境變量  
  --entrypoint=""            覆蓋image的入口點  
  --env-file=[]              指定環(huán)境變量文件,文件格式為每行一個環(huán)境變量  
  --expose=[]                指定容器暴露的端口,即修改鏡像的暴露端口  
  -h, --hostname=""          指定容器的主機(jī)名  
  -i, --interactive=false    打開STDIN,用于控制臺交互  
  --link=[]                  指定容器間的關(guān)聯(lián),使用其他容器的IP、env等信息  
  --lxc-conf=[]              指定容器的配置文件,只有在指定--exec-driver=lxc時使用  
  -m, --memory=""            指定容器的內(nèi)存上限  
  --name=""                  指定容器名字,后續(xù)可以通過名字進(jìn)行容器管理,links特性需要使用名字  
  --net="bridge"             容器網(wǎng)絡(luò)設(shè)置,待詳述  
  -P, --publish-all=false    指定容器暴露的端口,待詳述  
  -p, --publish=[]           指定容器暴露的端口,待詳述  
  --privileged=false         指定容器是否為特權(quán)容器,特權(quán)容器擁有所有的capabilities  
  --restart=""               指定容器停止后的重啟策略,待詳述  
  --rm=false                 指定容器停止后自動刪除容器(不支持以docker run -d啟動的容器)  
  --sig-proxy=true           設(shè)置由代理接受并處理信號,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理  
  -t, --tty=false            分配tty設(shè)備,該可以支持終端登錄  
  -u, --user=""              指定容器的用戶  
  -v, --volume=[]            給容器掛載存儲卷,掛載到容器的某個目錄  
  --volumes-from=[]          給容器掛載其他容器上的卷,掛載到容器的某個目錄  
  -w, --workdir=""           指定容器的工作目錄  

1、創(chuàng)建并運行容器

# docker run 鏡像命名
docker run centos
# 指定容器名
docker run --name webcentos  centos
# 以守護(hù)進(jìn)程的方式運行容器
# docker run 鏡像命名
docker run centos
# 指定容器名
docker run --name webcentos  centos
# 以守護(hù)進(jìn)程的方式運行容器
docker run --name webcentos -d centos

2、容器列表

docker ps

3、容器狀態(tài)

docker ps -a

4、停止容器

# docker stop 容器名稱/容器Id
docker stop webcentos

5、啟動容器

docker start webcentos

6、刪除容器

# docker rm 容器id
docker rm 235483s15c

7、刪除所有容器

docker rm $(docker ps -a -q)

8、容器日志

# docker logs 容器名稱/容器id
docker logs webcentos

9、登陸容器

# docker exec -it 容器名稱/容器id  /bin/bash
docker exec -it webcentos /bin/bash

其中, -t 選項讓Docker分配一個偽終端( pseudo-tty)并綁定到容器的標(biāo)準(zhǔn)輸入上, -i 則讓容器的標(biāo)準(zhǔn)輸入保持打開。

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容