我有一个开发环境,我正在dockerizing,我希望能够重载我的变化,而不必重建docker图像。我使用docker撰写,因为redis是我的应用程序的依赖项之一,我喜欢能够链接一个redis容器

我在docker-compose.yml中定义了两个容器:

node:
  build: ./node
  links:
    - redis
  ports:
    - "8080"
  env_file:
    - node-app.env

redis:
  image: redis
  ports:
    - "6379"

我已经在我的节点应用程序的dockerfile中添加了一个卷,但我如何在卷中挂载主机的目录,以便我对代码的所有实时编辑都反映在容器中?

这是我当前的Dockerfile:

# Set the base image to Ubuntu
FROM    node:boron

# File Author / Maintainer
MAINTAINER Amin Shah Gilani <amin@gilani.me>

# Install nodemon
RUN npm install -g nodemon

# Add a /app volume
VOLUME ["/app"]

# TODO: link the current . to /app

# Define working directory
WORKDIR /app

# Run npm install
RUN npm install

# Expose port
EXPOSE  8080

# Run app using nodemon
CMD ["nodemon", "/app/app.js"]

我的项目是这样的:

/
- docker-compose.yml
- node-app.env
- node/
  - app.js
  - Dockerfile.js

当前回答

下面是我的Node.js应用程序和MongoDB数据库的工作示例:

docker-compose.yml

version: '3'
services: 
    my-app:
        container_name: my-app-container
        restart: always
        build: .
        volumes:
            - './storage:/usr/src/app/storage'
        ports: 
            - "3000:3000"
        links:
            - my-app-db
    
    my-app-db:
        container_name: my-app-db-container
        image: mongo
        restart: always
        volumes:
            - './data:/data/db'          
        ports:
            - "27017:27017"

Dockerfile

FROM node:16.13.2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . /usr/src/app/
EXPOSE 3000
CMD [ "npm", "start"]

其他回答

在docker-compose。Yml你可以使用这样的格式:

volumes:
    - host directory:container directory

根据他们的文件

共享redis golang docker-compose.yaml。使用绑定挂载我已经实现了它。

version: '3.0'
services:
  redisdb:
    image: redis:6.0
    restart: always
    ports:
      - "6379:6379"
    container_name: redisdb-container
    command: ["redis-server", "--bind", "redisdb", "--port", "6379"]

  urlshortnerservice:
    depends_on:
      - redisdb
    ports:
      - "7777:7777"
    restart: always
    container_name: url-shortner-container
    image: url-shortner-service
    volumes:
      - ../pkg/repository/filestorage:/pkg/repository/filestorage #host directory:container directory

检查他们的文档

从它的外观来看,您可以在docker-compose.yml上执行以下操作

volumes:
    - ./:/app

其中。/是主机目录,/app是容器的目标目录。


EDIT:
Previous documentation source now leads to version history, you'll have to select the version of compose you're using and look for the reference.

对于lazy - v3 / v2 / v1

旁注:在此编辑之前,语法对所有版本保持相同

有两件事:

我在docker-compose.yml中添加了卷:

node:
  volumes:
    - ./node:/app

我把npm install && nodemon app.js部分移动到CMD中,因为RUN会把东西添加到联合文件系统中,而我的卷不是UFS的一部分。

# Set the base image to Ubuntu
FROM    node:boron

# File Author / Maintainer
MAINTAINER Amin Shah Gilani <amin@gilani.me>

# Install nodemon
RUN npm install -g nodemon

# Add a /app volume
VOLUME ["/app"]

# Define working directory
WORKDIR /app

# Expose port
EXPOSE  8080

# Run npm install
CMD npm install && nodemon app.js

下面是我的Node.js应用程序和MongoDB数据库的工作示例:

docker-compose.yml

version: '3'
services: 
    my-app:
        container_name: my-app-container
        restart: always
        build: .
        volumes:
            - './storage:/usr/src/app/storage'
        ports: 
            - "3000:3000"
        links:
            - my-app-db
    
    my-app-db:
        container_name: my-app-db-container
        image: mongo
        restart: always
        volumes:
            - './data:/data/db'          
        ports:
            - "27017:27017"

Dockerfile

FROM node:16.13.2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . /usr/src/app/
EXPOSE 3000
CMD [ "npm", "start"]