1.docker容器环境安装

容器常用选项

一、容器介绍

1、容器是什么?

轻量级虚拟化技术

应用场景:

1、快速构建业务环境

2、便于业务迁移,避免兼容性问题

2、与传统虚拟化区别

创建速度快, 秒级

无虚拟硬件

共享物理机内核、IO速度快

3、容器的三要素

容器、镜像、仓库

4、容器核心技术

  • namespace技术 命名空间

    实现资源(文件目录、用户、端口、进程)隔离

  • cgroup技术

    实现容器的资源限制(cpu、内存)

5、容器管理工具/软件

  • docker
    docker-ce、docker-ee
  • podman
  • containerd

二、安装docker

1、配置docker软件仓库

bash 复制代码
[root@martin-host ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

[root@martin-host ~]# cat /etc/yum.repos.d/docker-ce.repo
[docker-ce]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7.9/x86_64/stable/
enabled=1
gpgcheck=0

2、安装docker, 启动docker服务

bash 复制代码
[root@martin-host ~]# yum install -y docker-ce 

[root@martin-host ~]# rpm -q docker-ce
docker-ce-26.1.4-1.el7.x86_64

[root@martin-host ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@martin-host ~]# 

3、配置国内docker镜像仓库

bash 复制代码
[root@martin-host ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://rywdmoco.mirror.aliyuncs.com"]
}
[root@martin-host ~]# 
[root@martin-host ~]# systemctl restart docker

4、主机网络变化

bash 复制代码
// docker0虚拟网卡,默认作为所有容器的网关使用
[root@martin-host ~]# ip addr show docker0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:81:00:b2:4b brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

// 路由转发
[root@martin-host ~]# cat /proc/sys/net/ipv4/ip_forward
1

// docker网段的SNAT规则
[root@martin-host ~]# iptables -t nat -nL 
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           

Chain DOCKER (2 references)
target     prot opt source               destination         
RETURN     all  --  0.0.0.0/0            0.0.0.0/0       

三、镜像基础操作

1、查看镜像

bash 复制代码
[root@martin-host ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    dabbfbe0c57b   2 years ago   144MB
nginx        1.18      c2c45d506085   3 years ago   133MB

2、搜索镜像

bash 复制代码
[root@martin-host ~]# docker search 关键字

3、下载镜像

bash 复制代码
[root@martin-host ~]# docker pull 镜像名称:标记

4、导入镜像

bash 复制代码
[root@martin-host ~]# docker load -i centos7.tar 
174f56854903: Loading layer [==================================================>]  211.7MB/211.7MB
Loaded image: centos:7

5、导出镜像

bash 复制代码
[root@martin-host ~]# docker save -o tomcat.tar tomcat:latest 
相关推荐
Patrick_Wilson5 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
Suroy6 天前
DockerView-Go:用 Go 写一个终端 Docker 监控工具,顺便做了个 Web 仪表盘
docker
云恒要逆袭6 天前
运行你的第一个Docker容器
后端·docker·容器
宋均浩7 天前
# Docker 镜像瘦身实战:从 1.2G 到 80MB 的五个优化步骤
ci/cd·docker
程序员老赵7 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程
WangMingHua1117 天前
LM Studio Docker 部署——本地大模型一键启动
docker
曲幽9 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
武子康10 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
Alsn8613 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
2601_9618752413 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant