因为我的主机是win 但是我写代码一般是在虚拟机 Ubuntu或者子系统Wsl里面 导致局域网无法拉取Docker镜像呢~
- 局域网内的 ubuntu系统 需要 docker 拉取镜像
- 本机 ip:192.168.0.104
Docker安装
# 卸载老版本
sudo apt-get remove docker docker-engine docker.io containerd runc
# 设置 Docker 的apt存储库。 如果您使用 Ubuntu 衍生发行版,例如 Linux Mint,则可能需要使用UBUNTU_CODENAME而不是VERSION_CODENAME。
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] 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
#安装Docker包 最新
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
#通过运行镜像来验证 Docker Engine 安装是否成功 hello-world。
sudo docker run hello-world
具体参考 官方文档
镜像拉取
一般的话 sudo docker run hello-world
是会拉取失败 超时
提示错误 大概如下
~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Retrying in 1 second
docker: error pulling image configuration: download failed after attempts=6: dial tcp xx.xx.xx.xx:443: i/o timeout.
See 'docker run --help'.
这个时候换加速源是没有用的 因为Docker被莫名禁止了 所以需要通过代理来运行
添加官方镜像
sudo vim /etc/docker/daemon.json
####内容如下
{
"registry-mirrors": ["https://hub.docker.com/"]
}
添加代理
#新建 proxy.conf 文件
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo cd /etc/systemd/system/docker.service.d
sudo touch proxy.conf
sudo vim /etc/systemd/system/docker.service.d/proxy.conf
####内容如下 检查自己的代理端口地址
[Service]
Environment="HTTP_PROXY=http://192.168.0.104:7890"
Environment="HTTPS_PROXY=http://192.168.0.104:7890"
重启 docker 服务
#加载变更的配置
systemctl daemon-reload
#重启docker服务
systemctl restart docker
查看配置是否生效
查看配置是否生效
systemctl show --property=Environment docker
运行sudo docker run hello-world
将当前用户加入 docker 用户组
这样就不用每次输入sudo
sudo usermod -aG docker $USER
# 然后注销并重新登录,或运行: newgrp docker
常用的Docker命令
类型 | 命令 | 说明 |
---|---|---|
🔍 镜像管理 | docker images | 查看已有镜像 |
docker pull <镜像名> | 拉取镜像 | |
docker rmi <镜像名/ID> | 删除镜像 | |
🏗️ 构建镜像 | docker build -t myimage:tag . | 从 Dockerfile 构建镜像 |
📦 容器运行 | docker run -d -p 8080:80 --name myapp myimage | 启动容器并映射端口 |
docker exec -it myapp /bin/bash | 进入容器终端 | |
docker stop <容器ID/名> | 停止容器 | |
docker start <容器ID/名> | 启动已停止容器 | |
docker restart <容器ID/名> | 重启容器 | |
docker rm <容器ID/名> | 删除容器 | |
🧪 调试 | docker logs <容器ID/名> | 查看容器日志 |
docker ps | 查看运行中的容器 | |
docker ps -a | 查看所有容器(含已停止) | |
🧼 清理 | docker system prune | 一键清理无用镜像、容器、网络 |
docker volume prune | 清理未使用的数据卷 |
常用 docker-compose 命令
前提:当前目录下有 docker-compose.yml 文件
类型 | 命令 | 说明 |
---|---|---|
🚀 启动 | docker-compose up | 启动服务(后台日志输出) |
docker-compose up -d | 启动服务(后台运行) | |
🛑 停止 | docker-compose down | 停止并清理所有服务 |
docker-compose stop | 停止服务但不删除容器 | |
🔄 重启 | docker-compose restart | 重启服务 |
🧱 构建 | docker-compose build | 构建服务镜像 |
docker-compose build --no-cache | 构建镜像,跳过缓存 | |
📜 查看 | docker-compose ps | 查看服务状态 |
docker-compose logs | 查看服务日志 | |
docker-compose logs -f | 实时查看日志 | |
🔍 进入容器 | docker-compose exec <服务名> sh | 进入容器终端(bash 或 sh ) |