Using Gradle for Single Click Deployment
As a software engineer, nothing demotivates me more than having to test a web application remotely. Just imagining having to stop the application, upload the new code, and rerun the app tires me out. Fortunately, this process can be easily automated with build tools. Here I will share how I’ve overcome this issue by using one of my favorite tools: Gradle.
We will be deploying a simple “Hello World” web app. The entire source used here can be found on GitHub.
Requirements:
Project folder structure:
- webapp/
- src/main/java/server/Main.java
- deploy/
- Dockerfile
- setup_webserver.sh
- build.gradle
- gradle.properties
Step 1: Setting up our remote machine
Note: If you already have a Linux server that you can SSH into, then skip to Step 2.
Let’s begin by creating a Docker image using a Dockerfile. Following the folder structure shown previously, save this to webapp/deploy/Dockerfile:
Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y openssh-server
RUN apt-get install -y unzip
RUN apt-get install -y default-jre
RUN mkdir /var/run/sshd
RUN echo 'root:password' | chpasswd
RUN useradd webadmin
RUN echo "webadmin:password" | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@sessions*requireds*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
RUN mkdir /home/webadmin/
RUN chown -R webadmin:webadmin /home/webadmin/
EXPOSE 22 80
CMD ["/usr/sbin/sshd", "-D"]
To execute:
- Open command line in webapp/deploy/
- Execute:
docker build -t ssh_ubuntu ./
- Then execute:
docker run -t -d -p 8887:22 -p 8888:8888 --name webserver ssh_ubuntu
The first command will create a Linux Docker image with all the requirements and a user called webadmin with password “password”.
The second command will create a Docker container with the Linux image. Note: It will map the SSH port to 8887. To continue with the other steps, download the rest of the article here.