CentOS 7安装Docker

作者: 溯水心生 | 来源:发表于2018-03-25 16:18 被阅读116次

一、Docker安装

1.卸载旧版本

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

2.安装并运行Docker[使用存储库进行安装]

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

3.使用以下命令设置稳定的存储库。即使您想从边缘或测试存储库安装构建,也总是需要稳定的存储 库

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

4. 安装docker

sudo yum install docker-ce

5.检查安装结果

# docker info

出现如下信息,则docker安装成功

Kernel Version: 3.10.0-693.17.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 1.359 GiB
Name: localhost.localdomain
ID: KE6P:FAHI:ZYWT:AUWU:NVFG:6JRF:33ZS:AT4X:63QZ:ICYW:PUTO:V4ZB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)

4.使用阿里镜像实现docker镜像加速

访问阿里云Docker管理地址,点击镜像加速器,获取镜像加速地址。

其中地址为阿里云生成,每个人生成会有所不同。

如何配置镜像加速器[Centos 7]

针对Docker客户端版本大于1.10.0的用户

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://7e2zsv7s.mirror.aliyuncs.com"] 
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

查看镜像加速地址:

[root@localhost ~]# docker info
Containers: 1
 Running: 0
 Paused: 0
 Stopped: 1
Images: 1
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: docker-init
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: N/A (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: N/A (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
 selinux
Kernel Version: 3.10.0-693.17.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 1.359 GiB
Name: localhost.localdomain
ID: KE6P:FAHI:ZYWT:AUWU:NVFG:6JRF:33ZS:AT4X:63QZ:ICYW:PUTO:V4ZB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:
 https://7e2zsv7s.mirror.aliyuncs.com
Live Restore Enabled: false
Registries: docker.io (secure)

5.查看docker版本

[root@localhost ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: <unknown>
 Go version:      go1.8.3
 Git commit:      774336d/1.13.1
 Built:           Wed Mar  7 17:06:16 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: <unknown>
 Go version:      go1.8.3
 Git commit:      774336d/1.13.1
 Built:           Wed Mar  7 17:06:16 2018
 OS/Arch:         linux/amd64
 Experimental:    false

二、Docker基本用法

1.Docker守护进程管理

# systemctl start docker     #运行Docker守护进程
# systemctl stop docker      #停止Docker守护进程
# systemctl restart docker   #重启Docker守护进程
# docker ps -a               #查看已存在且创建的容器
# docker ps                  #查看正在运行的容器
# docker images              #查看已存在的镜像

2.镜像管理-使用的是来自阿里云仓库的Apache镜像

# docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  • 修改标签,由于阿里云仓库镜像的镜像名称很长,可以修改镜像标签以便记忆区分。
# docker tag  registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
  • 查看已有镜像
# docker images
  • 强制删除镜像
# docker  rmi  –f  registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5

3.容器管理

  • e121d5f99e1e是执行docker images命令查询到的IMAGE ID,使用docker run命令进入容器
# docker run –ti e121d5f99e1e /bin/bash
  • 使用exit可以退出当前容器
  • run命令加上–d参数可以在后台运行容器,—name指定容器命名为apache
# docker run -d --name apache e121d5f99e1e
  • 进入后台运行的容器
# docker exec -ti apache /bin/bash
  • 将容器做成镜像
# docker commit containerID/containerName  newImageName:tag

4.容器操作

  • 进入容器
sudo docker exec -it 5e74792c44ad  /bin/bash
  • 退出容器
exit
  • 删除容器
docker rm arc
  • 强制删除镜像
docker rmi -f redis

相关文章

网友评论

    本文标题:CentOS 7安装Docker

    本文链接:https://www.haomeiwen.com/subject/lvzhcftx.html