关于Docker的快速教程

235 阅读7分钟

Docker是一个平台即服务(PaaS)产品的集合,它使用操作系统级的虚拟化来提供被称为容器的软件包。容器之间是完全隔离的,是一个标准的软件单元,它将代码和所有的依赖关系打包,因此应用程序可以快速可靠地从一个计算环境运行到另一个。这使得容器成为重现开发环境以及在生产中服务于可重现的应用程序的一个伟大工具。在这个快速教程中,我们将开始使用Docker。


为你的操作系统安装Docker

Download Docker


启动Docker并开始教程

安装完成后,你会得到完成教程的选项,这就是你应该做的。

0 Get started with docker easy steps


克隆一个存储库

为了启动一个容器,我们首先需要从Github克隆一个仓库。

git clone https://github.com/docker/getting-started

1 docker clone a repository


构建镜像

Docker镜像是一个专门为你的容器设计的私有文件系统。它提供了你的容器需要的所有文件和软件。

cd getting-started
docker build -t docker101tutorial

2 docker build image after clone repo


运行一个容器

根据你在上一步建立的镜像,启动一个容器。运行一个容器可以用私有资源启动你的应用程序,并与机器的其他部分安全地隔离。

docker run -d -p 80:80 --name docker-tutorial docker101tutorial

3 docker run your first container


(可选)在Docker Hub上注册

Docker hub是一个很好的资源,可以让你找到世界上任何可以在容器中运行的软件。

4 docker save image to docker hub optional


在仪表板上查看容器

在Docker仪表盘中,你会看到容器在运行,并有一些选项。我们可以点击在浏览器中查看选项来查看我们的劳动成果。

5 docker container image now running

你的网络浏览器应该自动启动并导航到http://localhost/tutorial,这是你刚刚用Docker在本地机器上启动的运行中的应用程序

docker tutorial application running

新启动的应用程序中包含的教程是非常好的。它涵盖了你需要知道的一切,并可能回答你可能有的很多问题,特别是如果你是来自虚拟机工作流程的背景。


Docker快速参考

恭喜你,你的第一个容器已经启动并运行了。以下是快速使用现有Docker命令的一个很好的参考。

docker build

从当前目录下的[Docker文件]中构建一个镜像,并对该镜像进行标记
docker build -t myimage:1.0 。

列出本地存储在Docker引擎中的所有镜像
docker image ls

从本地镜像存储中删除一个镜像
docker image rm alpine:3.4

docker run
从Alpine 3.9版本的镜像中运行一个容器,将运行中的容器命名为 "web",并在外部暴露5000端口,在容器内映射到80端口。
docker container run -nameweb -p 5000:80 alpine:3.9

通过SIGTERM停止一个正在运行的容器
docker container stop web

通过SIGKILL停止运行中的容器
docker container kill web

列出网络
docker network ls

列出运行中的容器(添加-all以包括停止的容器)
docker container ls -all

删除所有运行和停止的容器
docker container rm -f $(docker ps -aq)

打印一个容器的最后100行日志
docker container logs -tail 100 web

docker share
从注册表中提取一个镜像
docker pull myimage:1.0

用新的镜像名称和标签重新标记一个本地镜像
docker tag myimage:1.0 myrepo/myimage:2.0

推送一个镜像到注册表
docker push myrepo/myimage:2.0


Docker管理

docker 

  builder     Manage builds
    build       Build an image from a Dockerfile
    prune       Remove build cache

  config      Manage Docker configs
    create      Create a config from a file or STDIN
    inspect     Display detailed information on one or more configs
    ls          List configs
    rm          Remove one or more configs

  container   Manage containers
    attach      Attach local standard input, output, and error streams to a running container
    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
    exec        Run a command in a running container
    export      Export a container's filesystem as a tar archive
    inspect     Display detailed information on one or more containers
    kill        Kill one or more running containers
    logs        Fetch the logs of a container
    ls          List containers
    pause       Pause all processes within one or more containers
    port        List port mappings or a specific mapping for the container
    prune       Remove all stopped containers
    rename      Rename a container
    restart     Restart one or more containers
    rm          Remove one or more containers
    run         Run a command in a new container
    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
    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
    wait        Block until one or more containers stop, then print their exit codes

  context     Manage contexts
    create      Create new context
    inspect     Display detailed information on one or more contexts
    list        List available contexts
    rm          Remove one or more contexts
    show        Print the current context
    use         Set the default context

  image       Manage images
    build       Build an image from a Dockerfile
    history     Show the history of an image
    import      Import the contents from a tarball to create a filesystem image
    inspect     Display detailed information on one or more images
    load        Load an image from a tar archive or STDIN
    ls          List images
    prune       Remove unused images
    pull        Pull an image or a repository from a registry
    push        Push an image or a repository to a registry
    rm          Remove one or more images
    save        Save one or more images to a tar archive (streamed to STDOUT by default)
    tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

  network     Manage networks
    connect     Connect a container to a network
    create      Create a network
    disconnect  Disconnect a container from a network
    inspect     Display detailed information on one or more networks
    ls          List networks
    prune       Remove all unused networks
    rm          Remove one or more networks

  node        Manage Swarm nodes
    demote      Demote one or more nodes from manager in the swarm
    inspect     Display detailed information on one or more nodes
    ls          List nodes in the swarm
    promote     Promote one or more nodes to manager in the swarm
    ps          List tasks running on one or more nodes, defaults to current node
    rm          Remove one or more nodes from the swarm
    update      Update a node

  plugin      Manage plugins
    create      Create a plugin from a rootfs and configuration. Plugin data directory 
                must contain config.json and rootfs directory.
    disable     Disable a plugin
    enable      Enable a plugin
    inspect     Display detailed information on one or more plugins
    install     Install a plugin
    ls          List plugins
    push        Push a plugin to a registry
    rm          Remove one or more plugins
    set         Change settings for a plugin
    upgrade     Upgrade an existing plugin

  secret      Manage Docker secrets
    create      Create a secret from a file or STDIN as content
    inspect     Display detailed information on one or more secrets
    ls          List secrets
    rm          Remove one or more secrets

  service     Manage services
    create      Create a new service
    inspect     Display detailed information on one or more services
    logs        Fetch the logs of a service or task
    ls          List services
    ps          List the tasks of one or more services
    rm          Remove one or more services
    rollback    Revert changes to a service's configuration
    scale       Scale one or multiple replicated services
    update      Update a service

  stack       Manage Docker stacks
    deploy      Deploy a new stack or update an existing stack
    ls          List stacks
    ps          List the tasks in the stack
    rm          Remove one or more stacks
    services    List the services in the stack

  swarm       Manage Swarm
    ca          Display and rotate the root CA
    init        Initialize a swarm
    join        Join a swarm as a node and/or manager
    join-token  Manage join tokens
    leave       Leave the swarm
    unlock      Unlock swarm
    unlock-key  Manage the unlock key
    update      Update the swarm

  system      Manage Docker
    df          Show docker disk usage
    events      Get real time events from the server
    info        Display system-wide information
    prune       Remove unused data

  trust       Manage trust on Docker images
    key         Manage keys for signing Docker images
      generate    Generate and load a signing key-pair
      load        Load a private key file for signing

    signer      Manage entities who can sign Docker images
      add         Add a signer
      remove      Remove a signer

  volume      Manage volumes
    create      Create a volume
    inspect     Display detailed information on one or more volumes
    ls          List volumes
    prune       Remove all unused local volumes
    rm          Remove one or more 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

Docker快速教程总结

这应该让你对Docker的运作方式有一个基本的了解,掌握了Docker的关键概念。在其最基本的层面上,Docker只是简单的图像和容器。