Hi everyone,
I’m creating a new project in Angular and trying to create a container for it
I install the Angular CLI
npm install -g @angular/cli
and create a new project
ng new my-app
cd my-app
ng serve --open
Everything works correctly in the browser
Then I generate the files needed for Docker using
docker init
malgorzata@malgorzata-X556UQK:~/angular/my-app$ docker init
Welcome to the Docker Init CLI!
This utility will walk you through creating the following files with sensible defaults for your project:
- .dockerignore
- Dockerfile
- compose.yaml
- README.Docker.md
Let's get started!
? What application platform does your project use? Node
? What version of Node do you want to use? 20.12.2
? Which package manager do you want to use? npm
? Do you want to run "npm run build" before starting your server? Yes
? What directory is your build output to? (comma-separate if multiple) dist/my-app
? What command do you want to use to start the app? npm start
? What port does your server listen on? 2323
✔ Created → .dockerignore
✔ Created → Dockerfile
✔ Created → compose.yaml
✔ Created → README.Docker.md
→ Your Docker files are ready!
Review your Docker files and tailor them to your application.
Consult README.Docker.md for information about using the generated files.
What's next?
Start your application by running → docker compose up --build
Your application will be available at http://localhost:2323
I follow the instructions byt I end up with the error ng: not found
malgorzata@malgorzata-X556UQK:~/angular/my-app$ docker compose up --build
[+] Building 18.8s (18/18) FINISHED docker:desktop-linux
=> [server internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 2.67kB 0.0s
=> [server] resolve image config for docker-image://docker.io/docker/dockerfile:1 2.8s
=> [server auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED [server] docker-image://docker.io/docker/dockerfile:1@sha256:a57df69d0ea827fb7266491f2813635de6f1 0.0s
=> [server internal] load metadata for docker.io/library/node:20.12.2-alpine 1.4s
=> [server auth] library/node:pull token for registry-1.docker.io 0.0s
=> [server internal] load .dockerignore 0.1s
=> => transferring context: 663B 0.0s
=> [server base 1/2] FROM docker.io/library/node:20.12.2-alpine@sha256:7a91aa397f2e2dfbfcdad2e2d72599f374e0 0.0s
=> [server internal] load build context 0.2s
=> => transferring context: 5.32MB 0.2s
=> CACHED [server base 2/2] WORKDIR /usr/src/app 0.0s
=> CACHED [server deps 1/1] RUN --mount=type=bind,source=package.json,target=package.json --mount=type= 0.0s
=> CACHED [server build 1/3] RUN --mount=type=bind,source=package.json,target=package.json --mount=type 0.0s
=> [server build 2/3] COPY . . 0.2s
=> [server build 3/3] RUN npm run build 13.5s
=> CACHED [server final 1/3] COPY package.json . 0.0s
=> CACHED [server final 2/3] COPY --from=deps /usr/src/app/node_modules ./node_modules 0.0s
=> CACHED [server final 3/3] COPY --from=build /usr/src/app/dist/my-app ./dist/my-app 0.0s
=> [server] exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:fe8f8fea5c15e62064e929f6446d40edfb2717f8789f9f355bb5a44653941f6d 0.0s
=> => naming to docker.io/library/my-app-server 0.0s
[+] Running 0/1
⠸ Container my-app-server-1 Recreated 0.3s
Attaching to server-1
server-1 |
server-1 | > my-app@0.0.0 start
server-1 | > ng serve
server-1 |
server-1 | sh: ng: not found
server-1 exited with code 127
This is my generated Dockerfile
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
ARG NODE_VERSION=20.12.2
################################################################################
# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base
# Set working directory for all build stages.
WORKDIR /usr/src/app
################################################################################
# Create a stage for installing production dependecies.
FROM base as deps
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
# Leverage bind mounts to package.json and package-lock.json to avoid having to copy them
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --omit=dev
################################################################################
# Create a stage for building the application.
FROM deps as build
# Download additional development dependencies before building, as some projects require
# "devDependencies" to be installed to build. If you don't need this, remove this step.
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci
# Copy the rest of the source files into the image.
COPY . .
# Run the build script.
RUN npm run build
################################################################################
# Create a new stage to run the application with minimal runtime dependencies
# where the necessary files are copied from the build stage.
FROM base as final
# Use production node environment by default.
ENV NODE_ENV production
# Run the application as a non-root user.
USER node
# Copy package.json so that package manager commands can be used.
COPY package.json .
# Copy the production dependencies from the deps stage and also
# the built application from the build stage into the image.
COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/dist/my-app ./dist/my-app
# Expose the port that the application listens on.
EXPOSE 2323
# Run the application.
CMD npm start
What should I modify to make it work?