docker 常用命令

[toc]

常用命令:

其中<>阔起来的参数为必选,[]阔起来为可选

  • docker version 查看docker的版本号,包括客户端、服务端、依赖的Go等
  • docker info 查看系统(docker)层面信息,包括管理的images, containers数等
  • docker search 在docker index中搜索image
  • docker pull 从docker registry server 中下拉image
  • docker push 推送一个image或repository到registry
  • docker push :TAG 同上,指定tag
  • docker inspect 查看image或container的底层信息
  • docker images TODO filter out the intermediate image layers (intermediate image layers 是什么)
  • docker images -a 列出所有的images
  • docker ps 默认显示正在运行中的container
  • docker ps -l 显示最后一次创建的container,包括未运行的
  • docker ps -a 显示所有的container,包括未运行的
  • docker logs 查看container的日志,也就是执行命令的一些输出
  • docker rm 删除一个或多个container
  • docker rm docker ps -a -q 删除所有的container
  • docker ps -a -q | xargs docker rm 同上, 删除所有的container
  • docker rmi 删除一个或多个image
  • docker start/stop/restart 开启/停止/重启container
  • docker start -i 启动一个container并进入交互模式
  • docker attach attach一个运行中的container
  • docker run 使用image创建container并执行相应命令,然后停止
  • docker run -i -t /bin/bash 使用image创建container并进入交互模式, login shell是/bin/bash
  • docker run -i -t -p 将container的端口映射到宿主机的端口
  • docker commit [repo:tag] 将一个container固化为一个新的image,后面的repo:tag可选
  • docker build 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image
  • docker build -t repo[:tag] 同上,可以指定repo和可选的tag
  • docker build - < 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image
  • docker port 查看本地哪个端口映射到container的指定端口,其实用docker ps 也可以看到

docker search 查找images

[root@localhost ~]# docker search centos
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.                   2793      [OK]       
jdeathe/centos-ssh              CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8...   44                   [OK]
jdeathe/centos-ssh-apache-php   CentOS-6 6.8 x86_64 - Apache / PHP / PHP M...   22                   [OK]
nimmis/java-centos              This is docker images of CentOS 7 with dif...   18                   [OK]
consol/centos-xfce-vnc          Centos container with "headless" VNC sessi...   14                   [OK]
gluster/gluster-centos          Official GlusterFS Image [ CentOS7 +  Glus...   12                   [OK]
million12/centos-supervisor     Base CentOS-7 with supervisord launcher, h...   12                   [OK]
torusware/speedus-centos        Always updated official CentOS docker imag...   8                    [OK]
nathonfowlie/centos-jre         Latest CentOS image with the JRE pre-insta...   4                    [OK]
centos/mariadb55-centos7                                                        3                    [OK]
harisekhon/centos-java          Java on CentOS (OpenJDK, tags jre/jdk7-8)       2                    [OK]
blacklabelops/centos            CentOS Base Image! Built and Updates Daily!     1                    [OK]
timhughes/centos                Centos with systemd installed and running       1                    [OK]
darksheer/centos                Base Centos Image -- Updated hourly             1                    [OK]
harisekhon/centos-scala         Scala + CentOS (OpenJDK tags 2.10-jre7 - 2...   1                    [OK]
sgfinans/docker-centos          CentOS with a running sshd and Docker           1                    [OK]
ustclug/centos                   USTC centos                                    0                    [OK]
januswel/centos                 yum update-ed CentOS image                      0                    [OK]
repositoryjp/centos             Docker Image for CentOS.                        0                    [OK]
kz8s/centos                     Official CentOS plus epel-release               0                    [OK]
grossws/centos                  CentOS 6 and 7 base images with gosu and l...   0                    [OK]
dmglab/centos                   CentOS with some extras - This is for the ...   0                    [OK]
aguamala/centos                 CentOS base image                               0                    [OK]
vcatechnology/centos            A CentOS Image which is updated daily           0                    [OK]
grayzone/centos                 auto build for centos.                          0                    [OK]

docker pull 拉去镜像

[root@localhost ~]# docker pull centos
latest: Pulling from centos
af0819ed1fac: Downloading [=====================================>             ] 53.51 MB/70.48 MB
05fe84bf6d3f: Download complete 
97cad5e16cb6: Download complete

docker run 创建container

[root@localhost ~]# docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

  -a, --attach=[]             Attach to STDIN, STDOUT or STDERR
  --add-host=[]               Add a custom host-to-IP mapping (host:ip)
  --blkio-weight=0            Block IO (relative weight), between 10 and 1000
  -c, --cpu-shares=0          CPU shares (relative weight)
  --cap-add=[]                Add Linux capabilities
  --cap-drop=[]               Drop Linux capabilities
  --cgroup-parent=            Optional parent cgroup for the container
  --cidfile=                  Write the container ID to the file
  --cpu-period=0              Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota=0               Limit the CPU CFS quota
  --cpuset-cpus=              CPUs in which to allow execution (0-3, 0,1)
  --cpuset-mems=              MEMs in which to allow execution (0-3, 0,1)
  -d, --detach=false          Run container in background and print container ID
  --device=[]                 Add a host device to the container
  --dns=[]                    Set custom DNS servers
  --dns-search=[]             Set custom DNS search domains
  -e, --env=[]                Set environment variables
  --entrypoint=               Overwrite the default ENTRYPOINT of the image
  --env-file=[]               Read in a file of environment variables
  --expose=[]                 Expose a port or a range of ports
  -h, --hostname=             Container host name
  --help=false                Print usage
  -i, --interactive=false     Keep STDIN open even if not attached
  --ipc=                      IPC namespace to use
  -l, --label=[]              Set meta data on a container
  --label-file=[]             Read in a line delimited file of labels
  --link=[]                   Add link to another container
  --log-driver=               Logging driver for container
  --log-opt=[]                Log driver options
  --lxc-conf=[]               Add custom lxc options
  -m, --memory=               Memory limit
  --mac-address=              Container MAC address (e.g. 92:d0:c6:0a:29:33)
  --memory-swap=              Total memory (memory + swap), '-1' to disable swap
  --name=                     Assign a name to the container
  --net=bridge                Set the Network mode for the container
  --oom-kill-disable=false    Disable OOM Killer
  -P, --publish-all=false     Publish all exposed ports to random ports
  -p, --publish=[]            Publish a container's port(s) to the host
  --pid=                      PID namespace to use
  --privileged=false          Give extended privileges to this container
  --read-only=false           Mount the container's root filesystem as read only
  --restart=no                Restart policy to apply when a container exits
  --rm=false                  Automatically remove the container when it exits
  --security-opt=[]           Security Options
  --sig-proxy=true            Proxy received signals to the process
  -t, --tty=false             Allocate a pseudo-TTY
  -u, --user=                 Username or UID (format: <name|uid>[:<group|gid>])
  --ulimit=[]                 Ulimit options
  --uts=                      UTS namespace to use
  -v, --volume=[]             Bind mount a volume
  --volumes-from=[]           Mount volumes from the specified container(s)
  -w, --workdir=              Working directory inside the container

docker images 查看 images

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mytomcat            1.0                 9bd4bfe35735        24 hours ago        1.807 GB
centos              7.2.1511            bcdaf234d72f        9 weeks ago         194.6 MB

使用image创建container

[root@localhost ~]# docker images --help

Usage: docker images [OPTIONS] [REPOSITORY]

List images

  -a, --all=false      Show all images (default hides intermediate images)
  --digests=false      Show digests
  -f, --filter=[]      Filter output based on conditions provided
  --help=false         Print usage
  --no-trunc=false     Don't truncate output
  -q, --quiet=false    Only show numeric IDs
[root@localhost www]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mytomcat            1.0                 9bd4bfe35735        21 hours ago        1.807 GB
centos              7.2.1511            bcdaf234d72f        9 weeks ago         194.6 MB
[root@localhost www]# docker run -i -t bcdaf234d72f /bin/bash
[root@1abd83b5a7df /]# cat /proc/version 
Linux version 2.6.32-642.6.1.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Wed Oct 5 00:36:12 UTC 2016

使用repository创建container

TAG默认为lastest,也可以指定

[root@localhost www]# docker run -i -t centos:7.2.1511
[root@15ff2d0f9591 /]# uanme -a
Linux 15ff2d0f9591 2.6.32-642.6.1.el6.x86_64 #1 SMP Wed Oct 5 00:36:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

docker ps 查看docker container

[root@localhost ~]# docker start --help

Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

  -a, --attach=false         Attach STDOUT/STDERR and forward signals
  --help=false               Print usage
  -i, --interactive=false    Attach container's STDIN
[root@localhost ~]# 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

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS                    NAMES
1abd83b5a7df        bcdaf234d72f        "/bin/bash"         14 seconds ago      Exited (0) 5 seconds ago                             clever_elion        
c61c5e58b5c7        mytomcat:1.0        "/root/run.sh"      23 hours ago        Up 2 hours                  0.0.0.0:8080->8080/tcp   mytomcat_1          
b7994eebec11        bcdaf234d72f        "/bin/bash"         2 weeks ago         Exited (130) 24 hours ago                            cocky_cori

docker commit 固化container为images

[root@localhost ~]# docker commit -a "Neo xu <[email protected]>" 1abd83b5a7df centos:test
65d08d79745e8ffe3a6fe8026cc17aa766f6cb6d9ec43ed66ee5b0be387205ee
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
centos              test                65d08d79745e        About a minute ago   194.6 MB
mytomcat            1.0                 9bd4bfe35735        24 hours ago         1.807 GB
centos              7.2.1511            bcdaf234d72f        9 weeks ago          194.6 MB

docker start 启动容器

[root@localhost ~]# docker start 1abd83b5a7df
1abd83b5a7df

docker attach 交互式进入容器

[root@localhost ~]# docker attach 1abd83b5a7df
[root@1abd83b5a7df /]#

docker rm 删除容器

[root@localhost ~]# docker rm 1abd83b5a7df
1abd83b5a7df

docker rmi 删除镜像

[root@localhost ~]# docker rmi 65d08d79745e
Untagged: centos:test
Deleted: 65d08d79745e8ffe3a6fe8026cc17aa766f6cb6d9ec43ed66ee5b0be387205ee

results matching ""

    No results matching ""