Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change content in node project run through docker

I have an angularjs application, I'm running using docker. The docker file looks like this:-

FROM node:6.2.2

RUN npm install --global gulp-cli && \
    npm install --global bower

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
COPY bower.json /usr/src/app/
RUN npm install && \
    bower install -F --allow-root --config.interactive=false
COPY . /usr/src/app

ENV GULP_COMMAND serve:dist

ENTRYPOINT ["sh", "-c"]
CMD ["gulp $GULP_COMMAND"]

Now when I make any changes in say any html file, It doesn't dynamically loads up on the web page. I have to stop the container, remove it, build the image again, remove the earlier image and then restart the container from new image. Do I have to do this every time? (I'm new to docker, and I guess this issue is coz my source code is not put into volume, but I don't know how to do it using docker file)

like image 742
Shubham Gupta Avatar asked Oct 15 '25 18:10

Shubham Gupta


2 Answers

You are correct, you should use volumes for stuff like this. During development, give it the same volumes as the COPY directories. It'll override it with whatever is on your machine, no need to rebuild the image, or even restart the container. Perfect for development.

When actually baking your images for production, you remove the volumes, leave the COPY in, and you'll get a deterministic container. I would recommend you read through this article here: https://docs.docker.com/storage/volumes/.

In general, there are 3 ways to do volumes.

  1. Define them in your dockerfile using VOLUME.

    Personally, I've never done this. I don't really see the benefits of this against the other two methods. I believe it would be more common to do this when your volume is meant to act as a permanent data-store. Not so much when you're just trying to use your live dev environment.

  2. Define them when calling docker run.

    docker run ... -v $(pwd)/src:/usr/src/app ...
    

    This is great, cause if your COPY in your dockerfile is ./src /usr/src/app then it temporarily overrides the directory while running the image, but it's still there for deployment when you don't use -v.

  3. Use docker-compose.

    My personal recommendation. Docker compose massively simplifies running containers. For sake of simplicity just calls docker run ... but automates the arguments based on a given docker-compose.yml config.

    Create a dev service specifying the volumes you want to mount, other containers you want it linked to, etc. Then bring it up using docker-compose up ... or docker-compose run ... depending on what you need.

Smart use of volumes will DRAMATICALLY reduce your development cycle. Would really recommend looking into them.

like image 87
SCB Avatar answered Oct 18 '25 13:10

SCB


Yes, you need to rebuild every time the files change, since you only modify the files that are outside of the container. In order to apply the changes to the files IN the container, you need to rebuild the container.

Depending on the use case, you could either make the Docker Container dynamically load the files from another repository, or you could mount an external volume to use in the container, but there are some pitfalls associated with either solution.

like image 36
Qiong Wu Avatar answered Oct 18 '25 11:10

Qiong Wu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!