TODO场景说明安装docker从dockerfile开始问题:docker run 如何执行多个命令(比如先npm i 然后npm start)名词解释 :image,container,host, linux containers 和 windows containerscontainer运行时的输入输出在哪里host上的文件如何共享给containerwindows 10 修改登录密码后无法访问host文件host访问container上的某个端口Ionic 4 app 在docker container中运行后,无法在host上访问container里面的app访问host的某个端口Ionic 4 app在docker container中执行npm i报错TODO:host上改data里面的东西,container中的app自动重启TODO:docker compose的使用问题集

TODO

  1. angular cli 自动complie和live reload问题。目前ng项目在windows和mac上都没法live reload。
  2. docker compose的使用。

场景说明

host机器上安装的node版本是6.10.3。下载了一个ng项目的代码,需要node 8.9+。

要求:

  1. 不想在host上安装nvm for windows,太麻烦
  2. 想要方便地切换node版本
  3. 代码的编辑仍然在host中进行
  4. 最好能有代码自动compile 和 live reload

安装docker

  1. Windows 10上很方便: Install Docker for Windows
  2. Windows 7 上麻烦一些,需要 Docker Toolbox

Windows 10 安装遇到的一个问题:Hardware assisted virtualization and data execution protection must be enabled in the BIOS : Win10 : virtualization enabled but Docker does not see it - will not load · Issue #412 · docker/for-win

从dockerfile开始

build image: docker build -t node-8.9.3 . check the image: docker image ls create a container from the image: docker run -v "D:/GitRepo/codematters.justbeta/angular":/data -p 4202:4200 node-8.9.3 npm start ctrl+c退出cmd,container仍然在后台运行, docker container ls能看到。 再次连接container获取console输出的信息:docker container logs -f CONTAINER_ID

问题:docker run 如何执行多个命令(比如先npm i 然后npm start)

docker run 的cmd没法写多个命令,但是在dockerfile中的CMD可以这样写:CMD ["sh","-c","npm i && npm start"]

这样npm i的包也会保留在host的文件夹中 但是这样效率比较低,每次新建一个container都要重新去npm i(虽然就是第一次要安装,第二次开始就npm i就是检查了)

Docker Succinctly, P24:

Everything except the final CMD instruction gets executed during the build. When you run a container from the image, Docker uses the CMD instruction to tell it how to start—in this case by running the echoserver.sh script.

可以在先在host上的data文件夹中先npm i,然后在docker run中直接npm start

名词解释 :image,container,host, linux containers 和 windows containers

This is why Docker containers can run so efficiently—they use the underlying operating system kernel of the host machine so that processes inside the container are actually running on the host. For a host, running multiple containers is the same as running multiple processes (unlike virtual machines, for which each VM has its own kernel and a hypervisor running on the host in order to translate between the virtual kernel and the real kernel). That is why you can’t run Linux containers on Windows or run Windows containers on Linux. A container using Ubuntu as its base image needs to run on a Linux machine so that when the container launches an executable, the host is capable of running it.

Docker and Linux Containers on Windows, with or without Hyper-V Virtual Machines - Scott Hanselman:

img

安装stable版本的docker for windows,在启动时,如果选择的是Linux Container,同时打开Hyper-V Manager,可以看到docker在新建一个叫MobyLinuxVM的虚拟机。

windows 10 原生支持container,不需要linux虚拟机了:

img

container运行时的输入输出在哪里

  1. 输入:container底下是一个linux或者windows,没有gui,只有命令行。所以输入方式就是bash或者powershell
  2. 输出也是在cmd上:用 docker exec -it xxxx bash连接后台运行的container的bash
  3. 如何让重新获取输出?第一次能看见ng的输出,ctrl-c退出之后,再进入bash,ng的输出不见了。 --关闭后再进入获取console输出的信息用docker container logs命令。
  4. docker run之后运行ctrl+c,会停止container吗? dock er run时如果加了-t -i(必须都加), ctrl+c就会停止container。单独加其中一个没事,container还在后台。

host上的文件如何共享给container

docker run 中 -v 加载host上的目录

windows 10 修改登录密码后无法访问host文件

使用如上方法启动一个容器,在虚拟机的/mnt目录下怎么也无法加载D:\docker-data\exchange\下面的文件。但是启动容器的时候正常启动,没有报任何错误,折腾了好一阵。但是昨天我用这个方法是能正常挂载,能正常读取到宿主机的文件的。 后来才发现,是自己昨天最后改了自己电脑的密码,要重新验证才可以正常读取。(不知道是不是docker的一个坑,明明之前验证过的密码已经变动了,执行上面的命令也不要求重新认证,也不报错,坑)

使用如下方法先清除验证,打开docker的settings窗口,在如下页面点击reset credentials,再勾选上要挂载的目录所在磁盘,重新输入密码(这里不知道为何非要密码,我的电脑一直都不设置密码,为了用docker特设置了一个)img

作者:ap10062kai 来源:CSDN 原文:https://blog.csdn.net/ap10062kai/article/details/79232582 版权声明:本文为博主原创文章,转载请附上博文链接!

host访问container上的某个端口

docker run 中-p 进行端口映射

Ionic 4 app 在docker container中运行后,无法在host上访问

启动container:1570525127361

这样不行(在host上访问http://127.0.0.1:8105 返回的ERR_EMPTY_RESPONSE):

1570525058319

docker exec -it e22a6 /bin/bash连进去,然后curl -vvv http://localhost:8100,有正确的响应。

后来container中改成这样:1570525091350

host中访问http://127.0.0.1:8105 返回了正确的结果。

container里面的app访问host的某个端口

比如,api部署在host的12021,而ui部署在container里面,ui需要访问api。

注意这里可能是两种情况: 1)请求发生在服务器端,比如 在node里面用request来访问api:

2)请求发生在浏览器里面,比如一个ng项目,是浏览器里面的js去发送ajax请求访问api。 也是OK的。注意:很有可能api里面设置了跨域的域名限制,如果在-p时host端口和原始端口不同,此时api接收到的是host端口发来的请求,可能会被拒绝。举例:-p 4201:4200。api上之运行来自localhost:4200的范围,当使用localhost:4201时,跨域过不去。

Ionic 4 app在docker container中执行npm i报错

ENOENT errors, .staging之类的错误。

一直都没解决。最后是用yarn i解决的。

TODO:host上改data里面的东西,container中的app自动重启

  1. 用nodemon:照着这里进行配置https://github.com/remy/nodemon#application-isnt-restarting
  2. ng live reload:还没试成功。

TODO:docker compose的使用

TODO.

问题集