Enable SSH In Container Using Dockerfile And Configure Apache Web Server In Container Using Ansible
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.
Basics of Ansible
Docker Installation
Steps:
✅ Enable SSH in Container Using Dockerfile.
✅ Build and Push this image to the repository.
✅ Ansible Play that will launch and configure the webserver.
Step1: Enable SSH in Container Using Dockerfile
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.
DockerFile
It is compulsory to give the docker/image file name as “Dockerfile”
- 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"]
Step2: Build and Push Docker Image to hub.docker.com
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
Last step to push
docker push dockerhub_id/imagename:image_version
Step3: Ansible Playbook that will launch container
Taking container name from the user
- hosts: localhost
vars_prompt:
- name: container_name
private: no
prompt: Enter Container Name
Installing docker SDK, pulling Image from docker registry, and Launching Container
tasks:
- name: "Install docker sdk"
pip:
name: "docker-py" - name: "Pull SSH Enabled Container Image"
docker_image:
name: rohitraut3366/ssh_centos:latest
source: pull
state: present
register: image_status - name: "Launch Container Image"
docker_container:
name: "{{ container_name }}"
state: started
image: rohitraut3366/ssh_centos:latest
ports:
- "2222:22"
- "8080:80"
volumes:
- webpages:/var/www/html
when: image_status.failed == false
register: container
Adding Container as Ansible Host in inventory memory
- name: "Adding Host"
add_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
tasks:
- name: "Install Apache websever"
package:
name: httpd
state: present - name: "Copy Web Pages"
copy:
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!!😀😀