云原生
Docker
安装 docker
官网教程:https://docs.docker.com/engine/install/
Ubuntu(Debian 类似):
- 卸载旧版 Docker
1 2 3 4
| for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
|
- 更新本地包的索引,安装 Docker 所需的依赖
1 2
| sudo apt-get update sudo apt-get install ca-certificates curl gnupg
|
- 下载 Docker 的 GPG 密钥并设置权限
1 2 3 4 5 6
| sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg sudo chmod a+r /etc/apt/keyrings/docker.gpg
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
- 将 Docker 的官方 APT 源添加到 Ubuntu 系统中
1 2 3 4 5 6 7 8 9 10 11
| echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
- 安装 Docker
1
| sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
CentOS(7):
- 卸载旧版本以及关联的依赖项
1 2 3 4 5 6 7 8
| yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
|
- 安装必要的软件包
1
| yum install -y yum-utils device-mapper-persistent-data lvm2
|
- 添加 Docker 的 GPG 密钥
1 2 3
| yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
|
- 安装最新版本的 Docker 引擎及其所有依赖项
1 2 3 4 5
| yum install -y docker-ce \ docker-ce-cli \ containerd.io \ docker-buildx-plugin \ docker-compose-plugin
|
启动与自启
1 2 3 4 5 6
| systemctl enable docker --now systemctl start docker systemctl enable docker systemctl stop docker systemctl restart docker systemctl status docker
|
配置镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": [ "https://cg6pt9r4.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn", "https://registry.docker-cn.com", "http://hub-mirror.c.163.com" ], "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF
sudo systemctl daemon-reload sudo systemctl restart docker
|
基础命令
1 2 3
| docker version docker info docker system df
|
镜像管理:
1 2 3 4 5 6 7 8 9 10 11 12
| docker pull nginx
docker pull nginx:1.20.1
docker images
docker rmi 镜像名:版本号/镜像id docker rmi -f 镜像 镜像 docker rmi -f $(docker images -aq)
|
容器管理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| docker run --name mynginx -d --restart always -p 88:80 nginx
docker run -it --rm --name my-mysql -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql mysql -uroot -p
docker ps docker ps -a docker ps -n=5
docker ps -a -f status=exited
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| docker stop 容器
docker stop $(dokcer ps -aq)
docker start 容器
docker restart 容器
docker rm 容器
docker rm -f 容器
docker rm -f $(docker ps -aq)
docker update 容器 --restart always
docker create 镜像
|
1 2 3 4 5 6 7 8
| docker stats docker logs 容器 docker top 容器 docker inspect 容器
docker inspect \ --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 容器
|
交互方式
交互模式:
1 2 3 4 5 6 7 8 9 10 11
| docker exec -it 容器 /bin/bash
docker attach 容器
docker run -it --rm mysql:5.7 mysql -h容器ip -uroot -p docker run -it centos /bin/bash
exit Ctrl+P+Q
|
挂载数据:
1 2 3 4
| docker run --name=mynginx -d -p 88:80 \ -v /data/html:/usr/share/nginx/html:ro nginx
|
数据拷贝:
1 2 3 4
| docker cp 容器:/etc/nginx/nginx.conf /data/conf/nginx.conf
docker cp /data/conf/nginx.conf 容器:/etc/nginx/nginx.conf
|
自定义网络:
1 2 3 4 5 6 7
| docker network create my-net
docker network connect my-net db-mysql
docker run -it --rm --network my-net mysql:5.7 mysql -hdb-mysql -uroot -p
|
镜像制作与保存
生成镜像:
1 2
| docker commit -a "作者" -m "信息" 容器名/id 新镜像名[:版本]
|
tar 保存镜像:
1 2 3 4 5 6
| docker save -o 压缩包名.tar 镜像名[:版本]/id
docker load -i 压缩包名.tar
|
镜像推送:
- 为本地的一个镜像添加一个新的标签:
1 2 3
| docker tag 镜像[:版本]/id 新镜像名即标签名[:版本] docker tag mynginx:1.0 ruoxijun/main:v1.0
|
- 登录并推送到云端仓库:
1 2 3 4 5 6 7 8 9 10 11
| docker login
docker login --username=ruoxijun registry.cn-chengdu.aliyuncs.com
docker push 镜像名即标签名[:版本] docker push ruoxijun/main:v1.0
docker logout
|
- 拉取镜像
1 2 3 4 5 6 7
| docker pull ruoxijun/main:v1.0
docker search ruoxijun/main
docker search --limit 5 镜像名称
|
Dockerfile
1 2 3 4 5
| FROM openjdk:8-jdk-slim LABEL maintainer=leifengyang COPY target/*.jar /app.jar ENTRYPOINT ["java","-jar","/app.jar"]
|
1 2 3 4 5 6
| docker build -t java-demo:v1.0 .
docker build -f /path/to/a/Dockerfile -t <image-name>:<tag> .
docker history java-demo:v1.0
|
docker-compose
1 2 3 4 5 6 7 8
| docker-compose up -d
docker-compose down
docker-compose start|stop|restart
docker-compose up -d --build
|