Hi Folks,
In my previouspost we have configured Jenkins docker with Docker-Outside-Of-Docker
approach. Today we will be creating docker from php application and deploying
through Jenkins as sibling to Jenkins docker. Below are the detail steps
Configure PHP code
I will be using yii2 application which will help those who
want to deploy other php framework applications.
·
Create a file with name: Dockerfile on
your project root. Paste below contents in the file created
FROM webdevops/php-apache-dev
COPY . /app
RUN apt-get update
COPY ./config/vhost.conf
/opt/docker/etc/httpd/vhost.conf
COPY ./config/10-server.conf
/opt/docker/etc/httpd/conf.d
RUN chmod 777 /app/runtime
RUN chmod 777 /app/web/assets
·
Create a folder with name : config
on your project root.
·
Create a file with name: vhost.conf inside
config
folder. Paste below contents in the file created
#######################################
# Vhost
#######################################
<VirtualHost *:80>
ServerName docker.vm
ServerAlias *.vm
DocumentRoot
"/app/web"
UseCanonicalName Off
<IfVersion <
2.4>
Include
/opt/docker/etc/httpd/vhost.common.d/*.conf
</IfVersion>
<IfVersion >=
2.4>
IncludeOptional
/opt/docker/etc/httpd/vhost.common.d/*.conf
</IfVersion>
</VirtualHost>
<VirtualHost *:443>
ServerName docker.vm
ServerAlias *.vm
DocumentRoot
"/app/web"
UseCanonicalName Off
<IfVersion <
2.4>
Include
/opt/docker/etc/httpd/vhost.common.d/*.conf
</IfVersion>
<IfVersion >=
2.4>
IncludeOptional
/opt/docker/etc/httpd/vhost.common.d/*.conf
</IfVersion>
Include
/opt/docker/etc/httpd/vhost.ssl.conf
</VirtualHost>
·
Create a file with name: 10-server.conf inside
config
folder. Paste below contents in the file created
# Settings
TimeOut 1000
ServerName
"11e7c84a0d5e"
DirectoryIndex index.php
DocumentRoot "/app/web"
<Directory "/app/web">
Options Indexes
FollowSymLinks
AllowOverride All
<IfVersion <
2.4>
Allow from all
</IfVersion>
<IfVersion >=
2.4>
Require all granted
</IfVersion>
</Directory>
Important line is : DocumentRoot
"/app/web" which will redirect http request to app/web
folder, which is the landing folder for yii2 applications. You can adjust
according to your use case
Jenkins configuration (Back end)
·
Go to your host application and execute below
command to go inside Jenkins docker
docker exec -it myjenkins bash
·
Execute below command to install php and other
required libraries
sudo apt-get install -y php7.0 libapache2-mod-php7.0 php7.0-cli
php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql
php7.0-mcrypt php7.0-zip
·
Execute below command to install composer
sudo curl -sS https://getcomposer.org/installer
| php
sudo mv composer.phar /usr/local/bin/composer
sudo composer global require "fxp/composer-asset-plugin:^1.3.1"
·
Create personal access token for get hub and
execute below command. Personal access token can be created by opening: https://github.com/settings/tokens/new
echo '{"github-oauth": {"github.com": "<personal
access token>"}}'>> /jenkins/.composer/auth.json
Jenkins configuration (Front end)
·
Open your Jenkins and New Item
·
Enter Item name (new project) and select Freestyle project
·
Select Git option in Source Code Management section.
Enter your repository url from
github in Repository URL. Add credentials if your repository is
private.
· In Build section add Execute shell by clicking on Add build step. Follow below sequence of Execute
shell commands.
1. sudo
composer update
2. sudo
docker build -t kodingpakdemoapp .
where kodingpakdemoapp is the
docker image name you want to create, it will be used in next commands
3.
Add below code in Execute shell. This code will check for running docker
with name: kodingpakdemoapp-docker.
If there is any running then it will be stopped and deleted.
CONTAINER_NAME='kodingpakdemoapp-docker'
CID=$(sudo docker ps -q -f status=running -f name=^/${CONTAINER_NAME}$)
if [ "${CID}"
]; then
sudo docker stop ${CONTAINER_NAME}
sudo docker rm ${CONTAINER_NAME}
fi
unset CID
4.
Add below code in Execute shell. This code will check for exited docker
with name: kodingpakdemoapp-docker.
If there is any exited one then it will be deleted.
CONTAINER_NAME='kodingpakdemoapp-docker'
CID=$(sudo docker ps -q -f status=exited -f name=^/${CONTAINER_NAME}$)
if [ "${CID}"
]; then
sudo docker rm ${CONTAINER_NAME}
fi
unset CID
5.
Add below final step which will deploy
and run your docker on port 8081
sudo docker run -d -p 8081:80 --name kodingpakdemoapp-docker
kodingpakdemoapp:latest
6.
Click Save and build your project. Once
it is completed your application will be available on port 8081
Congratulations we have deployed yii2
application in docker using Jenkins
Great article
ReplyDelete