How to install and use on Debian 9 Docker

Docker is a container platform, allows you to quickly build, test and deploy applications, as a portable, self-contained container, you can run almost anywhere.

Docker container technology is the de facto standard, it is an indispensable tool for DevOps engineers and continuous integration and delivery pipeline.

In this tutorial, we will guide you through the process of installing Debian 9 Docker on a machine, and to explore the basic concepts and command Docker.

prerequisites

Before continuing with the tutorial, please ensure that the user has sudo privileges login identity. All commands in this tutorial should be run as non-root user.

Docker installed on Debian

The following steps describe the process of how to install the latest stable version from Docker repository.

  1. The installed packages to the latest version:

    sudo apt update
    sudo apt upgrade
    1. Add installation dependencies needed for the new repositories through HTTPS:
    sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg2
  2. Use the following  curl command to import GPG key repository:

    curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

    Type the following to add Docker APT repository to the list of software repositories system:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

    $(lsb_release -cs) Will return the name of the Debian distribution, in this case, it returns  stretch .

  3. Docker now enabled repository, update  apt the package list and install the latest version of Docker CE (Community Edition):

    sudo apt updatesudo apt install docker-ce
    1. After installation is complete, Docker service will start automatically. You can enter the following command to verify:
    sudo systemctl status docker
    ● docker.service - Docker Application Container Engine
    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2018-07-27 17:02:07 UTC; 1min 14s ago
        Docs: https://docs.docker.com
    Main PID: 16929 (dockerd)
    CGroup: /system.slice/docker.service
    1. As of this writing, Debian 9 of the current version of Docker Shi  18.06.0-ce . View Docker Version:
    docker -v
    Docker version 18.06.0-ce, build 0ffa825

    Docker execute commands without sudo case

    By default, only users with administrator privileges can execute Docker command.

    如果您希望以非 root 用户身份运行 Docker 命令而不 sudo 需要预先添加,则需要将用户添加到安装 Docker CE 软件包期间创建的 docker 组。您可以输入以下命令:

    sudo usermod -aG docker $USER

注销并重新登录,以便刷新组成员身份。

要验证您是否可以在不添加 sudo 前缀情况下运行 docker 命令,请运行以下命令(它将下载测试镜像),在容器中运行它,打印 “Hello from Docker” 消息并退出:

docker container run hello-world

输出应如下所示:

Docker 命令行界面

现在我们已经安装了 Docker ,让我们来看看 docker CLI 的基本语法:

docker [option] [subcommand] [arguments]

如果要列出所有可用的命令,请不带参数运行 docker :

docker

如果您需要关于 [subcommand] 的更多帮助信息,可以使用如下所示的 --help 开关:

docker [subcommand] --help

Docker 镜像

Docker 镜像由一系列文件系统层组成,这些文件系统层表现为镜像的 Dockerfile 中的指令,构成可执行软件应用程序。镜像是一个不可变的二进制文件,包括应用程序和运行应用程序所需的所有其他依赖项,如库,二进制文件和指令。

您可以将 Docker 镜像视为 Docker 容器的快照。

Docker Hub 上提供了大多数 Docker 镜像。

Docker Hub 是基于云的注册服务,用于将 Docker 镜像保存在公共或私有存储库中。

搜索 Docker 镜像

要从 Docker Hub 注册表中搜索镜像,请使用 search 子命令。

例如,要搜索 Debian 镜像,您可以键入:

docker search debian

输出应如下所示:

正如你可以看到搜索结果打印的表格有 5 列,NAMEDESCRIPTIONSTARSOFFICIALAUTOMATED

官方镜像是 Docker 与上游合作伙伴共同开发的镜像。

Docker Hub 上的大多数 Docker 镜像都标有版本号。如果未指定任何标记, Docker 将提取最新镜像。

下载 Docker Image

如果我们想要下载 Debian 镜像的官方版本,我们可以使用 image pull 子命令来完成:

docker image pull debian

根据您的 Internet 速度,下载可能需要几秒钟或几分钟。

由于我们没有指定标签,因此 docker 将提取最新的 Debian 9.5 镜像。如果你想拉取一些以前的 Debian 版本,比如说 Debian 8, 你需要使用 docker image pull debian:8

下载镜像后,我们可以通过键入以下内容列出镜像:

docker image ls

输出看起来像这样:

删除 Docker 镜像

如果由于某种原因您要删除镜像,可以使用 image rm [image_name] 子命令执行此操作:

docker image rm debian

Docker 容器

镜像的实例称为容器。容器表示单个应用程序,进程或服务的运行时。

举一个可能不是最合适的类比:如果您是程序员,您可以将 Docker 镜像视为类,将 Docker 容器视为类的一个实例。

我们可以使用 docker container 子命令启动,停止,删除和管理容器。

启动 Docker 容器

以下命令将基于 Debian 镜像启动 Docker 容器。如果您没有本地镜像,将首先下载它:

docker container run debian

乍一看,在你看来,根本没有任何事情发生。嗯,那不是真的。 Debian 容器在启动后立即停止,因为它没有长时间运行的进程,我们没有提供任何命令,因此容器启动,运行空命令然后退出。

选项 -it 允许我们通过命令行与容器进行交互。要启动一个可交互的容器:

docker container run -it debian /bin/bash
root@ee86c8c81b3b:/#

正如您在容器启动时从上面的输出中看到的那样,命令提示符已更改,这意味着您现在正在从容器内部工作:

列出 Docker 容器

要列出活动容器,请键入:

docker container ls

如果您没有任何正在运行的容器,则输出将为空。

要查看活动和非活动容器,请使用选项 -a :

docker container ls -a

删除 Docker 容器

要删除一个或多个容器,只需复制容器 ID (或 或多个 ID)并将其粘贴到 container rm 子命令之后:

docker container rm c55680af670c

结论

您已经学习了如何在 Debian 9 机器上安装 Docker 以及如何下载 Docker 镜像和管理 Docker 容器。本教程几乎没有涉及 Docker 生态系统的表面。在我们的下一篇文章中,我们将继续深入探讨 Docker 的其他方面。

Guess you like

Origin www.cnblogs.com/surplus/p/11367567.html