Docker 入门练习 1

333 阅读3分钟

1. 开始之前 | Before getting-start

docker 官网 Get started 的学习记录,中文均为自翻,未经校准,原网址:docs.docker.com/get-started…

前置条件:

1. 安装 docker desktop application,完成 application 中的欢迎指南

2. 有 GitHub 账号会创建 git 远程仓库

3. 概念

- Image:Image 文件

Image provides an isolated filesystem to run a container. It contains everything to run an app, including all dependcies, configurations, scripts, binaries, environmental viriables default command to run and other metadata.

Image 文件给容器的运行提供一个与外界相隔绝的系统。

- Container:容器

A process on your machine that has been isolated from all other process on host.

通过 Image 文件,可以生成正在运行的容器实例/容器文件。


2. 打样 | Sample application

get the app

 

下载地址:github.com/docker/gett…

(记录:app 目录命令行 pull 下载失败,返回到 app 的 git repo 中点击压缩包下载的)

下载打开后,可以看到 `package.json` 文件和 `src` `spec` 两个目录。

build the app's container image

在 `package.json` 同目录中创建 `Dockerfile` 文件,内容如下

# syntax=docker/dockerfile:1
 FROM node:12-alpine
 RUN apk add --no-cache python g++ make
 WORKDIR /app
 COPY . .
 RUN yarn install --production
 CMD ["node", "src/index.js"]

在 app 目录下输入命令如下

docker build -t getting-started .

`docker build` :从 Dockerfile build image 文件

`-t` :参数 -t flag 指定(tag) getting-started 标签

`.` :结尾的点表示在当前目录下查找 Dockerfile

start an app container

1. 通过 `docker run` 命令运行指定名称的 image 文件

 docker run -dp 3000:3000 getting-started

`-d` :参数 run container in detached mode (background) flag,在隔绝模式下运行容器

`-p`:参数 map port flag

single flag 可以合并,所以 -d 和 -p 可以合并成 -dp

`3000:3000` :创建映射(map),让容器/ container 的 3000 端口映射本机/ host 的 3000 端口

2. localhost 3000 端口得到如下图的sample

3. 可以自行添加事项

4. 在 docker desktop 中可以看到正在使用的 image 文件和正在运行的 container

回顾 | recap

学到了:

- build container image

- create a Dockerfile

- start the container and see the running app from localhost

下一步:

- 修改 app

- 用新的 image 运行这个应用


3. 更新应用| Update the application

更新源代码 | update the source code

1. 在 `src/static/js/app.js` 文件中更新代码

-                <p className="text-center">No items yet! Add one above!</p>
+                <p className="text-center">You have no todo items yet! Add one above!</p>

2.  build 更新版本的 image 文件

docker build -t getting-started .

3. 开一个新的 container

docker run -dp 3000:3000 getting-started

4. 然后。。。出现了报错!

原因是:当旧的容器还在运行 (run) 时,新的容器无法 start。容器在使用 3000 端口的时候,只能在机器中 process 一个

解决办法是:移除旧的容器

移除旧容器 | replace the old container

移除 container 之前要先终止他的运行。

移除的方法有两种:使用 CLI(Command Line Interface) ,或在 docker dashboard 里移除。

1. CLI 移除 container

`docker ps`:拿到 container 的 ID

2. 拿到 ID 后,终止指定 ID 的 container 的运行

 # Swap out <the-container-id> with the ID from docker ps
 docker stop <the-container-id>

如图,名为 peaceful_elbakyan,ID 为 35c7440c0ed7 的 container 被终止了

从 docker dashboard 里也可以看到 ⬇️

3. 终止之后再移除

 docker rm <the-container-id>

这些操作也可以在 docker dashboard 中通过点击图标实现~

启动更新后的 app 容器 | start the updated app container

删除之后,就可以重新运行 `docker run`命令启动更新的容器了

docker run -dp 3000:3000 getting-started

3000端口显示如下

回顾 | recap

下一步:

- 希望启动更新后,容器能保留原来的条目

- 希望无需 rebuild and start 就能看到改动的代码更新