Home > OS >  How to bind folders inside docker containers?
How to bind folders inside docker containers?

Time:11-23

I have docker-compose.yml on my local machine like below:

version: "3.3"
services:
  api:
    build: ./api
    volumes:
      - ./api:/api
    ports:
      - 3000:3000
    links:
      - mysql
    depends_on:
      - mysql
  app:
    build: ./app
    volumes:
      - ./app:/app
    ports:
      - 80:80
  mysql:
    image: mysql:8.0.27
    volumes:
      - ./mysql:/var/lib/mysql
    tty: true
    restart: always
    environment:
      MYSQL_DATABASE: db
      MYSQL_ROOT_PASSWORD: qwerty
      MYSQL_USER: db
      MYSQL_PASSWORD: qwerty
    ports:
      - '3306:3306'

The api is NestJS app, app, mysql - Angular and Mysql respectively. And I need to work with this one localy.

How could I make so, that any my changes will be applied without rebuilding containers every time?

CodePudding user response:

You don't have to build an image for a development environment with your sources in it. For NestJS, and since you're using Docker (I voluntary specify this because it exists other container runtimes), you can simply run a NodeJS image from the Docker main registry: https://hub.docker.com/_/node.

You could run it with:

docker run -d -v ./app:/app node:12-alpine /app/index.js

N.B.: I choose 12-alpine for the example. I imagine the file to start your app is index.js, replace it with yours.

You must consider to install the node dependencies yourself and they must be in the ./app directory.

For docker-compose, it could look like this:

version: "3.3"
services:
  app:
    image: node:12-alpine
    command: /app/index.js
    volumes:
      - ./app:/app
    ports:
      - "80:80"

Same way for your API project.

For a production image, it is still suggested to build the image with the sources in it.

CodePudding user response:

Say you're working on your front-end application (app). This needs to make calls out to the other components, especially api. So you can start the things it depends on, but not the application itself

docker-compose up -d api

Update your application configuration for this different environment; if you would have proxied to http://api:3000 before, for example, you need to change this to http://localhost:3000 to connect to the container's published ports:.

Now you can develop your application totally normally, without doing anything Docker-specific.

# outside Docker, on your normal development workstation
yarn run dev
$EDITOR src/components/Foo.tsx

You might find it convenient to use environment variables for these settings that will, well, differ per environment. If you're developing the back-end code but want to attach a live UI to it, you'll either need to rebuild the container or update the front-end's back-end URL to point at the host system.

This approach also means you do not need to bind-mount your application's code into the container, and I'd recommend removing those volumes: blocks.

  • Related