Enable SSH In Container Using Dockerfile And Configure Apache Web Server In Container Using Ansible

From Google

In this article, we will see how we can enable SSH in the docker container using Dockerfile. After Enabling we will configure the Apache web server in the container using the Ansible playbook.


✅ Enable SSH in Container Using Dockerfile.

✅ Build and Push this image to the repository.

✅ Ansible Play that will launch and configure the webserver.

Dockerfile has two types of keyword that helps to configure Image.

Build Time: Executes at build time and in the build phase all run time commands are skipped. examples are FROM, RUN, etc.

Run Time: Run time commands are ENTRYPOINT, CMD, etc.

It is compulsory to give the docker/image file name as “Dockerfile”

  1. To use base os from container Image: Dockerfile Keyword: FROM
FROM centos:latest

2. To run OS Specific command: Dockerfile keyword: RUN

RUN yum install openssh-server -y

3. Installing OpenSSH-server

RUN yum install openssh-server -y

4. Configure SSH Server for password authentication

By Default password authentication is disabled we need to add the below string in the SSHd service configuration file to enable password authentication.

RUN echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config

5. Generating Keys

For each of the key types (RSA, DSA, ECDSA, and ED25519) for which host keys do not exist, generate the host keys with the default key file path, an empty passphrase, default bits for the key type, and default comment.

RUN ssh-keygen -A

6. Changing the Password of the root user

chpasswd command is used to change the password although passwd command can also do the same. But it changes the password of one user at a time so for multiple users, chpasswd is used.

# RUN echo "user_name:password" | chpasswd
RUN echo "root:centos" | chpasswd

7. To start service at runtime

CMD ["/usr/sbin/sshd", "-D"]

here, we don't need to give a file name because it is compulsory to give the file name as “Dockerfile” and we only need to give the file location at the end.

run: docker build -t dockerhub_id/imagename:image_version fileLocation

To push the image to the Docker registry.

First, we need to give a username and password to the docker command. for this run the below command.

docker login
docker login

Last step to push

docker push dockerhub_id/imagename:image_version

Taking container name from the user

- hosts: localhost
- name: container_name
private: no
prompt: Enter Container Name

Installing docker SDK, pulling Image from docker registry, and Launching Container

- name: "Install docker sdk"
name: "docker-py"
- name: "Pull SSH Enabled Container Image"
name: rohitraut3366/ssh_centos:latest
source: pull
state: present
register: image_status
- name: "Launch Container Image"
name: "{{ container_name }}"
state: started
image: rohitraut3366/ssh_centos:latest
- "2222:22"
- "8080:80"
- webpages:/var/www/html
when: image_status.failed == false
register: container

Adding Container as Ansible Host in inventory memory

- name: "Adding Host"
name: "{{ ansible_facts['enp0s3']['ipv4']['address'] }}"
groups: docker
ansible_ssh_port: 2222
ansible_ssh_user: root
ansible_ssh_pass: centos

The last step is to configure Apache Webserver, Copying the testing Page, and Starting the Apache web service.

- hosts: docker
- name: "Install Apache websever"
name: httpd
state: present
- name: "Copy Web Pages"
content: "Hello"
dest: /var/www/html/index.html
- name: "Start Apache Web Service"
command: /usr/sbin/httpd
changed_when: false

Run the playbook

ansible-plabook playbook_name.yml

Congratulation, completed with the task

Hope This will Help you all.

Thank you for reading!!😀😀

Aspiring Cloud DevOps Engineer