skip to Main Content

tried running cron through dockerfile but when running the container its getting exit. Below is my dockerfile and error. Any help would be really appreciated

Error:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "cron": executable file not found in $PATH: unknown.

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"]

2

Answers


  1. Chosen as BEST ANSWER

    Anyone looking for answer to this, I had to change the approach a bit of running cron and it worked finally. Here is the dockerfile with updated approach.

    # 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"]
    

  2. Update the CMD to this:

    CMD ["/usr/bin/crontab", "/var/crontab"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search