簡體   English   中英

Docker卷不持久更改

[英]Docker Volumes not persisting changes

認為這是一個兩部分錯誤,但這里的背景故事是我試圖學習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圖像 - 這對我來說是一個學習練習,所以我從頭開始做。 未來的可能性我將使用其中一個預制的泊塢窗圖像。

穿過建築物我沒有收到任何錯誤。 我收到一個警告,但是我在每個容器上看到的是因為我正在使用模擬Linux機器的Windows機器。

現在,在我的工作中,我們有一些Dockerfile容器用於參考,我查找的一件事是我在編輯器中進行的文件更改是如何保存到復制到容器中的文件中的。 這似乎是通過docker-compose.yml文件中的volume選項。 所以,我試圖自己創造一個。 請注意,我已經對dockerfile進行了評論,以了解我對它們的理解。 如果我錯了,或者說完全正確,請糾正我,因為我想以正確的方式學習。

泊塢窗,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');
});

的package.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,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容器的更多信息,請訪問此Wiki 由於您在Dockerfile上運行npm install,因此必須確保在mount /node_modules -compose mount /node_modules為數據卷。

所以要克服這一點,你需要:

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

然后在docker中撰寫yaml文件包括:

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

如此處所述 ,請確保您在docker-compose中安裝的卷不會完全覆蓋您在映像中的內容(使用Dockerfile ADD .指令)

還可以查看類似的練習aherve / simple-dockerized-dev-env issue 2

這是因為hello服務正在丟失node_modules文件夾。 它需要添加到docker-compose.yml文件中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM