技术交流28群

服务热线

135-6963-3175

微信服务号

2、Docker安装 更新时间 2019-1-13 浏览2040次

  Docker安装


  建议在Linux环境中安装Docker。 win环境复杂且容易出错。 使用Centos7 yum安装Docker环境非常方便。

  卸载旧版本

    sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
yum update 升级本地yum包


 首先安装所依赖的软件包,yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

  sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2


设定稳定的仓库

阿里源:

sudo yum-config-manager \
    --add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

官方源(慢):

sudo yum-config-manager \
    --add-repo \    
https://download.docker.com/linux/centos/docker-ce.repo


也可以通过下面文件进行设置源

tree /etc/yum.repos.d/docker.repo <<-'EOF'  添加yum 仓库配置(内容见下框)
[dockerrepo] 
name=Docker Repository 
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpgEOF

 

 安装docker-ce社区版

     安装最新版本的 Docker Engine-Community 和 containerd,您只需运行以下yum命令:

sudo yum install docker-ce docker-ce-cli containerd.io
#--enablerepo=dockerrepo指定仓库源

过程中需要一次导入 GPG key确认y/n?:选择y

要安装特定版本的 Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:

$ yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64             17.06.1.ce-1.el7.centos           docker-ce-stable
docker-ce.x86_64             17.06.0.ce-1.el7.centos           docker-ce-stable
docker-ce.x86_64             17.03.3.ce-1.el7                     docker-ce-stable
docker-ce.x86_64             17.03.2.ce-1.el7.centos           docker-ce-stable
docker-ce.x86_64             17.03.1.ce-1.el7.centos            docker-ce-stable
docker-ce.x86_64             17.03.0.ce-1.el7.centos           docker-ce-stable
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io


安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:

service docker start
chkconfig docker on

LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:

systemctl start docker.service
systemctl enable docker.service

测试

docker version

输入上述命令,返回docker的版本相关信息,证明docker安装成功。

  接下来,让我们通过最简单的映像文件“ hello world”来了解Docker。

  由于官方的国内Docker仓库非常慢,因此我们将在日常使用中使用Docker China加速器。 通过Docker官方镜像加速,中国用户可以快速访问最受欢迎的Docker映像。 该映像托管在中国大陆,并且本地用户现在将享受更快的下载速度和更高的稳定性,从而他们可以更敏捷地开发和交付Dockerized应用程序。

  可以通过registry.docker-cn.com访问Docker China的官方映像加速。 镜像库仅包含流行的公共镜像,而私人镜像仍需要从美国镜像库中提取。

  只需修改系统中与docker相对应的配置文件,如下所示:

vi  /etc/docker/daemon.json#添加后
{    
"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com"],    
"live-restore": true
}

可选仓库:

"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"https://pee6w651.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com"


运行下面的命令,将 image 文件从仓库抓取到本地。

docker pull library/hello-world

在上面的代码中,docker image pull是获取镜像文件的命令。  library / hello-world是镜像文件在仓库中的位置,library是镜像文件所在的组,hello-world是图像文件的名称。

  成功获取后,您可以在计算机上看到镜像文件。

docker images#显示结果
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world           latest              f2a91732366c        3 months ago        1.848 kB

现在,运行这个 image 文件。

$ 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.
    (amd64)
 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 ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

输出这段提示以后,hello world就会停止运行,容器自动终止。有些容器不会自动终止,因为提供的是服务,比如Mysql镜像等。

## 常用命令

除过以上我们使用的Docker命令外,Docker还有一些其它常用的命令

拉取docker镜像

docker pull image_name

查看宿主机上的镜像,Docker镜像保存在/var/lib/docker目录下:

docker images

删除镜像

docker rmi  docker.io/tomcat:7.0.77-jre7   或者  docker rmi b39c68b7af30

查看当前有哪些容器正在运行

docker ps

查看所有容器

docker ps -a

启动、停止、重启容器命令:

docker start container_name/container_id
docker stop container_name/container_id
docker restart container_name/container_id

后台启动一个容器后,如果想进入到这个容器,可以使用attach命令:

docker attach container_name/container_id

删除容器的命令:

docker rm container_name/container_id

查看当前系统Docker信息

docker info

从Docker hub上下载某个镜像:

docker pull centos:latest
docker pull centos:latest

执行docker pull centos会将Centos这个仓库下面的所有镜像下载到本地repository。