Docker 在CentOS 7中安装Docker

官网安装文档https://docs.docker.com/engine/installation/linux/docker-ce/centos/


1-确认系统信息

# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)
# uname -a
Linux docker 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

2-安装docker

[shu@docker ~]$ yum list docker-ce --showduplicates | sort -r
[shu@docker ~]$ sudo yum install 17.09.0.ce-1.el7.centos
[sudo] password for shu: 
shu is not in the sudoers file.  This incident will be reported.
解决:
[root@docker ~]# chmod u+w /etc/sudoers
[root@docker ~]# vi /etc/sudoers 
最后增加:%sudo ALL=(ALL) NOPASSWD: ALL 
#允许用户组sudo里面的用户执行sudo命令,并且在执行的时候不输入密码.
[root@docker ~]# chmod u-w /etc/sudoers
[root@docker ~]# 
[root@docker ~]# groupadd sudo
[root@docker ~]# usermod -aG sudo,adm shu

# yum -y install docker

[shu@docker yum.repos.d]$ yum list docker-ce --showduplicates | sort -r
 * updates: ftp.sjtu.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
 * extras: ftp.sjtu.edu.cn
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
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.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
 * base: ftp.sjtu.edu.cn
Available Packages
[shu@docker yum.repos.d]$ sudo yum install docker-ce-17.09.0.ce-1.el7.centos

安装指定版本

3-关闭SELinux和启动docker服务

关闭SELinux

  • 永久方法:修改/etc/selinux/config文件中设置SELINUX=disabled ,然后重启。
  • 临时方法:执行setenforce 0命令设置SELinux成为permissive模式

    sudo systemctl status firewalld.service
    sudo systemctl stop firewalld.service          
    sudo systemctl disable firewalld.service

启动docker服务
[root@CentOS-7 ~]# systemctl start docker.service

设置自启动docker服务

[root@CentOS-7 ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@CentOS-7 ~]# systemctl is-enabled docker.service
enabled
[root@CentOS-7 ~]# 

4-设置docker代理(若需要)

# vim /etc/sysconfig/docker
添加如下内容
HTTP_PROXY=http://10.144.1.10:8080
HTTPS_PROXY=https://10.144.1.10:8080
FTP_PROXY=ftp://10.144.1.10:8080

5-重启docker

# systemctl daemon-reload
# systemctl restart docker

6-安装验证

运行官方镜像hello-world文件

# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
c04b14da8d14: Pull complete 
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for docker.io/hello-world:latest

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.
 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 Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

# 
# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              c54a2cc56cbb        5 months ago        1.848 kB

另外一种Docker代理设置方法(若需要):

修改docker.service文件
# vim /usr/lib/systemd/system/docker.service
在[Service]部分添加如下内容
Environment="HTTP_PROXY=http://10.144.1.10:8080"
Environment="HTTPS_PROXY=https://10.144.1.10:8080"
Environment="FTP_PROXY=ftp://10.144.1.10:8080"

或者,另外创建代理文件

# mkdir /etc/systemd/system/docker.service.d
# touch /etc/systemd/system/docker.service.d/proxy.conf
# vim /etc/systemd/system/docker.service.d/proxy.conf

增加以下内容
[Service]
Environment="HTTP_PROXY=http://10.144.1.10:8080"
Environment="HTTPS_PROXY=https://10.144.1.10:8080"
Environment="FTP_PROXY=ftp://10.144.1.10:8080"

重启docker
# systemctl daemon-reload
# systemctl restart docker

检查docker环境变量是否加载

# systemctl show docker --property Environment
Environment=GOTRACEBACK=crash HTTP_PROXY=http://10.144.1.10:8080 HTTPS_PROXY=https://10.144.1.10:8080 FTP_PROXY=ftp://10.144.1.10:8080

Log - Install Docker in CentOS-7

[root@CentOS7 ~]# route add default gw 10.0.3.2
[root@CentOS7 ~]# 
[root@CentOS7 ~]# yum -y install docker
[root@CentOS7 ~]# vim /etc/selinux/config
[root@CentOS7 ~]#  cat /etc/selinux/config |grep "SELINUX="
# SELINUX= can take one of these three values:
SELINUX=disabled
[root@CentOS7 ~]# 
[root@CentOS7 ~]# setenforce 0
[root@CentOS7 ~]# 
[root@CentOS7 ~]# systemctl start docker.service
[root@CentOS7 ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@CentOS7 ~]# systemctl is-enabled docker.service
enabled
[root@CentOS7 ~]# 
[root@CentOS7 ~]# sudo mkdir -p /etc/docker
[root@CentOS7 ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://t5t8q6wn.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://t5t8q6wn.mirror.aliyuncs.com"]
}
[root@CentOS7 ~]# systemctl daemon-reload
[root@CentOS7 ~]# systemctl restart docker
[root@CentOS7 ~]# 
[root@CentOS7 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@CentOS7 ~]# 
[root@CentOS7 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
78445dd45222: Pull complete 
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7

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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

[root@CentOS7 ~]# 
[root@CentOS7 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              48b5124b2768        3 months ago        1.84 kB
[root@CentOS7 ~]# 
   
   

Uninstall Docker CE

  1. Uninstall the Docker package:

    $ sudo yum remove docker-ce
    
  2. Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:

    $ sudo rm -rf /var/lib/docker
    

You must delete any edited configuration files manually.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值