I’ve been trying to get a simple Nuxt 3 up and running with docker, yarn, and workspaces with the following setup:
mkdir nuxt-app
cd nuxt-app
yarn init -p
using just the defaultsmkdir config
mkdir config/admin
- Add docker-compose.yml to root directory
- Add Dockerfile to
config/admin
mkdir packages
npx nuxi init packages/admin
cd packages/admin
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
UPATED: So got it working on localhost with some help. Hope this helps anyone else looking to do this.
Dockerfile
Package.json
admin_app| /bin/sh: nuxt: not found
– means that you do not installed yarn dependenciesIn 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 copyingpackage.json
andyarn.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.You can read about multi-stage build from here.
Example of docker-compose.yaml:
Example of Dockerfile:
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:
Base image (dependencies):
Build image:
FROM
your base imageNote: you can create additional docker-compose.build.yaml for just building a project. Official docs
Example of Dockerfile: