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的使用问题集
host机器上安装的node版本是6.10.3。下载了一个ng项目的代码,需要node 8.9+。
要求:
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
xxxxxxxxxx
## 指定这个镜像的基础是什么,我们选择了node: 8.9.3这个版本作为基础镜像
FROM node:8.9.3
##安装node相关依赖
RUN \
npm install yarn -g \
yarn global add nrm && \
nrm use taobao
## 创建一个目录
RUN mkdir /data
## 指定命令运行的目录
WORKDIR /data
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 的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
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:
安装stable版本的docker for windows,在启动时,如果选择的是Linux Container,同时打开Hyper-V Manager,可以看到docker在新建一个叫MobyLinuxVM的虚拟机。
windows 10 原生支持container,不需要linux虚拟机了:
docker exec -it xxxx bash
连接后台运行的container的bashdocker container logs
命令。docker run
之后运行ctrl+c
,会停止container吗?
dock er run
时如果加了-t -i
(必须都加), ctrl+c
就会停止container。单独加其中一个没事,container还在后台。docker run 中 -v 加载host上的目录
使用如上方法启动一个容器,在虚拟机的/mnt目录下怎么也无法加载D:\docker-data\exchange\下面的文件。但是启动容器的时候正常启动,没有报任何错误,折腾了好一阵。但是昨天我用这个方法是能正常挂载,能正常读取到宿主机的文件的。 后来才发现,是自己昨天最后改了自己电脑的密码,要重新验证才可以正常读取。(不知道是不是docker的一个坑,明明之前验证过的密码已经变动了,执行上面的命令也不要求重新认证,也不报错,坑)
使用如下方法先清除验证,打开docker的settings窗口,在如下页面点击reset credentials,再勾选上要挂载的目录所在磁盘,重新输入密码(这里不知道为何非要密码,我的电脑一直都不设置密码,为了用docker特设置了一个)
作者:ap10062kai 来源:CSDN 原文:https://blog.csdn.net/ap10062kai/article/details/79232582 版权声明:本文为博主原创文章,转载请附上博文链接!
docker run 中-p 进行端口映射
启动container:
这样不行(在host上访问http://127.0.0.1:8105 返回的ERR_EMPTY_RESPONSE):
用docker exec -it e22a6 /bin/bash
连进去,然后curl -vvv http://localhost:8100
,有正确的响应。
后来container中改成这样:
host中访问http://127.0.0.1:8105 返回了正确的结果。
比如,api部署在host的12021,而ui部署在container里面,ui需要访问api。
注意这里可能是两种情况: 1)请求发生在服务器端,比如 在node里面用request来访问api:
xxxxxxxxxx
var url = "http://172.17.0.1:3000"; //不OK,172.17.0.1是用 ip route|awk '/default/ { print $3 }' 获取到的
var url = "http://host.docker.internal:3000"; // OK
var url = "http://localhost:3000"; // 不OK
2)请求发生在浏览器里面,比如一个ng项目,是浏览器里面的js去发送ajax请求访问api。 也是OK的。注意:很有可能api里面设置了跨域的域名限制,如果在-p时host端口和原始端口不同,此时api接收到的是host端口发来的请求,可能会被拒绝。举例:-p 4201:4200。api上之运行来自localhost:4200的范围,当使用localhost:4201时,跨域过不去。
ENOENT errors, .staging
之类的错误。
一直都没解决。最后是用yarn i
解决的。
.staging
? · Issue #13528 · npm/npmTODO.