简单了解docker
如果你恰好有多台设备,windows电脑,本地搭建的虚拟机,云服务器甚至电视盒子,或者叫生产环境,测试环境,开发环境。在排查错误的时候,有一个重要的思想,控制变量法
,在不同地方跑的代码,一旦出现报错起码保证他们的运行环境是一致的,这是其一,其二,起手就是搞环境,你也不想每台设备上搭建环境的动作都做一遍,比如我在云服务器上搭建了一套Linux环境,在本地windows上也要保持一样的环境,就可以两个系统都安装docker,只需复制
过来就执行就能用。复制过来的东西就是镜像,在跑起来的镜像就叫做容器,镜像更像是食材,不论谁做的菜端上来总归能吃就行,能吃的这个就看作是容器。
接下来的操作可能要理清一些东西,跑镜像的这台机器,叫宿主机。
宿主机和容器的网络关系(此宿主机为windows系统)
C:\Users\specialwu3>ipconfig
Windows IP 配置
无线局域网适配器 WLAN:
连接特定的 DNS 后缀 . . . . . . . :
IPv4 地址 . . . . . . . . . . . . : 192.168.1.9
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.1.1
- 在容器中的IP
[root@1858af0f30e5 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 1858af0f30e5
容器和虚拟机
– 官网给的解释
Docker is an open platform for developing, shipping, and running applications.
Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
With Docker, you can manage your infrastructure in the same ways you manage your applications.
By taking advantage of Docker's methodologies for shipping, testing, and deploying code,
you can significantly reduce the delay between writing code and running it in production.
Docker是一个用于开发、部署和运行应用程序的开放平台。
Docker使你能够将应用程序与基础设施分离,从而可以快速交付软件。
通过Docker,你可以以与管理应用程序相同的方式管理基础设施。
通过利用Docker的代码交付、测试和部署方法,
你可以大大缩短编写代码和在生产环境中运行代码之间的延迟。
一个简单的示例
- 在Linux上安装docker (同理在windows系统上安装docker-desk为另一种方式)
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
- 拉取镜像 (通常拉取这个镜像测试docker是否安装到位)
[root@specialwu3 ~]# docker pull hello-world
[root@specialwu3 ~]# docker run hello-world
Hello from Docker!
...
- 查看镜像
[root@specialwu3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
- 制作一个简单的镜像
- 创建一个名为
Dockerfile
的文件 在当前目录中构建镜像必须有的
# FROM 是 Dockerfile 中的关键字,表示接下来要指定一个基础镜像
#alpine:latest:alpine 是一个轻量级的 Linux 发行版,latest 是标签(tag),表示使用该发行版的最新版本
#alpine 是一个精简的 Linux 发行版,非常适合用于构建轻量级和小型的 Docker 镜像。
FROM alpine:latest
# 在容器中的工作目录 /app
WORKDIR /app
# 当前目录下的文件复制到容器 app目录下 /app
COPY . /app
# Define environment variable
ENV NAME specialwu
# Run the command when the container starts
CMD ["sh", "-c", "echo Hello $NAME && cat test.txt"]
- 构建镜像 (-t 指定镜像名 镜像文件所在路径)
docker build -t hello-mytest /opt/makeimages/
- 运行容器
[root@specialwu3 ~]# docker run hello-mytest
Hello specialwu
宿主机上的文件
目录说明
[root@specialwu3 makeimages]# ll
total 8
-rw-r--r-- 1 root root 346 Jan 24 00:34 Dockerfile
-rw-r--r-- 1 root root 22 Jan 24 00:26 test.txt
[root@specialwu3 makeimages]# cat test.txt
宿主机上的文件
其他节点的机器如何使用这个镜像
有两种仓库选择,公共仓库官方地址(hub.docker.com
),私有仓库,本次传到公有仓库,如果你想要使用,也可直接拉取
docker commit -m '第一版' -a 'specialwu' hello-mytest dockertest:001
docker tag bbb65ee4be77 specialwu/dockertest:001
docker push specialwu/dockertest:001
通过 docker commit 创建一个新的镜像,基于现有容器的状态,并添加了说明和作者信息。
通过 docker tag 为该镜像添加一个新的标签。
通过 docker pull 从本地推到远程仓库。
- 拉取
[root@specialwu3 ~]# docker pull specialwu/dockertest:001
001: Pulling from specialwu/dockertest
Digest: sha256:1ba296227d4f7c7a41d09a3415149b00afa4d77b1d0a8e024aa5c9d66f624952
Status: Image is up to date for specialwu/dockertest:001
docker.io/specialwu/dockertest:001
容器里的文件系统和宿主机文件系统时隔离的,容器要读取宿主机的文件要么选择文件复制进容器,要么看一看数据卷的概念,使两个文件系统打通
docker cp /mnt/specialwu2/node-v20.10.0.tar.gz hello-mytest:/opt/
- 运行容器的两种方式
- 交互式执行方式
docker run -itd --name hello-mytest -p 66:61 --privileged=true hello-mytest:latest /sbin/init
-itd:这三个选项分别表示以交互模式(-i)、分离模式(-d)启动容器,并分配一个伪终端。这样容器就会在后台运行,并且你可以进入容器的交互式 Shell。
--name hello-mytest:指定容器的名称为 hello-mytest。
-p 66:61:将容器的端口 61 映射到主机的端口 66。这样可以通过主机的端口 66 访问容器内的服务。
--privileged=true:以特权模式运行容器,即具有更高的权限,可以执行一些特权操作。
hello-mytest:latest:指定要运行的镜像及其标签。
/sbin/init:启动容器时执行的命令,通常是指定容器启动的进程。
[root@specialwu3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
722faf5bb5ab hello-mytest:latest "/bin/sh" 31 minutes ago Up 31 minutes 0.0.0.0:66->61/tcp, :::66->61/tcp
简单理解为进入容器中
[root@specialwu3 ~]# docker exec -it hello-mytest /bin/sh
/app # cat test.txt
宿主机上的文件
/app #
- 简化方式
docker run hello-mytest
这种方式是一个简化的运行容器的命令,它使用默认选项,
不指定额外的参数。在这种情况下,Docker 将按照默认设置运行容器,不会使用交互模式、不会指定容器名称、端口映射等