Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

named docker volume not updating using docker-compose

I'm trying to have one service to build my client side and then share it to the server using a named volume. Every time I do a docker-compose up --build I want the client side to build and update the named volume clientapp:. How do I do that?

docker-compose.yml

version: '2' 

volumes:
  clientapp:

services:
  database:
    image: mongo:3.4
    volumes:
      - /data/db
      - /var/lib/mongodb
      - /var/log/mongodb

  client:
    build: ./client
    volumes:
      - clientapp:/usr/src/app/client

  server:
    build: ./server
    ports:
      - "3000:3000"
    environment:
      - DB_1_PORT_27017_TCP_ADDR=database
    volumes:
      - clientapp:/usr/src/app/client
    depends_on:
      - client
      - database

client Dockerfile

FROM node:6

ENV NPM_CONFIG_LOGLEVEL warn

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package.json /usr/src/app

RUN npm install

COPY . /usr/src/app

# builds my application into /client
CMD ["npm", "build"]
like image 277
Ero Avatar asked Jun 21 '17 13:06

Ero


People also ask

Does Docker compose down Remove named volumes?

Networks and volumes defined as external are never removed. Anonymous volumes are not removed by default.

Does Docker compose create volumes?

Here's an example of a single Docker Compose service with a volume: services: frontend: image: node:lts volumes: - myapp:/home/node/app volumes: myapp: Running docker compose up for the first time creates a volume. The same volume is reused when you subsequently run the command.

Do you need Dockerfiles with Docker compose?

Docker compose uses the Dockerfile if you add the build command to your project's docker-compose. yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

Where does Docker compose create volume?

These volumes are created inside /var/lib/docker/volume local host directory. As we can see, we don't have to specify the host directory. We just need to specify the directory inside the container. If we remove the volume instruction from the docker-compose.


2 Answers

By definition, a volume is the persistent directories that docker won't touch other than to perform an initial creation when they are empty. If this is your code, it probably shouldn't be a volume.

With that said, you can:

  1. Delete the volume between runs with docker-compose down -v and it will be recreated and initialized on the next docker-compose up -d.

  2. Change your container startup scripts to copy the files from some other directory in the image to the volume location on startup.

  3. Get rid of the volume and include the code directly in the image.

I'd recommend the latter.

like image 154
BMitch Avatar answered Sep 21 '22 20:09

BMitch


Imagine you shared your src folder like this :

...
volumes:
      - ./my_src:/path/to/docker/src
...

What worked for me is to chown the my_src folder :

chown $USER:$USER -R my_src

It turned out some files were created by root and couldn't be modified by docker.

Hope it helps !

like image 28
madjaoue Avatar answered Sep 19 '22 20:09

madjaoue