如何修复Kubernetes创建容器错误。

4
当我尝试从Docker镜像创建Pod时,我遇到了创建容器的错误。这是我的pod.yml文件:
apiVersion: v1
kind: Pod
metadata:
  name: client
spec:
  containers:
    - image: es-tutorial_web
      imagePullPolicy: Never
      name: es-web
      ports:
        - containerPort: 3000
    - image: es-tutorial_filebeat
      imagePullPolicy: Never
      name: es-filebeat

docker-compose.yml

version: '3.7'
services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    container_name: test-app
    working_dir: /usr/src/app
    command: /bin/bash startup.sh
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000"
    networks:
      - logs
  filebeat:
    build:
      context: .
      dockerfile: filebeat/Dockerfile
    container_name: test-filebeat
    volumes:
      - .:/usr/src/app
    depends_on:
      - web
    networks:
      - logs

networks:
  logs:
    driver: bridge

kubectl get pods

client                               1/2     CreateContainerError   0          24m

查看 kubectl 客户端的详细信息

Name:         client
Namespace:    default
Priority:     0
Node:         minikube/10.0.2.15
Start Time:   Tue, 15 Oct 2019 15:29:02 +0700
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"client","namespace":"default"},"spec":{"containers":[{"image":"es-tut...
Status:       Pending
IP:           172.17.0.8
Containers:
  es-web:
    Container ID:   
    Image:          es-tutorial_web
    Image ID:       
    Port:           3000/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CreateContainerError
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5ftqt (ro)
  es-filebeat:
    Container ID:   docker://4174e7eb5bf8abe7662698c96d7945a546503f3c5494cad2ae10d2a8d4f02762
    Image:          es-tutorial_filebeat
    Image ID:       docker://sha256:4e3d24ef67bb05b2306eb49eab9d8a3520aa499e7a30cf0856b8658807b49b57
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 15 Oct 2019 15:29:03 +0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5ftqt (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-5ftqt:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-5ftqt
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  13m                   default-scheduler  Successfully assigned default/client to minikube
  Normal   Pulled     13m                   kubelet, minikube  Container image "es-tutorial_filebeat" already present on machine
  Normal   Created    13m                   kubelet, minikube  Created container es-filebeat
  Normal   Started    13m                   kubelet, minikube  Started container es-filebeat
  Warning  Failed     11m (x11 over 13m)    kubelet, minikube  Error: Error response from daemon: No command specified
  Normal   Pulled     3m26s (x50 over 13m)  kubelet, minikube  Container image "es-tutorial_web" already present on machine

Dockerfile

...
RUN apt-get update && apt-get install -y curl

RUN curl -sL "https://deb.nodesource.com/setup_12.x" | bash - && apt-get install -y nodejs && echo 'node' > node
RUN mkdir -p /usr/src/app
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN chmod +x startup.sh
RUN npm install -g nodemon

startup.sh

if [ ! -d /usr/src/app/node_modules ]; then
  echo "Install dependencies..."
  cd /usr/src/app && npm install --no-bin-links
fi
cd /usr/src/app && nodemon -L bin/www

我的错在哪里?请帮帮我。


@kenorb 是的,它存在,并且图像已经成功构建。 - Akashii
@kenorb 我在我的问题中更新了dockerifle和startup.sh。 - Akashii
@Akashii 如果你已经有Docker镜像了,为什么还需要docker-compose.yml文件呢?我认为在你的Dockerfile中,你没有CMD命令。可以尝试添加CMD ./startup.sh吗? - DUDANF
1个回答

5
我相信您在您的Dockerfile中缺少CMDENTRYPOINT。这些命令是运行容器所必需的。
它们应该设置为一些默认命令,当执行容器时,您计划运行这些命令。
如果您的脚本startup.sh正在运行应用程序,请尝试以下操作:
ENTRYPOINT /usr/src/app/startup.sh

或修改您的Dockerfile为:

# ...
WORKDIR /usr/src/app
RUN chmod +x startup.sh
RUN npm install -g nodemon
RUN test ! -d /usr/src/app/node_modules && npm install --no-bin-links
ENTRYPOINT ["/usr/src/app/nodemon", "-L", "bin/www"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接