`

docker基本使用

 
阅读更多

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 ~]$ 

 

 

分享到:
评论

相关推荐

    docker基本使用方法

    docker基本使用方法 docker基本使用方法 docker基本使用方法

    docker基本使用方法.docx

    docker基本使用方法,docker基本使用方法文档,docker使用方法

    docker基本使用手册

    docker使用说手册,包含docker的基本使用,镜像的导入导出,容器的导入导出和一些命令操作技巧。

    Docker基本命令.docx

    Docker基本命令 将tocker加到用户组 sudo groupadd docker //将docker加到用户组 sudo gpasswd -a rocky docker //将rockyyoghurt添加到docker sudo service docker restart //重启docker服务 镜像操作 docker ...

    01.Docker基本概念和框架

    01.Docker基本概念和框架01.Docker基本概念和框架01.Docker基本概念和框架

    实验3-Docker基本操作.docx

    Docker 是一个开源的应用容器引擎,基于 Go ...Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。Docker 容器通过 Docker 镜像来创建。容器与镜像的关系类似于面向对象编程中的对象与类。

    Docker 的基本使用

    Docker 的基本使用

    docker基本管理.docx

    docker基本管理.docx

    docker文档使用指南

    Docker容器是资源分割和调度的基本单位,封装整个服务的运行时环境,用于构建、发布和运行分布式应用的一个框架。它是一个跨平台、可移植并且简单易用的容器解决方案。Docker的源代码托管在GitHub上,基于Go语言开发...

    docker基本命令.docx

    docker 基础命令,还不是很完善继续整理中 ,对于初入门的人还是有些帮助的,1、docker基础命令 启动一个容器 Docker run -i-t ubuntu /bin/bash -i表示docker守护进程为客户端打开标准输入,而 -t:表示 打开一伪...

    docker基本操作与命令

    docker基本操作与命令,

    docker安装与使用基本命令

    docker安装与使用基本命令

    docker基本讲解PPT

    新人学习docker可以参考

    Docker的使用.docx

    Docker的安装及其基本命令的使用,Docker Registry的Hub镜像服务管理,创建,拉取 阿里云Docker registry

    docker的基本命令及使用docker安装mysql.doc

    一 docker基本命令 docker search xxx --搜索镜像 docker pull xxx --拉取镜像 docker images --查看当前安装的镜像 docker rmi image-id --删除指定镜像,image-id表示docker images查询出的镜像的id

    docker 基本常用操作

    本文档主要针对与刚接触docker的童鞋,总结了docker的一些基本的常用的操作,能使你尽快的熟悉docker

    详解如何使用Docker部署一个web项目并打包成镜像文件

    docker基本入门以后,可以试试打包docker镜像与dockerfile了 docker镜像 docker hub仓库有2类仓库,用户仓库和顶层仓库,用户仓库由docker用户创建的,顶层仓库由docker内部的人来管理的。仓库里存放的是镜像文件,...

    Docker基础入门到实战视频教程.rar

    01.Docker基本概念和框架 01.Docker -课程介绍 02.Docker -Docker 简介 03.Docker -Docker 的基本组成 04.Docker -Docker 容器相关技术简 02.Docker的安装和部署 01.Docker -Docker安装和部署课程简介 02.Ubuntu ...

    docker基本概念.doc

    docker基本概念

    docker+vue实战项目,docker基本操作

    docker+vue实战项目,docker基本操作

Global site tag (gtag.js) - Google Analytics