欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

docker操作命令大全和后台参数

程序员文章站 2022-06-29 23:22:19
一、命令行 可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 CentOS 为操作环境为例): [root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker help Usage: docker [OPTIONS] COMMA... ......

一、命令行

可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 centos 为操作环境为例):

[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker help

usage:    docker [options] command

a self-sufficient runtime for containers

options:
      --config string      location of client config files (default "/root/.docker")
  -c, --context string     name of the context to use to connect to the daemon (overrides docker_host env var and default context set with "docker context use")
  -d, --debug              enable debug mode
  -h, --host list          daemon socket(s) to connect to
  -l, --log-level string   set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                use tls; implied by --tlsverify
      --tlscacert string   trust certs signed only by this ca (default "/root/.docker/ca.pem")
      --tlscert string     path to tls certificate file (default "/root/.docker/cert.pem")
      --tlskey string      path to tls key file (default "/root/.docker/key.pem")
      --tlsverify          use tls and verify the remote
  -v, --version            print version information and quit

management commands:
  builder     manage builds
  config      manage docker configs
  container   manage containers
  context     manage contexts
  engine      manage the docker engine
  image       manage images
  network     manage networks
  node        manage swarm nodes
  plugin      manage plugins
  secret      manage docker secrets
  service     manage services
  stack       manage docker stacks
  swarm       manage swarm
  system      manage docker
  trust       manage trust on docker images
  volume      manage volumes

commands:
  attach      attach local standard input, output, and error streams 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 to files or directories 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 docker objects
  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
  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 one or more containers
  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
  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
  tag         create a tag target_image that refers to source_image
  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
  wait        block until one or more containers stop, then print their exit codes

run 'docker command --help' for more information on a command.

其中:

info、version是系统环境信息相关的命令

events、history、logs 是日志信息相关的命令

login\pull\push\search是dockerhub服务相关的命令

其余都是日常维护相关的命令

二、命令语法

基本用法:命令 + 参数

[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker info
client:
 debug mode: false

server:
 containers: 4
  running: 0
  paused: 0
  stopped: 4
 images: 2
 server version: 19.03.1
...............(省略)
 plugins:
  volume: local
  network: bridge host ipvlan macvlan null overlay
  log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog

 security options:

 kernel version: 3.10.0-693.2.2.el7.x86_64
 operating system: centos linux 7 (core)
 ostype: linux
 architecture: x86_64
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker version
client: docker engine - community
 version:           19.03.1
 api version:       1.40
 go version:        go1.12.5
 git commit:        74b1e89e8a
 built:             thu jul 25 21:17:37 2019
 os/arch:           linux/amd64
 experimental:      false

server: docker engine - community
 engine:
  version:          19.03.1
  api version:      1.40 (minimum version 1.12)
  go version:       go1.12.5
  git commit:       74b1e89e8a
  built:            thu jul 25 21:27:55 2019
  os/arch:          linux/amd64
  experimental:     false
 containerd:
  version:          v1.2.6
  gitcommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  version:          1.0.0-rc8
  gitcommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  version:          0.18.0
  gitcommit:        fec3683

查看已下载的镜像

[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker images
repository          tag                 image id            created             size
ubuntu              latest              3556258649b2        2 weeks ago         64.2mb
hello-world         latest              fce289e99eb9        7 months ago        1.84kb
learn/tutorial      latest              a7876479f1aa        6 years ago         128mb

 单个字符的参数可以放在一起:

[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker run -t -i ubuntu bash # 使用镜像 ubuntu:latest 以交互模式启动一个容器,在容器内执行 bash 命令,标红部分表示使用分开的参数
unable to find image 'ubuntu:latest' locally
latest: pulling from library/ubuntu
7413c47ba209: pull complete 
0fe7e7cbb2e8: pull complete 
1d425c982345: pull complete 
344da5c95cec: pull complete 
digest: sha256:c303f19cfe9ee92badbbbd7567bc1ca47789f79303ddcef56f77687d4744cd7a
status: downloaded newer image for ubuntu:latest

root@a0629a1e86e4:/# cat /etc/issue
ubuntu 18.04.2 lts \n \l

root@a0629a1e86e4:/# exit
exit
[root@iz2ze2bn5x2wqxdeq65wlpz ~]#
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker run -ti ubuntu bash # 标红部分,表示把单个字符参数,合在一起使用
root@5b84ee078161:/# exit
exit
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker rm imgid-1 imgid-2 # 标红部分,表示把多个参数放在一行使用
error: no such container: imgid-1
error: no such container: imgid-2

二、docker 后台进程参数

参数 介绍
--api-enable-cors=false 远程api调用。
-b, --bridge="" 桥接一个系统上的网桥设备到 docker 容器里,当使用 none 可以停用容器里的网络
--bip="" 使用 cidr 地址来设定网络桥的 ip。此参数和 -b 不能一起使用。
-d, --debug=false 开启debug模式。例如:docker -d -d
-d, --daemon=false 开启daemon模式。
--dns=[] 设置容器使用dns服务器。例如: docker -d --dns 8.8.8.8
-dns-search=[] 设置容器使用指定的dns搜索域名。如: docker -d --dns-search example.com
--exec-driver="native" 设置容器使用指定的运行时驱动。如:docker -d -e lxc
-g, --group="docker" 在后台运行模式下,赋予指定的group到相应的unix socket上。注意,当此参数 --group 赋予空字符串时,将去除组信息
-g, --graph="/var/lib/docker" 设置docker运行时根目录
-h, --host=[] 设置后台模式下指定socket绑定,可以绑定一个或多个 tcp://host:port, unix:///path/to/socket, fd://* 或 fd://socketfd。如:$ docker -h tcp://0.0.0.0:2375 ps 或者$ export docker_host="tcp://0.0.0.0:2375"$ docker ps
-icc=true 设置启用内联容器的通信。
--ip="0.0.0.0" 设置容器绑定ip时使用的默认ip地址
--ip-forward=true 设置启动容器的 net.ipv4.ip_forward
--iptables=true 设置启动docker容器自定义的iptable规则
--mtu=0 设置容器网络的mtu值,如果没有这个参数,选用默认 route mtu,如果没有默认route,就设置成常量值 1500。
-p, --pidfile="/var/run/docker.pid" 设置后台进程pid文件路径。
-r, --restart=true 设置重启之前运行中的容器
-s, --storage-driver="" 设置容器运行时使用指定的存储驱动,如,指定使用devicemapper,可以这样:docker -d -s devicemapper
--selinux-enabled=false 设置启用selinux支持
--storage-opt=[] 设置存储驱动的参数

三、docker 命令大全

容器生命周期管理
    run
    start/stop/restart
    kill
    rm
    pause/unpause
    create
    exec
容器操作
    ps
    inspect
    top
    attach
    events
    logs
    wait
    export
    port
容器rootfs命令
    commit
    cp
    diff
镜像仓库
    login
    pull
    push
    search
本地镜像管理
    images
    rmi
    tag
    build
    history
    save
    load
    import
docker信息|版本
    info
    version

 


 

1、docker run :创建一个新的容器并运行一个命令

 语法:docker run [options] image [command] [arg...]

options说明:

-a stdin: 指定标准输入输出内容类型,可选 stdin/stdout/stderr 三项;

-d: 后台运行容器,并返回容器id;

-i: 以交互模式运行容器,通常与 -t 同时使用;

-p: 随机端口映射,容器内部端口随机映射到主机的高端口

-p: 指定端口映射,格式为:主机(宿主)端口:容器端口

-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

--name="nginx-lb": 为容器指定一个名称;

--dns 8.8.8.8: 指定容器使用的dns服务器,默认和宿主一致;

--dns-search example.com: 指定容器dns搜索域名,默认和宿主一致;

-h "mars": 指定容器的hostname;

-e username="ritchie": 设置环境变量;

--env-file=[]: 从指定文件读入环境变量;

--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定cpu运行;

-m :设置容器使用内存最大值;

--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;

--link=[]: 添加链接到另一个容器;

--expose=[]: 开放一个端口或一组端口;

--volume , -v:    绑定一个卷

实例:

 使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx。

docker run --name mynginx -d nginx:latest

使用镜像nginx:latest以后台模式启动一个容器,并将容器的80端口映射到主机随机端口。

docker run -p -d nginx:latest

使用镜像 nginx:latest,以后台模式启动一个容器,将容器的 80 端口映射到主机的 80 端口,主机的目录 /data 映射到容器的 /data。

docker run -p 80:80 -v /data:/data -d nginx:latest

绑定容器的 8080 端口,并将其映射到本地主机 127.0.0.1 的 80 端口上。

docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

使用镜像nginx:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。

 

docker run -it nginx:latest /bin/bash