[Docker] Run Stateless Docker Containers (Volumes with docker compose)

Docker containers should be designed to be stateless, meaning that they can survive system reboots and container terminations gracefully, and without the loss of data. Designing with stateless containers in mind will also help your app grow and make future horizontal scaling trivial.

In this lesson, we will review an app that saves uploaded files to the filesystem. Then we will learn how to setup a persistent volume for the uploaded files so they can survive Docker container halts, restarts, stops and respawns.

Dockerfile:

FROM mhart/alpine-node
WORKDIR /srv
COPY . .
RUN mkdir uploads
RUN yarn
EXPOSE 8080
CMD node index.js

docker-compose.yaml:

version: '3'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    volumes:
      - appdata:/srv/uploads
volumes:
  appdata:

Remove docker compose:

docker-compose rm -f
 
原文地址:https://www.cnblogs.com/Answer1215/p/14367490.html