docker
1.docker环境部署以及语法
[root@docker ~]# cat << EOF | tee /etc/modules-load.d/k8s.conf
> overlay
> br_netfilter
> EOF
overlay
br_netfilter
[root@docker ~]# modprobe overlay
[root@docker ~]# modprobe br_netfilter
[root@docker ~]# cat /etc/modules-load.d/k8s.conf
overlay
br_netfilter
[root@docker ~]# sysctl --system
[root@docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
# 配置一个仓库
[root@docker ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@docker ~]# ls /etc/yum.repos.d/
CentOS-Base.repo docker-ce.repo epel.repo epel-testing.repo haha.repo
[root@docker ~]# yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# 启动服务
[root@docker ~]# systemctl start docker
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
# 搜索镜像
[root@docker ~]# docker search tomcat
# 配置docker守护进程
[root@docker ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
]
[root@docker ~]# systemctl restart docker
# 下载centos镜像
[root@docker ~]# docker pull centos
# 开启服务
[root@docker ~]# systemctl start docker.service
# 查看镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
# -i 允许交互 -t终端运行
[root@docker ~]# docker run -i -t --name=c1 centos:latest /bin/bash
[root@21d6b0b5e067 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@21d6b0b5e067 /]# cd /etc/yum.repos.d/
[root@21d6b0b5e067 yum.repos.d]# ls
CentOS-Linux-AppStream.repo CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo CentOS-Linux-Sources.repo
# 不能查看ip(容器)
[root@21d6b0b5e067 yum.repos.d]# ifconfig
bash: ifconfig: command not found
[root@21d6b0b5e067 yum.repos.d]# cd
# 配置阿里云的仓库
[root@21d6b0b5e067 /]# rm -rf /etc/yum.repos.d/*
[root@21d6b0b5e067 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 464 0 0:00:05 0:00:05 --:--:-- 637
[root@21d6b0b5e067 /]# ls /etc/yum.repos.d/
CentOS-Base.repo
# 清除缓存,建立缓存
[root@21d6b0b5e067 /]# yum clean all && yum makecache
# 安装httpd和net-tools
[root@21d6b0b5e067 /]# yum -y install httpd
[root@21d6b0b5e067 /]# yum -y install net-tools
# 可以查到ip地址
[root@21d6b0b5e067 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 10539 bytes 17122684 (16.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7348 bytes 404167 (394.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@21d6b0b5e067 /]# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
71 pts/0 00:00:00 ps
[root@21d6b0b5e067 /]# uptime
06:33:36 up 3:35, 0 users, load average: 0.06, 0.05, 0.05
[root@21d6b0b5e067 /]# pwd
/
# 查看版本信息
[root@21d6b0b5e067 /]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
# 修改资源文件
[root@21d6b0b5e067 /]# echo "docker_httpd_server" > /var/www/html/index.html
# 启动服务(启动失败)--内存太小
[root@21d6b0b5e067 /]# systemctl start httpd
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
# 手动启动
[root@21d6b0b5e067 /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
# 在容器里可以访问到内容
[root@21d6b0b5e067 /]# curl localhost
docker_httpd_server
[root@21d6b0b5e067 /]# curl http://localhost:80
docker_httpd_server
# 在配置c1容器的主机里也可以访问到内容
[root@docker ~]# curl http://172.17.0.2:80
docker_httpd_server
# 外界浏览器访问不到内容
2.docker创建容器的语法
3.退出以及重进容器
# 如果没有指令正在运行,容器就会停止
[root@21d6b0b5e067 /]# exit
exit
[root@docker ~]# curl 172.17.0.2
^C
[root@docker ~]# ssh root@172.17.0.2
ssh: connect to host 172.17.0.2 port 22: No route to host
# 退出c1镜像后也可以继续进到容器中
# 重启容器
[root@docker ~]# docker start c1
c1
# 将c1容器的终端附加到docker主机上
[root@docker ~]# docker attach c1
# 这个时候,httpd服务又停了
[root@21d6b0b5e067 /]# netstat -lntup|grep 80
# 启动服务
[root@21d6b0b5e067 /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
# 可以访问
[root@21d6b0b5e067 /]# curl localhost
docker_httpd_server
# 退出容器服务不中断-按ctrl+p+q键
[root@21d6b0b5e067 /]systemctl start httpdread escape sequence
[root@docker ~]# curl 172.17.0.2
docker_httpd_server
4.远程管理
# 检查状态
# 查看套接字文件
[root@docker ~]# ls -lh /var/run/
srw-rw----. 1 root docker 0 8月 22 11:44 docker.sock
# 要允许远程连接,需要有一个服务,使用端口
[root@docker ~]# systemctl start docker
[root@docker ~]# netstat -lntup|grep 2375
[root@docker ~]# ls -lh /var/run/
# 设置允许远程管理
# 停止docker服务
[root@docker ~]# vim /usr/lib/systemd/system/docker.service
# 删除13行dockerd后面的内容
13 ExecStart=/usr/bin/dockerd
# 修改daemon.json文件
[root@docker ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
],
"hosts": [
"tcp://0.0.0.0:2375",
"unix:///var/run/docker.sock"
]
}
# 加载daemon.json文件
[root@docker ~]# systemctl daemon-reload
# 启动服务
[root@docker ~]# systemctl start docker
[root@docker ~]# netstat -lntup|grep 2375
tcp6 0 0 :::2375 :::* LISTEN 6321/dockerd
# 查看套接字文件
[root@docker ~]# ls -lh /var/run/
srw-rw----. 1 root docker 0 8月 22 16:10 docker.sock
# 远程管理
[root@docker ~]# docker -H 192.168.2.30 images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB