设为首页 收藏本站
查看: 1074|回复: 0

[经验分享] docker基本使用

[复制链接]

尚未签到

发表于 2016-1-11 12:07:26 | 显示全部楼层 |阅读模式
docker基本使用
  本文中使用命令没有加sudo,因为在此之前已经将我的用户添加到了docker的用户组中。
  

[linus_dev@localhost ~]$ sudo usermod -aG docker linus_dev

  
  

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

[linus_dev@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
--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命令运行了一个镜像。
  
  

[linus_dev@localhost ~]$ 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/
[linus_dev@localhost ~]$

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

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

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

[linus_dev@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
--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
[linus_dev@localhost ~]$

  以下是在我的机器上运行的结果:
  
  

[linus_dev@localhost ~]$ 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命令可以删除一个指定的容器或所有的容器。
  

[linus_dev@localhost ~]$ docker rm --help
Usage:docker rm [OPTIONS] CONTAINER [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
[linus_dev@localhost ~]$

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

[linus_dev@localhost ~]$ docker rm f4f346f71dce
f4f346f71dce
[linus_dev@localhost ~]$ 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
[linus_dev@localhost ~]$

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

[linus_dev@localhost ~]$ docker rm $(docker ps -a -q)
9cee93bcb48e
00d4845c4972
[linus_dev@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[linus_dev@localhost ~]$

  
  

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

[linus_dev@localhost ~]$ docker images --help
Usage:docker images [OPTIONS] [REPOSITORY[:TAG]]
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
[linus_dev@localhost ~]$

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

[linus_dev@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              975b84d108f1        8 weeks ago         960 B

  
  

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

[linus_dev@localhost ~]$ docker search --help
Usage:docker search [OPTIONS] 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
[linus_dev@localhost ~]$

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

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

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

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

  
  

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

[linus_dev@localhost ~]$ docker logs --help
Usage:docker logs [OPTIONS] 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的容器的日志:
  
  

[linus_dev@localhost ~]$ 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/
[linus_dev@localhost ~]$

  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-163033-1-1.html 上篇帖子: 阿里云Docker镜像库 下篇帖子: 在 Docker 上配置 Oracle
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表