简体   繁体   中英

Dockerfile entrypoint in Kubernetes not executed

I've a docker container based ReactJS based app, a shell script is defined in docker image as the ENTRYPOINT, and I'm able to use docker run image-name successfully.

Now the task is to use this docker image for Kubernetes deployment using standard deployment.yaml file templates, something like following

# Deployment file
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      terminationGracePeriodSeconds: 120
      containers:
      - name: my-app
        imagePullPolicy: Always
        image: my-docker-image
        command: ["/bin/bash"]
        args: ["-c","./entrypoint.sh;while true; do echo hello; sleep 10;done"]

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
  - port: 3000
    targetPort: 3000
    protocol: TCP
    nodePort: 31110

spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 3000

when I do kubectl apply -f mydeployment.yaml, its creating required pod, but the entrypoint.sh script is not being executed upon creation of pod unlike direct running of docker image. Can someone please help in sharing what is wrong with above yaml file, am I missing or doing something incorrectly?

I also tried direclty call npm run start in command [] within yaml but no luck. I can enter in pod container using kubectl exec but I don't see react app running, I can manually execute entrypoint.sh and see the required output in browser.

Edit: Adding kubectl logs and describe output

logs: when I removed command/args from yaml and applied deploy.yaml, I get following logs as is, until starting the dev server line, there's nothing beyond that.

> myapp start /app
> react-scripts start

ℹ 「wds」: Project is running at http://x.x.x.x/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

Describe output

Name:         my-view-85b597db55-72jr8
Namespace:    default
Priority:     0
Node:         my-node/x.x.x.x
Start Time:   Fri, 16 Apr 2021 11:13:20 +0800
Labels:       app=my-app
              pod-template-hash=85b597db55
Annotations:  cni.projectcalico.org/podIP: x.x.x.x/xx
              cni.projectcalico.org/podIPs: x.x.x.x/xx
Status:       Running
IP:           x.x.x.x
IPs:
  IP:           x.x.x.x
Controlled By:  ReplicaSet/my-view-container-85b597db55
Containers:
  my-ui-container:
    Container ID:   containerd://671a1db809b7f583b2f3702e06cee3477ab1412d1e4aa8ac93106d8583f2c5b6
    Image:          my-docker-image
    Image ID:       my-docker-image@sha256:29f5fc74aa0302039c37d14201f5c85bc8278fbeb7d70daa2d867b7faa6d6770
    Port:           <none>
    Host Port:      <none>
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Fri, 16 Apr 2021 11:13:41 +0800
      Finished:     Fri, 16 Apr 2021 11:13:43 +0800
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Fri, 16 Apr 2021 11:13:24 +0800
      Finished:     Fri, 16 Apr 2021 11:13:26 +0800
    Ready:          False
    Restart Count:  2
    Environment:
      MY_ENVIRONMENT_NAME:                          TEST_ENV
      MY_SERVICE_NAME:                              my-view-service
      MY_SERVICE_MAIN_PORT:                         3000
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-9z8bw (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-9z8bw:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-9z8bw
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  32s                default-scheduler  Successfully assigned default/my-view-container-85b597db55-72jr8 to my-host
  Normal   Pulled     31s                kubelet            Successfully pulled image "my-docker-image" in 184.743641ms
  Normal   Pulled     28s                kubelet            Successfully pulled image "my-docker-image" in 252.382942ms
  Normal   Pulling    11s (x3 over 31s)  kubelet            Pulling image "my-docker-image"
  Normal   Pulled     11s                kubelet            Successfully pulled image "my-docker-image" in 211.2478ms
  Normal   Created    11s (x3 over 31s)  kubelet            Created container my-view-container
  Normal   Started    11s (x3 over 31s)  kubelet            Started container my-view-container
  Warning  BackOff    8s (x2 over 26s)   kubelet            Back-off restarting failed container

and my entrypoint.sh is

#!/bin/bash
( export REACT_APP_ENV_VAR=env_var_value;npm run start )
exec "$@"

I figured out the solution finally, I included following options in yaml under spec section and removed command/args as mentioned by above comments. Hopefully it'll be useful to anyone facing this issue.

spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-app
      template:
        metadata:
          labels:
            app: my-app
        spec:
          terminationGracePeriodSeconds: 120
          containers:
          - name: my-app
            imagePullPolicy: Always
            image: my-docker-image
            stdin: true
            tty: true

When you write this in a pod description:

      containers:
      - name: my-app
        imagePullPolicy: Always
        image: my-docker-image
        command: ["/bin/bash"]
        args: ["-c","./entrypoint.sh;while true; do echo hello; sleep 10;done"]

The command argument overrides the container ENTRYPOINT . The above is roughly equivalent to:

docker run --entrypoint /bin/bash my-docker-image ...args here...

If you want to use the ENTRYPOINT from the image, then just set args .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM