gpt4 book ai didi

Docker 卷不持久更改

转载 作者:行者123 更新时间:2023-12-02 18:54:00 25 4
gpt4 key购买 nike

我认为这是一个由两部分组成的错误,但这里的背景故事是我正在尝试学习 Docker。我想学习它以便在 Google Cloud Platform 上进行一些测试,并且我已经使用 Docker 文件创建了镜像,但是文件在我的机器上或容器内持久存在更改时遇到了问题。

Dockerfile(以备不时之需)

FROM ubuntu:latest
# Use Ubuntu as the base
RUN apt-get -y update && apt-get -y upgrade && apt-get -y install curl git
# update & updgrade all the apps. Also install cURL and Git (needed for node, apparently?)
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
# download the node 8.x setup (don't know the words on that one) file
RUN apt-get install -y nodejs
# Install NodeJS
RUN mkdir /var/www/ && mkdir /var/www/app/
# create the directory /var/www/ and then, inside of that directory, create the var/www/app/
WORKDIR /var/www/app/
# our work directory is the app directory
VOLUME /var/www/app
ADD . .
# add everything in the current directory on our file system to the work directory in our container
RUN npm install
# node apps need to have their dependencies installed
CMD ["npm", "start"]
#The command used when the docker container is run.

请注意,我确实知道已经存在一些 NodeJS 图像——这对我来说是一个学习练习,所以我是从头开始做的。将来我很可能会使用其中一个预先构建的 docker 镜像。

穿过大楼我没有收到任何错误。 我确实收到了警告,但这是我在每个容器上看到的警告,因为我正在使用模拟 linux 机器的 Windows 机器。

现在,在我的工作中,我们有一些 Dockerfile 容器用作引用,我查找的其中一件事是我在编辑器中所做的文件更改如何保存到复制到容器中的文件中。这似乎是通过 volume docker-compose.yml 中的选项文件。所以,我试图自己创造一个。请注意,我已经评论了 dockerfile 关于我对它们的理解。如果我错了,或者注意完全正确,请纠正我,因为我想以正确的方式学习这一点。

docker -compose.yml
version: "3"
#what docker-compose version are we using? 3.
services:
#describes the services to be build when this is composed up
web:
# we will have a web service -- in the future I will obviously need to have some form of database
build: ./
#where do we build this image from? the Dockerfile in this directory
image: dockertest_node
#what image will be named "dockertest_node" on my machine
container_name: node_test
#this container (once build), however, will be titled "node_test" to make it easier to work with and run docker exec commands
volumes:
- ./:/var/www/app
#volumes, as I understand them, are a map from acutal file system -> containerized file system
ports:
- "8080:8080"
# we expose port 8080 from my local machine to port 8080 on the container's machine.

截至目前,这是该项目中的两个文件。这些都是简单的事情,只是让我开始。

index.js
const express = require('express');
const app = express();

app.get('/', (req,res)=>{
res.send('hello world, from express!');
});

app.listen(8080, (err)=>{
if(err) {
console.error(err);
return 1;
}
console.log('server started');
});

包.json
{
"name": "docker-test",
"version": "0.0.0",
"description": "Testing",
"dependencies": {
"express": "^4.0.0",
"bootstrap":"4.0.0-beta",
"jquery": "^3.0.0",
"popper.js":"^1.11.0"
},
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC"
}

其他信息

Docker 版本(从 docker -v 检索)

Docker version 17.09.1-ce, build 19e2cf6



我在 Windows 10 Pro 上使用 Docker for Windows

现在,如果我尝试运行 docker-compose up我收到以下错误
Starting node_test ...
Starting node_test ... done
Attaching to node_test
node_test |
node_test | > docker-test@0.0.0 start /var/www/app
node_test | > node index.js
node_test |
node_test | module.js:538
node_test | throw err;
node_test | ^
node_test |
node_test | Error: Cannot find module 'express'
node_test | at Function.Module._resolveFilename (module.js:536:15)
node_test | at Function.Module._load (module.js:466:25)
node_test | at Module.require (module.js:579:17)
node_test | at require (internal/module.js:11:18)
node_test | at Object.<anonymous> (/var/www/app/index.js:1:79)
node_test | at Module._compile (module.js:635:30)
node_test | at Object.Module._extensions..js (module.js:646:10)
node_test | at Module.load (module.js:554:32)
node_test | at tryModuleLoad (module.js:497:12)
node_test | at Function.Module._load (module.js:489:3)
node_test | npm ERR! code ELIFECYCLE
node_test | npm ERR! errno 1
node_test | npm ERR! docker-test@0.0.0 start: `node index.js`
node_test | npm ERR! Exit status 1
node_test | npm ERR!
node_test | npm ERR! Failed at the docker-test@0.0.0 start script.
node_test | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
node_test | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
node_test |
node_test | npm ERR! A complete log of this run can be found in:
node_test | npm ERR! /root/.npm/_logs/2017-12-25T22_47_55_509Z-debug.log
node_test exited with code 1

在我在 .yml 中添加卷相关信息之前没有显示文件和 Dockerfile 本身。但是,我可以使用命令 docker exec -p 8080:8080 -it dockertest_node bash 登录到容器。 , 我可以手动运行 npm start没有问题。

但是如果当我在容器中时,我会做 touch something.txt该文件仅在容器内创建;反之亦然,我对 index.js 或 package.json 文件的任何更改都没有进入容器。

感谢您指出我可能犯的任何错误;我真的在努力学习如何使用 Docker。

最佳答案

您收到此错误是因为主机和容器之间的第二个卷映射确实覆盖了现有数据。有关启动 docker 容器的更多信息,请访问 this wiki .由于您在 Dockerfile 上运行 npm install,因此您必须确保 mount /node_modules作为 docker-compose 中的数据卷。

所以要克服这个问题,你需要:

VOLUME /var/www/app
ADD . /var/www/app
RUN npm install

然后在 docker compose yaml 文件中包含以下内容:

volumes:
- ./: /var/www/app
- /var/www/app/node_modules

关于Docker 卷不持久更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47973831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com