【问题标题】:cron service not starting post docker run and container is getting exitcron 服务未启动后 docker run 并且容器正在退出
【发布时间】:2022-08-10 14:48:38
【问题描述】:

尝试通过 dockerfile 运行 cron,但是在运行容器时它会退出。下面是我的 dockerfile 和错误。任何帮助将非常感激

错误:

docker:来自守护进程的错误响应:OCI 运行时创建失败:container_linux.go:380:启动容器进程导致:exec:\“cron\”:$PATH 中找不到可执行文件:未知。

Dockerfile:

# Pull base image.
FROM amazonlinux:2

ARG TERRAFORM_VERSION=1.2.6

RUN \\
yum update -y && \\
yum install unzip -y && \\
yum install wget -y && \\
yum install vim -y \\
yum install bash -y

################################
# Install Terraform
################################

# Download terraform for linux
RUN wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip

RUN unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip

RUN mv terraform /usr/local/bin/


################################
# Install python
################################

RUN yum install -y python3-pip
RUN pip3 install --upgrade pip


################################
# Install AWS CLI
################################
RUN pip install awscli --upgrade --user

# add aws cli location to path
ENV PATH=~/.local/bin:$PATH


RUN mkdir ~/.aws && touch ~/.aws/credentials


################################
# Install Cron
################################

RUN yum -y install ca-certificates shadow-utils cronie && yum -y clean all

# Creating crontab
COPY  ./automation.sh /var/automation.sh


# Giving executable permission to script file.
RUN chmod +x /var/automation.sh \\
    && echo \"* * * * *  /bin/bash /var/automation.sh\" >> /var/crontab


# Ensure sudo group users are not asked for a password
RUN echo \'%sudo ALL=(ALL) NOPASSWD:ALL\' >> \\
/etc/sudoers

#run cron process through cmd

CMD [\"cron\", \"-f\"]

    标签: docker cron dockerfile devops


    【解决方案1】:

    将 CMD 更新为:

    CMD ["/usr/bin/crontab", "/var/crontab"]
    

    【讨论】:

    • 这个也试过了,但没有成功。终于稍微改变了方法,现在工作正常
    【解决方案2】:

    任何寻找答案的人,我都不得不改变运行 cron 的方法,它终于奏效了。这是具有更新方法的 dockerfile。

    # Pull base image.
    FROM amazonlinux:2
    
    ARG TERRAFORM_VERSION=1.2.6
    
    ################################
    # Install Dependencies
    ################################
    RUN yum update -y && yum -y install unzip wget vim bash procps python3-pip jq git && pip3 install --upgrade pip
    
    ################################
    # Install AWS CLI
    ################################
    RUN pip install awscli --upgrade --user
    
    # add aws cli location to path
    ENV PATH=~/.local/bin:$PATH
    RUN mkdir ~/.aws && touch ~/.aws/credentials
    
    ################################
    # Install Terraform
    ################################
    # Download terraform for linux
    RUN wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip
    
    RUN unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
    
    RUN mv terraform /usr/local/bin/
    
    ################################
    # Install Cron
    ################################
    RUN yum -y install ca-certificates shadow-utils cronie && yum -y clean all
    
    # Creating crontab
    COPY  ./automation.sh /var/automation.sh
    
    
    # Giving executable permission to script file.
    RUN chmod +x /var/automation.sh \
        && echo "* * * * *  /bin/bash /var/automation.sh" >> /var/crontab
    
    RUN crontab /var/crontab
    
    # Ensure sudo group users are not asked for a password
    RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> \
    /etc/sudoers
    
    #run cron process through cmd
    CMD ["/usr/sbin/crond", "-n"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2019-07-13
      • 2018-09-29
      • 2016-09-03
      • 2018-01-20
      • 1970-01-01
      相关资源
      最近更新 更多