Simple Dockerfile with cron and sshd in centos version.

Dockerfile

Use the below content to build your own docker image.

FROM centos:latest

MAINTAINER ahamed

ADD run.sh /tmp/run.sh
ADD root_cron /tmp/root_cron

## Install crontab and basic configuratoin
RUN yum -y update \
	&& yum -y install crontabs \
	&& sed -i -e '/pam_loginuid.so/s/^/#/' /etc/pam.d/crond \
	&& crontab /tmp/root_cron 

## Install openssh and give access to outside
RUN yum install -y openssh-server passwd \
	&& mkdir /var/run/sshd \
	&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' \
	&& echo 'secret@123' | passwd --stdin root \
	&& adduser anotheruser \
	&& echo 'secret@123' | passwd --stdin anotheruser 

RUN chmod +x /tmp/run.sh

EXPOSE 22
CMD /tmp/run.sh

Steps to run this docker

Clone the below project and edit root_cron as per your requirement.

git clone git@github.com:ahamedyaserarafath/docker_ssh_cron_centos.git

Build the docker image

docker build -t centos_ssh_cron . 

To Test, start the container with port forwarding

docker container run -it -p 2222:22 -d centos_ssh_cron 

And get the container IP

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id 

Try ssh into the private IP with both users mentioned in the docker file

ssh root@<ip>
password -> secret@123
ssh anotheruser@<ip>
password -> secret@123
https://github.com/ahamedyaserarafath/docker_ssh_cron_centos

GitHub Link, Please feel free to fork and use it

Categorized in:

Tagged in: