Docker安装

发布时间:2017-12-06 22:45:08编辑:admin阅读(5780)

    安装之前,我们首先确保自己的linux系统内核版本高于3.10,并且系统是64位,才能体验Docker。所以我用的是Centos7.3


    直接yum安装就可以了

    [root@localhost ~]# yum install -y docker-io

    默认用的是国外的镜像源,所以网络非常慢

    需要添加国内镜像源才行

    通过 Docker 官方镜像加速,中国区用户能够快速访问最流行的 Docker 镜像。该镜像托管于中国大陆,本地用户现在将会享受到更快的下载速度和更强的稳定性,从而能够更敏捷地开发和交付 Docker 化应用。

    为了永久保留更改,需要修改配置文件 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值。

    [root@localhost ~]# vim /etc/docker/daemon.json

    默认内容是{},修改效果如下:

    {
      "registry-mirrors": ["https://registry.docker-cn.com"]
    }

    重启docker服务

    [root@localhost ~]# systemctl restart docker

    查看docker进程是否存在

    [root@localhost ~]# ps -aux | grep docker
    root      3697  0.3  1.3 629816 25920 ?        Ssl  21:47   0:00 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --selinux-enabled --log-driver=journald --signature-verification=false
    root      3701  0.0  0.4 263768  8212 ?        Ssl  21:47   0:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc --runtime-args --systemd-cgroup=true
    root      3802  0.0  0.0 112664   972 pts/0    R+   21:48   0:00 grep --color=auto docker

    先安装一个软件包,用来支持docker命令补全的

    [root@localhost ~]# yum install -y bash-completion

    注意:必须要退出终端,重新登录一次才能生效。


    查看本地已有的镜像 Docker images

    [root@localhost ~]# docker images

    Cannot connect to the Docker daemon. Is the docker daemon running on this host?

    上面提示说明没有镜像

    那么就去搜索一个镜像,比如ubuntu

    [root@localhost ~]# docker search ubuntu

    结果如下:

    3.PNG

    下载第一个,就是一个基本的ubuntu系统

    [root@localhost ~]# docker pull docker.io/ubuntu

    再次使用命令查看本地镜像,就可以看到下载的镜像了

    [root@localhost ~]# docker images

    REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE

    docker.io/ubuntu                        latest              20c44cd7596f        2 weeks ago         122.8 MB


    启动一个基于docker.io/ubuntu的容器

    [root@localhost ~]# docker run -it --name test-ubuntu docker.io/ubuntu /bin/bash
    root@1cfd7b760ee0:/#


    -it 交互式容器 退出命令窗口容器就停止运行了

    -d 守护容器,就是后台运行,退出命令窗口容器也不会停止

    --name 为容器命名

    docker.io/ubuntu 镜像名

    /bin/bash 进入bash命令行


    root@1cfd7b760ee0:/# 表示已经进入了容器内部


    如果需要退出,可以使用命令exit

    root@1cfd7b760ee0:/# exit
    exit
    [root@localhost ~]#

    执行exit, 此时, 运行docker ps -a 查看容器, 发现状态为Exited, 运行中的状态是Up

    3.PNG

    docker ps 命令, 查看运行中的容器

    docker ps -a命令, 查看全部的容器, 包括运行中和已停止运行的容器

    删除状态为Exited, 已停止运行的容器


    通过容器名称删除已经停止运行的容器(test-centos为docker run命令中给容器的命名)

    [root@localhost ~]# docker rm test-ubuntu
    test-ubuntu
    [root@localhost ~]#

    通过容器ID删除已经停止运行的容器

    docker rm 1cfd7b760ee0 这两个命令效果一样, 1cfd7b760ee0是containerID

    强制删除状态为Up, 正在运行中的容器

    [root@localhost ~]# docker rm -f test-ubuntu








关键字

上一篇: Docker是啥?

下一篇: docker制作镜像