Docker 安装与使用

一、Docker 安装

        使用官方安装脚本自动安装:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

        也可以使用国内 daocloud 一键安装命令:

curl -sSL https://get.daocloud.io/docker | sh

二、Docker 使用

       2.1 获取镜像

        如果我们本地没有 ubuntu:15.10 镜像,我们可以使用 docker pull 命令来载入 ubuntu:15.10 镜像:

[root@VM_0_8_centos ~]# docker pull ubuntu:15.10 

        2.2 启动 Docker 容器

        使用 ubuntu:15.10 镜像启动一个容器:

[root@VM_0_8_centos ~]# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

        使用 docker run命令启动一个镜像容器。

        ubuntu:15.10 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。

        /bin/echo "Hello world": 在启动的容器里执行的命令。

        以上命令完整的意思可以解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。

        2.3 运行交互式的 Docker 容器

[root@VM_0_8_centos ~]# docker run -i -t ubuntu:15.10 /bin/bash
root@ff81c0d5a17e:/# 

        -i 参数:允许你对容器内的标准输入进行交互。

        -t 参数:在新容器内指定一个伪终端或终端。

        看到root@ff81c0d5a17e:/# 表示我们已经进入一个 ubuntu15.10 系统的容器。

        使用 cat /proc/version 查看当前系统的版本信息:

root@ff81c0d5a17e:/# cat /proc/version 
Linux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
root@ff81c0d5a17e:/# 

        2.4 使用 exit 或者 ctrl+d 退出容器

root@ff81c0d5a17e:/# exit
exit
[root@VM_0_8_centos ~]# 

        2.5 后台模式启动容器

[root@VM_0_8_centos ~]# docker run -d ubuntu:15.10 /bin/bash
075b5e09e067bacc409b85a178f9acfb982fb17ba93f30ee0d49d7a1685c160e

        -d参数: 后台运行容器,并返回容器ID。这个长字符串叫做容器 ID,对每个容器来说都是唯一的。

        2.6 查看容器

[root@VM_0_8_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS           NAMES
075b5e09e067        ubuntu:15.10        "/bin/bash"         9 seconds ago       Exited (0) 8 seconds ago                    sweet_liskov

        docker ps -a 显示的是所有的 Docker 容器。

        docker ps 显示所有正在运行的 Docker 容器。

        CONTAINER ID: 容器 ID。

        IMAGE: 使用的镜像。

        COMMAND: 启动容器时运行的命令。

        CREATED: 容器的创建时间。

        STATUS: 容器状态。

        状态有7种:

  • created(已创建)
  • restarting(重启中)
  • running(运行中)
  • removing(迁移中)
  • paused(暂停)
  • exited(停止)
  • dead(死亡)

        PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。

        NAMES: 自动分配的容器名称。

        2.7 查看容器内的标准输出

[root@VM_0_8_centos ~]# docker logs 075b5e09e067

        或用容器名称

[root@VM_0_8_centos ~]# docker logs sweet_liskov

        2.8 停止容器

[root@VM_0_8_centos ~]# docker stop 075b5e09e067

        也可以:

[root@VM_0_8_centos ~]# docker stop sweet_liskov

        2.9 启动已停止的容器

[root@VM_0_8_centos ~]# docker start 075b5e09e067
075b5e09e067

        2.10 重启容器

[root@VM_0_8_centos ~]# docker restart 075b5e09e067
075b5e09e067

        2.11 进入容器

        在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过以下指令进入:

[root@VM_0_8_centos ~]# docker attach 075b5e09e067

        使用 attach 命令进入容器,当退出容器后会导致容器停止。那么就推荐exec命令,如下:

[root@VM_0_8_centos ~]# docker exec -it 075b5e09e067 /bin/bash

        如果从这个容器退出,不会导致容器的停止。

三、Docker 客户端

        使用 docker 命令查看 Docker 客户端的所有命令选项:

[root@VM_0_8_centos ~]# docker

Usage:    docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

        可以通过命令 docker command --help 更深入的了解指定的 Docker 命令使用方法:

[root@VM_0_8_centos ~]# docker run --help

Usage:    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds

四、导入和导出容器

        4.1 导出容器

[root@VM_0_8_centos ~]# docker expert 075b5e09e067 > ubuntu.tar

        导出容器 075b5e09e067 快照到本地文件 ubuntu.tar。

        4.2 导入容器

cat ubuntu.tar | docker import - test/ubuntu:v1

        查看导入结果:

[root@VM_0_8_centos test]# docker images
test/ubuntu         v1                  e76cc2165488        4 minutes ago       73.9MB

        4.3 删除容器

[root@VM_0_8_centos test]# docker rm -f 7682e260b663

        4.4 清理掉所有处于终止状态的容器

[root@VM_0_8_centos test]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] 

结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值