Docker基础2
如需转载,标记出处
本次我们将下载一个 Docker 镜像,从镜像中启动容器
上一章,安装 Docker 时,获得两个主要组件:
-
Docker 客户端
-
Docker 守护进程(有时称为“服务器”或“引擎”)
守护进程实现 Docker Remote API。 在默认的 Linux 安装中,客户端通过位于 /var/run/docker.sock
的本地 IPC/Unix 套接字与守护进程通信。
用 docker version
命令测试客户端和守护进程是否正在运行并可以相互通信
从远处拉取镜像,使用容器
映像包含足够的操作系统 (OS),以及运行其设计的任何应用程序所需的所有代码和依赖项。
从Docker Hub 拉取最新版本的 ubuntu
镜像到本地
将镜像(一般在github上)放到 Docker 主机(本地这边)称为“拉取”。
其实就是下载
docker pull ubuntu:latest
(如果拉取失败是十有八九是没设置 Docker 的代理,看下面第一个问题集)
运行 docker image ls
命令以查看刚刚拉取的映像
docker container run
命令从中启动容器。
docker container run -it ubuntu:latest /bin/bash root@d08b290c8882:/#
shell 提示都发生了变化
因为 shell 现在连接了新容器 shell 上
按下 Ctrl-PQ
退出容器
docker container ls查看容器状态
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES db489e5f4d94 ubuntu:latest "/bin/bash" 40 seconds ago Up 40 seconds wizardly_jackson d08b290c8882 ubuntu:latest "/bin/bash" 7 minutes ago Up 7 minutes festive_shannon
前面步骤中的容器仍在运行,用 docker container exec
命令将 shell 连接到正在运行的容器。
docker container exec -it wizardly_jackson bash
使用 docker container stop
和 docker container rm
命令停止容器/关闭容器
比如:
admin123@admin-virtual-machine:~$ docker container kill wizardly_jackson wizardly_jackson
从github上拉取到本地,手动构建容器
上面一部分和这部分有区别吗
肯定的
Docker hub是专门的 容器镜像仓库,里面的镜像是 预先构建好的,由官方或社区成员提前创建、打包好并上传,可以直接用
从 Git 仓库克隆一个应用程序,检查其 Dockerfile,容器化,然后将其作为容器运行
git clone https://github.com/nigelpoulton/psweb.git
(如果报错看问题集4)
admin123@admin-virtual-machine:~/learn$ ls psweb admin123@admin-virtual-machine:~/learn$ cd psweb admin123@admin-virtual-machine:~/learn/psweb$ ls app.js Dockerfile package.json README.md views
cd 进入目录需要保证 ls 后出现 Dockerfile,Dockerfile 是一个纯文本文档,描述了如何构建 Docker 镜像。 使用 docker image build
命令,按照 Dockerfile 中的说明创建新的映像
admin123@admin-virtual-machine:~/learn/psweb$ cat Dockerfile # Test web-app to use with Pluralsight courses and Docker Deep Dive book FROM alpine LABEL maintainer="nigelpoulton@hotmail.com" # Install Node and NPM RUN apk add --update nodejs npm curl # Copy app to /src COPY . /src WORKDIR /src # Install dependencies RUN npm install EXPOSE 8080
根据 Dockerfile
文件构建一个名字叫 test
的镜像
docker image build -t test:latest .
上面的不行换这个
docker buildx build --build-arg http_proxy=http://你的代理ip:端口号\--build-arg https_proxy=你的代理ip:端口号 \-t test:latest .
使用上一步构建的镜像来启动名字叫web1的容器
docker container run -d \--name web1 \-p 8080:8080 \test:latest
访问 http://127.0.0.1:8080/
就可以看到
Hello Docker learners!!! Check out my other books Quick Start KubernetesThe Kubernetes BookAI ExplainedBe careful. The last time I updated the packages in this app was Dec 2024.
BUG报错问题集
1.Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
网络问题,输入ifconfig在ens33这个网卡下面没有看到ipv4地址,说明网卡掉了
sudo ifconfig ens33 down sudo ifconfig ens33 up
我也不知道为啥虚拟机的网卡掉线是家常便饭,不过我的问题还是没解决又碰到了下面的报错
4.6补充:如果你一直在这边卡着,网络问题也解决了,剩下的问题就是代理问题了,我昨天写到这里排查半天,查了两本教程书籍,不同种拉取镜像的方法都不行,今天除了代理的问题解决的,
我自己的ubuntu虚拟机的代理不知道啥时候也掉线了
1.永久设置代理(如果已经有这一步跳过即可)
编辑 ~/.bashrc
或 ~/.zshrc
文件:
sudo vim ~/.bashrc
在文件末尾添加:(这个只是 参考我也不知道你用的哪个代理,如果是nat用的是主机的ip,桥接就是ifcomfig查虚拟机自己的ip,端口号看使用的哪个软件用的)
唯一需要注意的是下面不是我打错,没有https://
除了ip和端口号需要根据自身情况改其他的不用
export http_proxy="http://127.0.0.1:xxxx" export https_proxy="http://127.0.0.1:xxxx"
保存后运行:
source ~/.bashrc
虚拟机的代理配置好,docker的也要配置
配置过程如下:
2.创建docker代理配置文件:
编辑 http-proxy.conf
文件:
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
在文件中加入代理环境变量:(这里还要细分你的虚拟机时)
[Service] Environment="HTTP_PROXY=http://127.0.0.1:xxxx/" Environment="HTTPS_PROXY=http://127.0.0.1:xxxx/"
2.Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded
解决方案
检查 Docker 配置文件中的 --registry-mirror 设置: 如果你希望使用镜像源,可以将该配置项移到 /etc/docker/daemon.json
中,而不是通过命令行启动时设置。这样可以避免与命令行启动参数冲突。
打开 /etc/docker/daemon.json
并添加以下内容:
{"registry-mirrors": ["https://mirrors.ustc.edu.cn"] }
清除命令行中的 --registry-mirror 参数: 修改完 /etc/docker/daemon.json
后,删除或注释掉启动时使用的 --registry-mirror 参数。
你可以编辑 Docker 服务文件来移除这个启动参数:
sudo systemctl edit docker
在打开的编辑器中删除或注释掉 --registry-mirror
参数(如果存在),保存并退出。
重载并重启 Docker 服务:
sudo systemctl daemon-reload sudo systemctl restart docker
3.*启动docker失败 Active: failed *
按照经验,一般都是在配置文件里面写的参数有问题,把改的东西还原回来再重启就行了
上面的话还是不行,再看下面的一种法子
查看日志:
sudo journalctl -u docker.service -n 50
其中有一句:
unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file.
分析:
错误的根本原因是 /etc/docker/daemon.json
配置文件中有一些选项是重复定义的,既通过配置文件设置,又通过命令行参数传递。这导致了 Docker 启动失败。
把文件中的内容全部删除,日志还是显示说重复了,最初能正常运行的时候这个文件就是空白的。
查看 Docker 服务的启动参数:
ps aux | grep dockerd
我的显示结果是这样的:
admin123@admin-virtual-machine:~$ ps aux | grep dockerdroot 11349 0.1 0.4 1895628 69920 ? Ssl 19:02 0:00 /usr/bin/dockerd --registry-mirror=https://mirrors.ustc.edu.cn admin123 11613 0.0 0.0 12000 720 pts/0 S+ 19:03 0:00 grep --color=auto dockerd
dockerd 启动时包含了 --registry-mirror=https://mirrors.ustc.edu.cn
参数。这意味着在启动 Docker 时通过命令行显式指定了 --registry-mirror
参数,可能与 daemon.json 文件中的配置发生了冲突。
解决方案:
将该配置项移至 /etc/docker/daemon.json
:
{"registry-mirrors": ["https://mirrors.ustc.edu.cn"] }
然后清除命令行中的 --registry-mirror 参数:
sudo systemctl edit docker
删除掉 --registry-mirror
参数,保存并退出。
重新加载 Docker 服务并重启:
sudo systemctl daemon-reload sudo systemctl restart docker
4.fatal: 无法访问 'https://github.com/nigelpoulton/psweb.git/':Couldn't connect to server
Git 无法连接到 GitHub,最常见原因是 没有正确配置代理
git config --global http.proxy http://127.0.0.1:xxxx
git config --global https.proxy http://127.0.0.1:xxxx
如果还是不行ifconfig看网卡掉线没,这个bug老朋友了,是不是来一下
sudo git clone https://github.com/nigelpoulton/psweb.git 正克隆到 'psweb'... remote: Enumerating objects: 85, done. remote: Counting objects: 100% (39/39), done. remote: Compressing objects: 100% (19/19), done. remote: Total 85 (delta 28), reused 21 (delta 19), pack-reused 46 (from 2) 展开对象中: 100% (85/85), 17.12 KiB | 922.00 KiB/s, 完成.