Dockerzie SSH and Configure Webserver in Docker with Ansible
Hello ?? ! Here is my Article to configure Webserver inside a Docker Container with Ansible Playbook
In this article, I am going to.
- Dockerize SSH - Install SSH and Enable SSH Service in Docker
- Configure WebServer in Docker with Ansible
Dockerize SSH
Ansible works on SSH protocol and we need SSH enabled in the managed node. In our case, we are Configuring Docker so, Our Managed node is Docker Container we should Enable SSH inside the Docker Container
Pre-Configuration :
- Install Docker and Ansible
Follow this Article to Install Docker and Ansible :
Launch the Docker Container :
docker run -it -p 2222:22 --name <Container_Name> centos:latest
This Docker command run a container in interactive terminal mode and expose the 22 port of Docker to 2222 port of Host Machine Where the Docker is running give a name to the container with --name tag with centos:latest container image
We now landed at the container terminal.
Inside Container
Install SSH :
SSH Server:
- We need to install SSH Server to allow ansible to configure with SSH
yum install openssh-server -y
SSH Client :
- To use SSH commands we need SSH Client Software
yum install openssh-client -y
Start SSH Server :
Before we start SSH Service we need some host keys and we have to generate host keys, The following command will do this: 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.
ssh-keygen -A
- systemctl command or service command doesn't work in Docker so we need to find which file will run when we start service with systemctl.
- For SSH we have to run /usr/sbin/sshd with these options: do not detach (-D), log to stderr (-e), passthrough other arguments
/usr/sbin/sshd -D -e "$@"
SSH is working great !!
Escape from the container with ctrl+p+q because when we start service the terminal become non-interactive and we have to make SSH Service Permanent Now, When we restart the container the SSH should not have to stop
- The command will execute the terminal of the Container we specified
docker exec -it Container_Name bash
Make SSH Service Permanent :
Add the command - /usr/sbin/sshd -D -e "$@" at last in file /root/.bashrc Because the file will run when we restart the container so our service also start again
We Miss One thing !!
- We are one step away to connect to SSH and we need a password to connect the SSH of Docker but Docker by default doesn't have any password
Let's activate the password for root in Docker :
- Install passwd command which helps us to set the password for root
yum install passwd -y
- Set new password for root
The following command will prompt for a password and we have to use this password when we do SSH
passwd root
Check SSH
- Find the IP of the container - Run this command from Host Machine Where Docker is Running
docker inspect Container_Name | grep IP
IP is 172.17.0.2 , We know Password from the Previous Step and the user is root
Do SSH from Host Machine to Container :
ssh [email protected]
This will prompt for password and enter the password you set, Don't Worry about Warnings ?
SSH Working Great !!
If we do SSH to the container, we can easily configure Webserver in Container with Ansible
Configure WebSever in Container with Ansible
Ansible Configuration :
- In /etc/ansible/ansible.cfg give the inventory location where the hosts present
Inventory : /etc/asible/hosts
- Inventory has the list of hosts and the credentials to log in to Managed Node with SSH
Playbook
- Playbook has the tasks to do configuration. hosts keyword specify where to do configuration
- hosts: docker tasks:
Install httpd :
- This task will install httpd package
- name: Install httpd package: name: httpd state: present
Copy WebPages to the Document root :
- This task will copy index.html to /var/www/html
- name: Copy Website copy: src: index.html dest: /var/www/html
Start Service :
- The Service module doesn't work in the docker container. We can run that with the command module or shell module
Check Service State:
The shell keyword is to run command and the command s to get the status of a webserver with status code
- name: check Service shell: curl -o /dev/null -s -w '%{http_code}' https://localhost ignore_errors: true register: httpd_status
The command to get httpd status :
The curl command is to get the webpage and we are extracting the httpd_code of webserver.
curl -o /dev/null -s -w '%{http_code}' https://localhost
If httpd_status.stdout = "200" then the webserver is running. The task of Staring the Web server will Skip
Playbook
- hosts: docker tasks: - name: Install httpd package: name: httpd state: present - name: Copy Website copy: src: index.html dest: /var/www/html - name: check Service shell: curl -o /dev/null -s -w '%{http_code}' https://localhost ignore_errors: true register: httpd_status - name: Print Status debug: var: httpd_status - name: Start Service command: "/usr/sbin/httpd" when: httpd_status.stdout != "200"
Run the Playbook :
ansible-playbook FileName.yml
- The error is because the webserver is not running and the status code is 000
Check Webpages
curl 172.17.0.2
Done this task under the guidance of Mr.Vimal Daga Sir in ARTH.
Thank you Vimal Daga sir.
Thank you for Reading, Please Drop a message If you have any questions about this article. Happy to Help!
Site Reliability Engineer II @ JP Morgan Chase. @IUDX, @IISC
3 年Great ?? Kethavath Siva Naik
Cloud Technical Solutions Engineer @ Google
3 年Well done Kethavath Siva Naik ??
DevOps Engineer || Python || Docker || Ansible || Kubernetes || AWS || Jenkins || Buildkite || GoCD || GIT || GITHUB ||
3 年Awesome , well done ??Kethavath Siva Naik
DevOps Engineer @ Hike || AWS Certified || RHCE || RHCSA || DevOps || Cloud Computing
3 年Great work Kethavath Siva Naik