docker-node icon indicating copy to clipboard operation
docker-node copied to clipboard

unable to spin up container with image: node:14-alpine3.14, but spinning up fine with node:14.18.0-alpine3.14

Open dyutis opened this issue 3 years ago • 5 comments

Version

14.18.2

Platform

alpine

Subsystem

No response

What steps will reproduce the bug?

FROM node:14-alpine3.14 (Node Version: 14.18.2)--> with this having as base image when we are trying to build a docker image of an application, the image is generated but while spinning up the container it is not executing the npm start command and exiting with exit code with 0. But the same application without any content changes if we build the image using base image as : FROM node:14.18.0-alpine3.14 (Node Version: 14.18.0), then upon image built, when we are trying to spin up the container it is spinning up fine and not exiting.

Since 14-alpine3.14 is exiting with exit code 0 so we thought of checking the docker-entrypoint.sh file in dockerhub for this base image and found it has the line: if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ] || { [ -f "${1}" ] && ! [ -x "${1}" ]; }; then --> condition case 1

However, the docker-entrypoint.sh file for the base-image: 14.18.0-alpine3.14 has the the same condition written as this: if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ]; then --> condition case 2

We understand this was done part of some bug fix, but as part of testing when we built an intermediate layer of image based on the condition case 1 docker-entrypoint.sh and used that intermediate layer of image in the FROM section of my application then for the same node version 14.18.2 the container spinned up fine.

Dockerfile (intermediate image) - tag: node14:1.0

FROM node:14-alpine3.14

COPY docker-entrypoint.sh /usr/local/bin

RUN /bin/sh -c 'chmod +x /usr/local/bin/docker-entrypoint.sh'

docker-entrypoint.sh

#!/bin/sh
set -e

# Run command with node if the first argument contains a "-" or is not a system command. The last
# part inside the "{}" is a workaround for the following bug in ash/dash:
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=874264
if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ]; then
  set -- node "$@"
fi

exec "$@"

Below is the Dockerfile where the working base image is mentioned, here if we use 14-alpine3.14 ( that has the condition case 1 in the docker-entrypoint.sh), it doesn't spin up the container properly. PFA the sample package.json we are using.

Dockerfile (correct one) -- For my application

FROM node14:1.0

# Set work directory to /usr/src/app
WORKDIR /usr/src/app

RUN apk update && apk upgrade

# Install app dependencies/www/package.json
COPY package*.json ./
RUN npm install

#switch to non-priviliaged user
RUN chown -R node:node /usr/src/app
USER node:node

# Copy app source
COPY --chown=node:node . .

# set your port
ENV PORT 3030

# expose the port to outside world
EXPOSE  3030

# start command as per package.json
CMD ["npm", "start"]

How often does it reproduce? Is there a required condition?

Every time we try

What is the expected behavior?

The container won't exit and should execute the commands in it. Must execute npm start command and container should run.

What do you see instead?

Docker conianter exits with code 0

Additional information

_No response package json-snippet _

dyutis avatar Dec 16 '21 13:12 dyutis

How is the container created? I believe that when using CMD, it bypasses the entrypoint script.

LaurentGoderre avatar Dec 16 '21 19:12 LaurentGoderre

How is the container created? I believe that when using CMD, it bypasses the entrypoint script.

In the entrypoint script if we have : if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ]; in this case the container spins up fine , however if it is: if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ] || { [ -f "${1}" ] && ! [ -x "${1}" ]; }; then the container doesn't spin up. The query is why & how the third segment of the condition which is: { [ -f "${1}" ] && ! [ -x "${1}" ]; } is affecting the container.

dyutis avatar Dec 17 '21 06:12 dyutis

The last condition doesn't run in Node context if it is the name of in executable in path.

LaurentGoderre avatar Dec 20 '21 17:12 LaurentGoderre

I don't think you should be running the entrypoint script for your image. The entrypoint is for instancd where you use the container to run Node as an executable in container (e.g. docker run node -e 'console.log("test");')

LaurentGoderre avatar Dec 20 '21 18:12 LaurentGoderre

When we are trying to spin up the pod in Kubernetes cluster using the mentioned docker image ( where only CMD is mentioned), the container just exits. The package.json screenshot is also attached above.

dyutis avatar Jan 03 '22 17:01 dyutis