Search code examples
angulardockerdockerfilecontainers

Docker container exits immediately after running the container


I have an angular 17 project and I want to dockerize it. I already build my image and when I tried to run the container using docker run -dit --name <container name> <imagename> but the container exits immediately after that. When I run docker ps my container doesn't appear.

Dockerfile:

# Use node image as builder
FROM node:18.14 AS builder

# Set working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the container
COPY package.json package-lock.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the application
RUN npm run build --prod

# Use nginx image for serving the application
FROM nginx:latest

# Copy built application files from builder stage to nginx directory
COPY --from=builder /app/dist/project /usr/share/nginx/html

# Expose port 80 for nginx
EXPOSE 80

# Start nginx in foreground
CMD ["nginx", "-g", "daemon off;"]

And I noticed that my image cannot be analyzed in docker desktop as shown in this screenshot: Docker Desktop


Solution

  • As you mentioned in comment exec /docker-entrypoint.sh: exec format error is the real problem to solve. Can you come up with a minimal, reproducible example? As an example, this is working locally for me

    # Use node image as builder
    FROM node:18.14 AS builder
    
    # Set working directory in the container
    WORKDIR /app
    
    # (pretend to) Build the application
    RUN echo "Hello world" > out.txt
    
    # Use nginx image for serving the application
    FROM nginx:latest
    
    # Copy built application files from builder stage to nginx directory
    COPY --from=builder /app/ /usr/share/nginx/html
    

    It's likely related to processor architecture differences, see Docker Fails When Building on M1 Macs (exec /usr/local/bin/docker-entrypoint.sh: exec format error) and https://docs.docker.com/build/guide/multi-platform/

    If you're building an image locally and running it elsewhere, this may well be the solution - https://stackoverflow.com/a/69636473/131391

    If you're building and running locally, then I would first make sure you can simply run the mentioned base images before writing the Dockerfile. e.g. docker run nginx:latest