Home > Back-end >  Adding varnish to existing node Dockerfile
Adding varnish to existing node Dockerfile

Time:11-07

I have the following Dockerfile

FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "app.js" ]

Now I would like to add the Varnish cache and consider this repo docker-varnish how can I organise both together?

UPDATE 1

Once I run this command docker compose build it show following information, but I don't see anything related to varnish

[ ] Building 4.6s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 362B 0.0s => [internal] load .dockerignore 0.1s => => transferring context: 174B 0.0s => [internal] load metadata for docker.io/library/node:10-alpine 4.0s => [internal] load build context 0.1s => => transferring context: 21.58kB 0.0s => [1/6] FROM docker.io/library/node:10-alpine@sha256:dc98dac24efd4254f75976c40bce46944697a110d06ce7fa47e7268470cf2e28 0.0s => CACHED [2/6] RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
0.0s => CACHED [3/6] WORKDIR /home/node/app 0.0s => CACHED [4/6] COPY package*.json ./ 0.0s => CACHED [5/6] RUN npm install 0.0s => [6/6] COPY --chown=node:node . . 0.1s => exporting to image 0.1s => => exporting layers 0.0s => => writing image sha256:7eec4ec76dbff93f8b0ebc6e03051331709d5f55a641be379a3e00697eabde70 0.0s => => naming to docker.io/library/test_project_node

Am I doing things right?

CodePudding user response:

You could use docker compose to orchestrate multiple containers, as described here: https://www.varnish-software.com/developers/tutorials/running-varnish-docker/#6-docker-compose .

This uses the official Varnish images, rather than the one you suggested.

Here's an example of such a docker-compose.yml file that is used by the docker compose command:

version: "3"
services:
  varnish:
    image: varnish:stable
    container_name: varnish
    volumes:
      - "./default.vcl:/etc/varnish/default.vcl"
    ports:
      - "80:80"
    tmpfs:
      - /var/lib/varnish:exec
    environment:
      - VARNISH_SIZE=2G  
    depends_on:
      - "node" 
  node:
    build: ./
    container_name: node
    ports:
      - "8080:8080"

This docker-compose.yml file assumes that it is located in the same folder as the Dockerfile for your Node container. It also assumes that default.vcl is also located in that folder.

  • Related