Docker安装(Centos7.9)及常用命令

 Centos下载  - CentOS-7-x86_64-Minimal-2009.iso(安装虚拟机时,记得开启network)

http://mirrors.aliyun.com/centos/7/isos/x86_64/

安装网络工具

sudo yum install net-tools
1.下载安装wget
yum install -y wget

2.备份默认的yum
mv /etc/yum.repos.d /etc/yum.repos.d.backup

3.设置新的yum目录
mkdir -p /etc/yum.repos.d

4.下载阿里yum配置到该目录中,选择对应版本
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

5.更新epel源为阿里云epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

6.重建缓存
yum clean all
yum makecache

7.看一下yum仓库有多少包
yum repolist

升级系统内核

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-lt
grep initrd16 /boot/grub2/grub.cfg
grub2-set-default 0
reboot

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
setenforce 0

网桥过滤

vi /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
net.ipv4.ip_forward=1
net.ipv4.ip_forward_use_pmtu = 0
生效命令
sysctl --system

命令补全

安装bash-completion
yum -y install bash-completion bash-completion-extras
使用bash-completion
source /etc/profile.d/bash_completion.sh

上传文件

yum -y install lrzsz

安装docker前置条件

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
yum makecache fast

查看docker版本

yum list docker-ce --showduplicates | sort -r

安装docker

安装最新版:推荐大家安装最新版本
yum -y install docker-ce
安装指定版本:
语法规则:yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
yum -y install docker-ce-18.06.3.ce-3.el7 docker-ce-cli.x86_64
yum install -y docker-ce-19.03.9-3.el7 docker-ce-cli-19.03.9-3.el7

开启docker服务

systemctl start docker
systemctl status docker

安装阿里云镜像加速器, 注册阿里云获取自己的加速器地址(https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors)

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

设置docker开启启动服务

systemctl enable docker

docker命令

docker -v
docker version
docker info

大功告成了!接下来我们再看一下常用命令

//docker远程仓库
https://hub.docker.com/

//拉取镜像
docker pull tomcat:9.0.20-jre8-alpine

列出本机已有的镜像

docker images

保存镜像为tar

docker save tomcat:9.0.20-jre8-alpine -o tomcat9.tar

获取镜像的详细信息

docker inspect tomcat:9.0.20-jre8-alpine

删除镜像

docker rmi tomcat:9.0.20-jre8-alpine

Docker容器常用命令

运行容器

//-d 后台运行 -it交互模式  -p端口映射 --name命名
docker run -itd --name tomcat9 --rm -p 8080:8080 tomcat:9.0.20-jre8-alpine

宿主机挂载数据卷(虚拟机重启,容器中的内容不会保存。需要挂载)

//docker run -v /宿主机绝对路径目录:/容器内目录 镜像名
docker run -itd --name nginx -p 80:80 -v mydata/nginx:/etc/nginx nginx:1.19.3-alpine

获取容器的日志

docker logs -f tomcat9

重启,暂停,删除容器

docker start tomcat9
docker stop tomcat9
docker rm tomcat9

查看运行中的程序

//-a 所有容器
docker ps

实用技巧

停止所有运行容器
docker stop $(docker ps -qa)
删除所有的容器
docker rm $(docker ps -aq)
docker rm $(docker stop $(docker ps -q))
删除所有的镜像
docker rmi $(docker images -q)

进入容器

docker exec -it tomcat9 /bin/bash
docker exec -it tomcat9 /bin/sh

查看容器

docker inspect tomcat9

杀掉容器

docker kill tomcat9

 

posted on 2021-03-25 22:48  lvguoliang(学无止境)  阅读(423)  评论(0编辑  收藏  举报