skip to Main Content

I’ve been trying to get a simple Nuxt 3 up and running with docker, yarn, and workspaces with the following setup:

  1. mkdir nuxt-app
  2. cd nuxt-app
  3. yarn init -p using just the defaults
  4. mkdir config
  5. mkdir config/admin
  6. Add docker-compose.yml to root directory
  7. Add Dockerfile to config/admin
  8. mkdir packages
  9. npx nuxi init packages/admin
  10. cd packages/admin
  11. yarn init -p

What the directory looks like

./config
  - admin
    - Dockerfile
./packages
 - admin
    nuxt...
./packages.json
./.dockerignore
./docker-compose.yml

docker-compose.yml

version: '3.7'
services:
  admin:
    container_name: admin_app
    command: yarn dev
    stdin_open: true
    tty: true
    ports:
      - 3030:3030
    build:
      dockerfile: Dockerfile
      context: ./config/admin
    volumes:
      - "./packages/admin:/admin"

Dockerfile

FROM node:lts-alpine

WORKDIR /admin

COPY ./package.json yarn.lock ./

RUN yarn install
RUN yarn admin:build

EXPOSE 8000

CMD [ "yarn", "serve" ]

package.json {root}

{
  "name": "docker.nuxt",
  "version": "1.0.0",
  "private": true,
  "workspaces": [
    "packages/*"
  ],
  "scripts": {
    "admin": "yarn --cwd packages/admin dev",
    "admin:build": "yarn --cwd packages/admin build"
  }
}

Then when I run docker-compose up it throws and error admin_app| /bin/sh: nuxt: not found.

2

Answers


  1. Chosen as BEST ANSWER

    UPATED: So got it working on localhost with some help. Hope this helps anyone else looking to do this.

    Dockerfile

    FROM node:lts-alpine AS dependencies
    WORKDIR /app
    
    COPY ./package.json ./yarn.lock ./
    COPY ./packages/admin ./packages/admin/
    
    RUN yarn install
    
    # Build .output
    RUN yarn admin:build
    
    ENV NITRO_PORT=3030
    ENV NITRO_PRESET=node-server
    
    EXPOSE 3030
    
    CMD ["node", "./packages/admin/.output/server/index.mjs"]
    
    version: '3.7'
    services:
      admin:
        container_name: admin_app
        ports:
          - "3030:3030"
        build:
          dockerfile: ./config/admin/Dockerfile
          context: .
    

    Package.json

    {
      "name": "nuxt.app",
      "version": "1.0.0",
      "private": true,
      "workspaces": [
        "packages/*"
      ],
      "scripts": {
        "admin": "yarn --cwd packages/admin dev",
        "admin:install": "yarn --cwd packages/admin install",
        "admin:build": "yarn --cwd packages/admin build",
        "admin:start": "yarn --cwd packages/admin start",
      }
    }
    

    1. admin_app| /bin/sh: nuxt: not found – means that you do not installed yarn dependencies

    1. In your provided docker-compose.yaml you are specifying Dockerfile as ‘Dockerfile’ and context as path, where the file exists.

    In a nutshell, if you want to store Dockerfile in a different location, you need to specify path in dockerfile property, not context. Context is used to define, where the ‘current directory’ is (from where you are copying package.json and yarn.lock).

    Learn more from the official documentation.

    I will assume that what you actually want is to copy source files from ./packages/admin to container and then build your app.


    1. You are trying to mount source files from docker-compose.yaml in order to build your app. It is a good practice to have a 2-step build if you want just to copy final built files to container.

    You can read about multi-stage build from here.


    Example of docker-compose.yaml:

    version: '3.7'
    services:
      admin:
        container_name: admin_app
        command: yarn dev
        stdin_open: true
        tty: true
        ports:
          - "3030:3030"
        build:
          dockerfile: ./config/admin/Dockerfile
          context: .
    

    Example of Dockerfile:

    FROM node:lts-alpine AS builder
    WORKDIR /admin
    
    # Copy all files for build - ignore unused in .dockerignore
    COPY ./packages/admin ./
    
    RUN yarn install
    RUN yarn build
    
    EXPOSE 8000
    
    CMD [ "yarn", "dev" ]
    

    Note: this Dockerfile will only build admin package without yarn workspace.


    Update

    If you want to use workspaces in your build, you’ll need to tweak docker files. It will be easier to create several images (multi-stage build) – one for dependencies, second for the app itself:

    1. Base image (dependencies):

      1. Copy all files needed for installing dependencies
      2. Install dependencies
    2. Build image:

      1. Use FROM your base image
      2. Copy or mount via volume files for building a package (the last one with docker-compose.yaml)
      3. Build package
      4. Run with docker-compose.yaml

    Note: you can create additional docker-compose.build.yaml for just building a project. Official docs

    Example of Dockerfile:

    FROM node:lts-alpine AS dependencies
    WORKDIR /app
    
    # Copy files from project for dependencies install: (TODO)
    # COPY ./ ./
    
    RUN yarn install
    
    # -----------------------------
    
    # You can define that in different Dockerfile
    # For that build the first image and tag it
    #     then replace dependencies with tagged image
    #
    # Or create additional docker-compose.build.yaml 
    #    that will build your project
    # Ref: https://docs.docker.com/compose/compose-file/build/#context-required
    
    FROM dependencies AS admin
    WORKDIR /app
    
    COPY ./packages/admin ./
    RUN yarn admin:build
    
    EXPOSE 8000
    
    CMD [ "yarn", "dev" ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search