cyrus 发表于 2016-1-11 12:07:26

docker基本使用

docker基本使用
  本文中使用命令没有加sudo,因为在此之前已经将我的用户添加到了docker的用户组中。
  

$ sudo usermod -aG docker linus_dev

  
  

启动一个docker镜像
  使用docker run命令可以启动一个镜像并运行一条命令。
  

$ docker run --help
Usage:docker run IMAGE
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
--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 CPU CFS (Completely Fair Scheduler) 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
--disable-content-trust=true    Skip image verification
--dns=[]                        Set custom DNS servers
--dns-opt=[]                  Set DNS options
--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
--group-add=[]                  Add additional groups to join
-h, --hostname=               Container host name
--help=false                  Print usage
-i, --interactive=false         Keep STDIN open even if not attached
--ipc=                        IPC namespace to use
--kernel-memory=                Kernel memory limit
-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-reservation=         Memory soft limit
--memory-swap=                  Total memory (memory + swap), '-1' to disable swap
--memory-swappiness=-1          Tuning container memory swappiness (0 to 100)
--name=                         Assign a name to the container
--net=default                   Set the Network 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
--stop-signal=SIGTERM         Signal to stop a container, SIGTERM by default
-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
--volume-driver=                Optional volume driver for the container
--volumes-from=[]               Mount volumes from the specified container(s)
-w, --workdir=                  Working directory inside the container
  下面这就是使用docker run命令运行了一个镜像。
  
  

$ docker run hello-world
Hello from Docker.
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/userguide/
$

  
  如果想要后台运行并暴露端口可以按如下方式使用:

docker run -d -p 127.0.0.1:33301:22 centos6-ssh
  

查看当前的容器
  使用docker ps命令可以查看当前的容器运行情况。
  

$ docker ps --help
Usage:docker ps
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
--format=             Pretty-print containers using a Go template
--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 ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED            STATUS                        PORTS               NAMES
9cee93bcb48e      hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                     backstabbing_darwin
00d4845c4972      hello-world         "/hello"            23 minutes ago       Exited (0) 23 minutes ago                           amazing_bose
f4f346f71dce      hello-world         "/hello"            15 hours ago         Exited (0) 15 hours ago                           romantic_panini

  
  

删除一个容器
  使用docker rm命令可以删除一个指定的容器或所有的容器。
  

$ docker rm --help
Usage:docker rm CONTAINER
Remove one or more containers
-f, --force=false      Force the removal of a running container (uses SIGKILL)
--help=false         Print usage
-l, --link=false       Remove the specified link
-v, --volumes=false    Remove the volumes associated with the container
$

  例如,在我的环境上,我要删除容器实例ID为f4f346f71dce的容器实例,可以按如下操作,可以看到执行完后再用docker ps命令可以看到那个容器实例已经没有了。
  
  

$ docker rm f4f346f71dce
f4f346f71dce
$ docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
9cee93bcb48e      hello-world         "/hello"            6 minutes ago       Exited (0) 6 minutes ago                        backstabbing_darwin
00d4845c4972      hello-world         "/hello"            27 minutes ago      Exited (0) 27 minutes ago                     amazing_bose
$

  
  如果要删除所有的容器,可以像下面这样操作,可以看到执行命令后所有的容器都没有了:
  

$ docker rm $(docker ps -a -q)
9cee93bcb48e
00d4845c4972
$ docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
$

  
  

查看镜像
  使用docker images命令可以查看当前环境上已有的镜像文件。
  

$ docker images --help
Usage:docker images ]
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
$

  在我的环境上因为只运行过hello-world这个镜像,因此使用docker images命令只看到了一个镜像:
  
  

$ docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest            975b84d108f1      8 weeks ago         960 B

  
  

查找镜像
  使用docker search命令可以查找在Docker HUB上已有的镜像。
  

$ docker search --help
Usage:docker search TERM
Search the Docker Hub for images
--automated=false    Only show automated builds
--help=false         Print usage
--no-trunc=false   Don't truncate output
-s, --stars=0      Only displays with at least x stars
$

  在我的环境上我搜索CentOS的镜像有哪些,其中的镜像名字就是你在docker run命令里需要使用的镜像名,STARS表示这个镜像的星级或受欢迎程度,OFFICIAL表示这个镜像是否是官方维护的。
  
  

$ docker search CentOS
NAME                              DESCRIPTION                                     STARS   OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   1735            
ansible/centos7-ansible         Ansible on Centos7                              62                  
jdeathe/centos-ssh                CentOS-6 6.7 x86_64 / EPEL/IUS Repos / Ope...   14                  
million12/centos-supervisor       Base CentOS-7 with supervisord launcher, h...   9                  
blalor/centos                     Bare-bones base CentOS 6.5 image                8                  
nimmis/java-centos                This is docker images of CentOS 7 with dif...   7                  
torusware/speedus-centos          Always updated official CentOS docker imag...   7                  
consol/centos-xfce-vnc            Centos container with "headless" VNC sessi...   3                  
nathonfowlie/centos-jre         Latest CentOS image with the JRE pre-insta...   3                  
jdeathe/centos-ssh-mysql          CentOS-6 6.7 x86_64 / MySQL.                  3                  
tcnksm/centos-node                Dockerfile for CentOS packaging node            2                  
nickistre/centos-lamp             LAMP on centos setup                            2                  
consol/sakuli-centos-xfce         Sakuli end-2-end testing and monitoring co...   2                  
yajo/centos-epel                  CentOS with EPEL and fully updated            1                  
layerworx/centos                  CentOS container with etcd, etcdctl, confd...   1                  
nickistre/centos-lamp-wordpress   LAMP on CentOS setups with wp-cli installed   1                  
nathonfowlie/centos-jira          JIRA running on the latest version of CentOS    1                  
lighthopper/orientdb-centos       A Dockerfile for creating an OrientDB imag...   1                  
blacklabelops/centos            Blacklabelops Centos 7.1.503 base image wi...   0                  
jasonish/centos-suricata          Suricata base image based on CentOS 7.          0                  
dmglab/centos                     CentOS with superpowers!                        0                  
timhughes/centos                  Centos with systemd installed and running       0                  
lighthopper/openjdk-centos      A Dockerfile for creating an OpenJDK image...   0                  
pdericson/centos                  Docker image for CentOS                         0                  
jsmigel/centos-epel               Docker base image of CentOS w/ EPEL installed   0                  
$

  下面是在我的环境上搜索hello-world的镜像,并且不截断打印信息,只搜索至少10星的镜像:
  
  

$ docker search --no-trunc -s=10 hello-world
NAME                DESCRIPTION                                                                                    STARS   OFFICIAL   AUTOMATED
hello-world         Hello World! (an example of minimal Dockerization)                                             33            
tutum/hello-world   Image to test docker deployments. Has Apache with a 'Hello World' page listening in port 80.   19                  
$

  
  

查看容器日志
  可以使用docker logs命令查看容器日志:
  

$ docker logs --help
Usage:docker logs CONTAINER
Fetch the logs of a container
-f, --follow=false      Follow log output
--help=false            Print usage
--since=                  Show logs since timestamp
-t, --timestamps=false    Show timestamps
--tail=all                Number of lines to show from the end of the logs
  在我的环境上查看容器ID为9cee93bcb48e的容器的日志:
  
  

$ docker logs -f 9cee93bcb48e
Hello from Docker.
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/userguide/
$

  
  
页: [1]
查看完整版本: docker基本使用