代码之家  ›  专栏  ›  技术社区  ›  Jhecht

Docker卷未持久化更改

  •  1
  • Jhecht  · 技术社区  · 7 年前

    认为 这是一个由两部分组成的错误,但背后的故事是,我正在尝试学习Docker。我想学习它,以便在谷歌云平台上进行一些测试,我用一个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编写。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.
    

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

    指数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版本17.09.1-ce,内部版本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 文件仅在容器内创建;反之亦然,我对索引没有任何更改。js或包。json文件将其放入容器中。

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

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ben Schmeltzer    7 年前

    出现此错误是因为主机和容器之间的第二个卷映射覆盖了现有数据。有关启动docker容器的更多信息,请访问 this wiki . 由于您正在Dockerfile上运行npm安装,因此必须确保 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
    
        2
  •  1
  •   VonC    7 年前

    mentioned here ,确保装载在docker compose中的卷没有完全覆盖您在映像中放置的内容(使用 ADD . 您的 Dockerfile )

    还可以查看类似的练习 aherve/simple-dockerized-dev-env issue 2

    这是因为 hello 服务正在丢失node\u modules文件夹。需要将其添加到 docker-compose.yml 文件

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