Buster
Buster

Reputation: 715

How to make local changes reflect in docker container upon saving

I am trying to play with docker and I am still in the process of setting up my development environment. I am trying to set up my container in such a way that I can save changes on the host machine and have them propagated to the container.

I thought by using a volume I was mounting my local ./code directory into the container. I was hoping that by doing this I could run docker-compose up, develop, save and have those changes pushed into the container. Though after saving I am not seeing my changes reflected in the app until I kill it and run docker-compose up again. Am I using the correct concepts? or is this possible with 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

Here is a test repo I am experimenting with. I would like it if I could, with this repo, run docker-compose up. Then make an edit to the response say go from

- Hello world!!!
+ Hello World.

and then be able to refresh localhost:3000 and see the new response.

Results from 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"
        }
    }
]

Upvotes: 1

Views: 3186

Answers (1)

DazWilkin
DazWilkin

Reputation: 40111

Don't RUN mkdir /code in the Dockerfile.

If you're mounting a local directory into /code, then your Dockerfile should just expect it be exist (have been mounted by docker run ...--volume=` or by Docker Compose as you have.

Leave WORKDIR /code.

I thought mounts had to be absolute paths but if ./code then works, you're good.

Upvotes: 1

Related Questions