【Docker】01 安装与入门

1. 基本概念

image: 映像档。只能读取。可以从网路下载或是自己建立。
container: 容器。建立在 image 之上,将多个 image 包装成一个专案环境。

2. 在 Windows 启用相关功能

启动 Windows 程序的 HyperV 、虚拟平台、Windows Subsystem,并重启电脑。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098I015zlHncV.png

3. 安装 Ubuntu

到 Microsoft Store 安装 Ubuntu
https://ithelp.ithome.com.tw/upload/images/20220412/20148098OVqWbN6v8f.png

安装, 然後开启。
接着会自动开启 Ununtu 命令视窗(shell),要设定帐号密码。设定完可以先关闭。或是按 exit 退出。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098BzZU9JbnXx.png

开启 Windows 的 CMD 视窗,执行 wsl -l -v
https://ithelp.ithome.com.tw/upload/images/20220412/20148098UCsrLB4CbC.png

中间那个 Ubuntu 所对应的 wsl 如果是 1,请改成 2
wsl --set-version Ubuntu 2

4. 安装 Docker

可以在 google 搜寻 docker hub,第一笔记录应该就是官网,或是直接使用下面网址
https://hub.docker.com/editions/community/docker-ce-desktop-windows
https://ithelp.ithome.com.tw/upload/images/20220412/20148098z2CcR0Xi7B.png

下载後,档名是 Docker Desktop Installer.exe 。只要点击安装就可以。安装好後重新启动 docker。第一次会有同意条款。选择 Accept 就好。

可能会要求安装更新包
https://ithelp.ithome.com.tw/upload/images/20220412/20148098tBLIfAmT5g.png

预设安装路径
%localappdata%\Docker\wsl
上面的连结会自动开启 Windows 使用者资料夹底下的 wsl
像这样
C:\Users\使用者名称\AppData\Local\Docker\wsl
使用者名称要改成自己的。

应该会看到两个资料夹

C:\Users\使用者名称\AppData\Local\Docker\wsl\
    data
        ext4.vhdx       916MB
    distro
        ext4.vhdx     106MB

进入 docker 程序
Settings / General
把这个打勾 "Use the WSL 2 based engine"
https://ithelp.ithome.com.tw/upload/images/20220412/20148098MYvGYTQlgQ.png

Settings / Resources
把这个打勾 "Enable integration with my default WSL distro"
然後再把 Ubuntu 打开
https://ithelp.ithome.com.tw/upload/images/20220412/201480986mh6eDZOaD.png

如果没有 Ubuntu 的开关,可能是因为没有先去 Microsoft Store 安装 Ubuntu,或者虽然装了,但是 wsl 版本没有切换到 2

5. 安装 Windows Terminal

这个不装也可以。比较 CMD, PowerShell, Windows Terminal,可以参考这篇
https://techwiser.com/command-prompt-vs-powershell-vs-windows-terminal-comparison/
重点: Windows Terminal 可以支援 Linux 子系统,可以用 BASH 指令,例如 ls。还可以分页。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098Bxt5yCGzab.png

6. 确认 Windows 资料夹

开启命令视窗,然後输入 wsl
https://ithelp.ithome.com.tw/upload/images/20220412/20148098INaYzg3mMn.png
预设会进入 C槽的使用者资料夹。可以看到 C 槽挂在 mnt 下面。

在档案总管输入 \\wsl$ ,应该可以看到
docker-desktop
docker-desktop-data
Ubuntu

Ubuntu 里面的 home/[使用者名称] 是家目录。将来安装 container 的时候很有可能会安装在这里面。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098H1wKL0iW5d.png

7. 安装 node

在 node 官网下载安装档。安装过程问到 chocolatey,这个不用勾。很简单的安装。装完要重新启动。

8. 建立专案

建立一个资料夹,可以取名为 hello-docker
在里面建立一个档案,取名为 app.js,内容只要写一行
console.log("Hello Docker!");
开启命令视窗,进到这个资料夹,执行 node app.js
D:\docker\hello-docker\node app.js
应该会看到结果
https://ithelp.ithome.com.tw/upload/images/20220412/20148098fGGnZY6syY.png

9. 建立image

建立 Dockerfile
D:\docker\hello-docker\Dockerfile

FROM node:alpine 
COPY . /app 
WORKDIR /app 
CMD node /app/app.js

执行
docker build -t hello-docker .
注意结尾有一个点

执行完毕之後,到 docker 介面可以看到刚刚建立的 image。档案会把 node 跟其它东西打包进去,所以虽然自己的内容只有一行,仍高达168MB。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098L5FobXqSH3.png

10. 建立container

docker run -d --name hellooo hello-docker
https://ithelp.ithome.com.tw/upload/images/20220412/20148098x4D2xq0TCh.jpg

11. 从当前的container再建立成image
请使用 CMD 视窗,不要用 PowerShell。因为後者做出来的容量可能会加倍。下面的网址讲到,可能是因为 PowerShell 在输出的时候会多2个空白字元。
https://stackoverflow.com/questions/40622162/docker-load-and-save-archive-tar-invalid-tar-header

Powershell emits two byte characters to STDOUT, not one byte characters. If you look in the file you'll notice that the TAR header has nulls between what should be the correct header (and in the rest of the file). This explains why the file is twice the size.

提交 image
格式:docker commit 容器名称 使用者名称/映像档名称
docker commit hellooo newhelloo

将映像档存成 tar 档
docker save newhelloo > newhelloo.tar
https://ithelp.ithome.com.tw/upload/images/20220412/20148098ap5u5IXmyn.jpg

12. 汇入
把 docker 里面的 newhello 这个 image 删掉。
执行
docker load --input newhelloo.tar
这时候可以看到 newhello 又出现在 image 里面了。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098LzkyrLhzaP.jpg

参考
Windows 上的 Docker 引擎
https://docs.microsoft.com/zh-tw/virtualization/windowscontainers/manage-docker/configure-docker-daemon

Docker Tutorial for Beginners
https://www.youtube.com/watch?v=pTFZFxd4hOI


<<:  <新手复习向> <CSS学习第1集/>:{简单介绍与常见属性;}

>>:  [自学笔记]Routing(MVC)路由

Day4-自制网站卷轴(中)_想要更多造型

今天开始来写出自制的卷轴,构想是这样子的 根据构想先把框架写出来 <div class=&qu...

[Golang]Go语言的关键字少,共25个

break、switch、case、default、func、interface、defer、 go...

【Day15】状态机的撰写

什麽是状态机呢? 状态机,其实是有限状态机(finite-state machine(FSM))的简...

NIST与云相关的准则

-云计算概念参考模型(来源:NIST) NIST SP 500-291v1(云路线图) NIST ...

学习笔记:一起进入 PixiJS 的世界 (六)

上一篇有提到可以利用PIXI.Ticker将定期渲染的机制加进场景,建立基础的小动画,接下来就来试试...