I have a Docker construct with two services: a mysql db and a nestJs application. Users can upload files. What I want to achieve is that these files and the database are persisted in docker volumes. Therefore, I create two named external volumes, starting with docker-compose up and running migration on nestJs. I've wrote a shell script for that case:
#!/bin/bash
docker volume create rrm_db_data_STAGING
docker volume create rrm_upload_data_STAGING
DB_PORT=3001 SERVER_PORT=5001 docker-compose -f docker-compose-stage.yml -p RRM_STAGING up --build -V --remove-orphans -d
docker exec rrm-app-api_STAGING npm run typeorm:migration:run
The script is working fine.
This is my docker-compose file:
version: '3.7'
services:
api:
container_name: 'rrm-app-api_STAGING'
build:
context: .
target: production
command: npm run start:prod
restart: always
env_file:
- ./staging.env
environment:
- NODE_ENV=staging
volumes:
- type: volume
source: rrm_upload_data_STAGING
target: /uploads
ports:
- ${SERVER_PORT}:3000
networks:
- rrm-app-api-network_STAGING
depends_on:
- db
db:
image: mysql
container_name: 'rrm-app-mysql_STAGING'
volumes:
- rrm_db_data_STAGING:/var/lib/mysql
networks:
- rrm-app-api-network_STAGING
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=dbpassword
- MYSQL_DATABASE=dbname
ports:
- ${DB_PORT}:3306
networks:
rrm-app-api-network_STAGING:
volumes:
rrm_db_data_STAGING:
external: true
rrm_upload_data_STAGING:
external: true
This is the docker file:
#Development stage
FROM node:13 AS development
LABEL key="removeme"
WORKDIR /usr/src/app
COPY package*.json ./
COPY . .
RUN rm -rf node_modules
RUN rm -rf dist
RUN npm install
RUN npm run build
FROM node:13 AS production
WORKDIR /usr/src/app
COPY . .
RUN rm -rf node_modules
RUN rm -rf dist
COPY package*.json ./
RUN npm install
COPY --from=development /usr/src/app/dist ./dist
The volumes are created and the one for the database works fine. The data is persistes / synced and is still there after I do docker-compose down.
This is not working for the /uploads
folder!
The size of the volume is always 0. Files that are uploaded by users are not stored in the volume, but are present in the containers /uploads
folder. As you can see I already tried to write the "long" version in the api service's volumes section.
There is no difference, if the /uploads
folder exists in the container on start up or not. Btw. Is this necessary for binding? Because the folder is created when the first upload happens. Anyway, I think this is not the actual mistake.
Can you tell me what I'm doing wrong?
Thanks!
All credits to @DennisRuiter.
I changed the location in the container from /uploads
to the folder under the working directory specified in Dockerfile. Now it is working as expected.