保存时如何使本地更改反映在Docker容器中

时间:2019-07-14 18:16:41

标签: docker docker-compose

我正在尝试与docker一起玩,但仍在设置开发环境。我试图以一种我可以将更改保存在主机上并将其传播到容器的方式来设置容器。

我以为通过使用volume我正在将本地./code目录安装到容器中。我希望通过这样做可以运行docker-compose up,进行开发,保存并将这些更改推送到容器中。尽管保存后,直到我将其终止并再次运行docker-compose up后,我的更改才会反映在应用程序中。我使用的是正确的概念吗?还是可以在docker上实现?

Dockerfile

FROM node:10.13-alpine
ENV NODE_ENV production
# RUN mkdir /code
WORKDIR /code

docker-compose.yml

version: '2.1'

services:
  express-docker-test:
    build: code
    # command: npm install --production --silent && mv node_modules ../
    volumes:
      - E:\git\express-docker-test\code\:/code/
    environment:
      NODE_ENV: production
    expose:
      - "3000"
    ports:
      - "3000:3000"
    command: npm start

这是一个测试repo I am experimenting with。如果可以,可以使用此仓库运行docker-compose up。然后对响应进行编辑,说从

- Hello world!!!
+ Hello World.

,然后能够刷新localhost:3000并查看新的响应。

docker inspect express-docker-test_express-docker-test的结果

[
    {
        "Id": "sha256:791e8fbd5c871af53a37f5e9f5058e423f8ddf914b09f21ff1d80a40ea4f142f",
        "RepoTags": [
            "express-docker-test_express-docker-test:latest"
        ],
        "RepoDigests": [],
        "Parent": "sha256:e87553281ff90e49977fb6166c70c0e5ebf7bb98f0ae06468d7883dc0314c606",
        "Comment": "",
        "Created": "2019-07-14T19:15:47.1174538Z",
        "Container": "8426c3db78c23c1cfc594c500ce77adf81ddd43ca52ca58456ba5b49ec60fee9",
        "ContainerConfig": {
            "Hostname": "8426c3db78c2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NODE_VERSION=10.13.0",
                "YARN_VERSION=1.10.1",
                "NODE_ENV=production"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) WORKDIR /code"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e87553281ff90e49977fb6166c70c0e5ebf7bb98f0ae06468d7883dc0314c606",
            "Volumes": null,
            "WorkingDir": "/code",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "18.09.2",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NODE_VERSION=10.13.0",
                "YARN_VERSION=1.10.1",
                "NODE_ENV=production"
            ],
            "Cmd": [
                "node"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e87553281ff90e49977fb6166c70c0e5ebf7bb98f0ae06468d7883dc0314c606",
            "Volumes": null,
            "WorkingDir": "/code",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 70255071,
        "VirtualSize": 70255071,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/8a0293f507ec80f68197b3bcb49b12e1a61c08f122a8a85060ab2d766d881a93/diff:/var/lib/docker/overlay2/a159e965ecf6e91533397def52fb1b3aef900c9793f933dc5120eed2381a37f4/diff:/var/lib/docker/overlay2/49f6493daf037bc88f9fb474ae71e36f13b58224082ff1f28ee367c795207c8d/diff",
                "MergedDir": "/var/lib/docker/overlay2/9c4a3691e175f15585034acf7146ce5601f9e9d9c9cb60023bd348172116ae5c/merged",
                "UpperDir": "/var/lib/docker/overlay2/9c4a3691e175f15585034acf7146ce5601f9e9d9c9cb60023bd348172116ae5c/diff",
                "WorkDir": "/var/lib/docker/overlay2/9c4a3691e175f15585034acf7146ce5601f9e9d9c9cb60023bd348172116ae5c/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:df64d3292fd6194b7865d7326af5255db6d81e9df29f48adde61a918fbd8c332",
                "sha256:387bc77dd3f21547b74752dd03f6018b5d750684c832c39cd239704052ce366e",
                "sha256:2faeaaebb1134fe62e2cc1603a761301e281c04a8e2e36ff2ac1005f7c06780f",
                "sha256:dfb2bf93a77a1907921d8f1622e831fa31c924c6c612e593b8937f95e42a0afa"
            ]
        },
        "Metadata": {
            "LastTagTime": "2019-07-14T19:31:00.4646905Z"
        }
    }
]

1 个答案:

答案 0 :(得分:0)

不要在Dockerfile中RUN mkdir /code

如果要将本地目录装载到/code中,那么您的Dockerfile应该只是期望它存在(已被docker run ...-volume =`或Docker Compose装载。

离开WORKDIR /code

我认为坐骑必须是绝对路径,但是如果./code可以工作,那就很好。