Use a Linux machine (like CentOS, Fedora etc).
Install Jenkins by command: yum install jenkins
If the above command fails then goto Jenkin's download page in their website an add Jenkin's repo into your machine using commands similar to the following:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
Start Jenkins by command: sudo service jenkins start
Go to URL: http://<host>:8080, and set admin account's password and install the following plugins
Publish over SSH, GitHub, Deploy to container, Green Balls.
Use a Linux machine (like CentOS, Fedora etc).
Install Docker by command: yum install docker
For CentOS-7 can refer to this page: https://phoenixnap.com/kb/how-to-install-docker-centos-7
In CentOS-7 docker daemon can be started by command: sudo dockerd
If nothing works, google !!
After installing docker, create an user named "dockeradmin", set a password for it, and add it to the "dockerroot" group by the following by commands:
useradd dockeradmin
passwd dockeradmin
usermod -aG dockerroot dockeradmin
Run command "id dockeradmin" , or open the file "/etc/group" to see that "dockeradmin" user is indeed aded to the "dockerroot" group.
Open file the "/etc/ssh/sshd_config" and check that the following lines are uncommented (if not, then edit):
PasswordAuthentication yes
Subsystem sftp /usr/libexec/openssh/sftp-server
The above lines will ensure that an external machine can connect and send files to this docker-machine via SSH, SFTP and Password-Authentication. If you had to edit the above mentioned file, then you need to run the following command to restart SSH service: service sshd reload
Some useful docker commands are below:
sudo docker images
sudo docker ps -a
sudo docker inspect <container_id>
sudo docker start/stop/kill <container_id>
sudo docker exec -ti <name> psql -d expo9db -U expo9
Command to enter docker bash to run further commands there
docker exec -it edb_primary bash
docker exec -ti edb_standby bash psql -d expo9db -U expo9
Command to run sql script in docker-hosted edb from remote
sudo ssh viexpo9-edb-01 docker exec -i edb_primary psql -U expo9 -d expo9db -w -f /mnt/prod/tf/real/runtime/deployscripts/tmp/tmp.sql
Use a Linux machine (like CentOS, Fedora etc.).
Download standard unix image containing installation of tomcat: docker image pull tomcat:8.0
Command to show all active images, should show recently downloaded tomcat image too:
docker image ls , or,
docker images
Command to create a docker container named "ap_tomcat_container" that will wrap the recently downloaded tomcat image and will run on its 8080 port, but that port will be mapped to outside world to 8082:
docker container create --publish 8082:8080 --name ap_tomcat_container tomcat:8.0
Command to show all containers, should show just now created "ap_tomcat_container" as well (-a parameter is to show all, including inactive):
docker container ls -a
Start just now created "ap_tomcat_container"
docker container start ap_tomcat_container
Command to enter the container's bash shell, tomcat installation is in "/usr/local/tomcat"
docker container exec -it ap_tomcat_container bash
Create a file named "Dockerfile" in project's root location, and add the following lines
Build the docker image
docker image build -t webapp_image ./
Command to show all active images, should show recently built "webapp_image" image too
docker image ls
Create and run container "webapp_image" and map its port 8080 to 8081 for public (2nd command is for Jenkins):
docker container run -it --publish 8081:8080 webapp_image
docker run -d --name webapp_container -p 8081:8080 webapp_image
12. A set commands for Jenkins can be:
docker image build -t webapp_image ./; docker run -d --name webapp_container -p 8081:8080 webapp_image;
Use a Linux machine (like CentOS, Fedora etc.).
Install ansible (for this you will need to install python, and python-pip first). Run the following commands in sequence:
yum install python
yum install python-pip
pip install ansible
mkdir /etc/ansible
Create user "ansadmin" and add it to "dockerroot" group as follows (before that install docker in this host as well and create "dockeradmin" user and add that to "dockerroot" group like above in Docker section):
useradd ansadmin
passwd ansadmin
usermod -aG dockerroot ansadmin
Add the line "ansadmin ALL=(ALL) NOPASSWD: ALL" at the end of the file "/etc/sudoers". You can use "visudo" command for that.
Now, let us establish a mechanism to connect docker-machine from ansible-machine via SSH without password. To achieve this we need to create a private/public SSH-key pair in ansible-machine, andd then share the public-key to docker-machine. For these we need to do the following:
Switch to "ansadmin" user: su - ansadmin , and run command: ssh-keygen (don't give any passphrase, other login without password will not be possible)
It will create SSH keys (private and public both) for "ansadmin" user in location: /home/ansadmin/.ssh/
Create a user named "ansadmin" in docker-host and addd it to "dockerroot" group.
Copy the public-key to docker-host by command: ssh-copy-id ansadmin@<docker_host_ip>
Create a dedicated directory (like "/opt/artifacts/") where war file will be dumped.
Now we are going to describe 2 ways to deploy webapp.war into multiple docker machines via ansible.
Switch to "ansadmin" user: su - ansadmin
Go inside the directory just create (/opt/artifacts) and create a file named devops_playbook.yml and paste the following content:
---
- hosts: all
become: true
tasks:
- name: stop if we have old docker container
command: docker stop webapp_container
ignore_errors: yes
- name: remove stopped docker container
command: docker rm webapp_container
ignore_errors: yes
- name: remove current docker image
command: docker rmi webapp_image
ignore_errors: yes
- name: building docker image
command: docker build -t webapp_image .
args:
chdir: /opt/artifacts
- name: running docker image as container
command: docker run -d --name webapp_container -p 8081:8080 webapp_image
We shall first try to deploy webapp.war in the ansible-box itself, i.e. inn localhost. For this create a file named "hosts" in /opt/artifacts and add just 1 line in it: "localhost". Then run the following command:
ansible-playbook -i /opt/artifacts/hosts /opt/artifacts/devops_project.yml
The above command should fail as "localhost" is not reachable for "ansadmin" user -- hence copy "ansadmin" user's public-key to "localhost" as well by command: ssh-copy-id ansadmin@localhost
After this run the command again and this time it should successfully deploy webapp.war in localhost docker and should be accessible from port 8081:
ansible-playbook -i /opt/artifacts/hosts /opt/artifacts/devops_project.yml
Ideally speaking we don't need to pass a hosts file with "-i" param in command, but we should create a file named "/etc/ansible/hosts" and write all the host information there. Once such a file is written ansible will automatically read it. One can test that by running the following command: ansible all -m ping
We can mention multiple hosts in the hosts file and run the above mentioned ansible-playbook command to deploy webapp.war in all these hosts -- but make sure that each of the hosts has the ansible-machine's public-key copied there.
Create image by command: docker image build -t webapp_image ./
Tag image: docker tag webapp_image:latest <docker_login_id>/<docker_repo_name>:webapp_image_for_push
Login to dockerhub account: docker login -u <docker_login_id>
Push the image to dockerhub: docker push <docker_login_id>/<docker_repo_name>:webapp_image_for_push
Goto dockerhub account and verify that an image with tagname "webapp_image_for_push" has appeared. Now even if we delete the webapp_image from local-machine, a copy of the same image will still be there in the dockerhub repo.
You can run the same command again push the image, as long as you are logged in it will not ask for password and will overwrite the image in dockerhub.
dsd: ansible-playbook -i /mnt/users/apal/work/docker_builds/hosts /mnt/users/apal/work/docker_builds/devops_project_create_n_push_image.yml --limit localhost
sdvs
sdds
sd