Centos7安装Docker及常用命令使用

一、检查内核版本

Docker 要求 CentOS 系统的内核版本高于 3.10 ,通过 uname -r 命令查看你当前的内核版本

uname -r

二、安装docker

#更新yum到最新版本
yum update -y
#如果报错
cd /etc/yum.repos.d
#备份原文件
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 创建自己的配置文件
touch local.repo 
# 在其中输入以下内容:
vi local.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7



#如有旧版先删除
docker -version
yum remove docker
#安装所需软件包:yum-util(提供yum-config-manager功能)、devicemapper、lvm2
yum install -y yum-utils device-mapper-persistent-data lvm2
#设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#【注意:
#当上面链接(官网)无法连接时,使用下面镜像地址
#yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#】
#可以查看设置源的仓库中所有docker的版本
yum list docker-ce --showduplicates | sort -r
#安装了安装Docker, docker-ce-版本号,我选的是17.12.1.ce
yum -y install docker-ce-17.12.1.ce
#启动Docker,然后加入开机启动
systemctl start docker
systemctl enable docker
#验证是否安装成功
docker version

三、docker常用命令

#查看当前正在运行的容器
docker ps
#查看所有容器的状态
docker ps -a
#启动/停止某个容器
docker start/stop id/name
#进入某个容器(使用exit退出后容器也跟着停止运行)
docker attach id
#启动一个伪终端以交互式的方式进入某个容器(使用exit退出后容器不停止运行)
docker exec -ti id
#查看本地镜像
docker images
#删除某个容器
docker rm id/name
#删除某个镜像
docker rmi id/name
#复制ubuntu容器并且重命名为test且运行,然后以伪终端交互式方式进入容器,运行bash
docker run --name test -ti ubuntu /bin/bash  
#通过当前目录下的Dockerfile创建一个名为soar/centos:7.1的镜像
docker build -t soar/centos:7.1 .
#以镜像soar/centos:7.1创建名为test的容器,并以后台模式运行,并做端口映射到宿主机2222端口,P参数重启容器宿主机端口会发生改变
docker run -d -p 2222:22 --name test soar/centos:7.1 

 

你可能感兴趣的:(Docker)