An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
Creating {legacy} static build via docker and deploying with mina-scp
I am sure! Many of our applications have turned into a legacy codebase, such applications might have some outdated scripts or build process that might need frequent maintenance and updates.
In this post, I will walk you through the steps with which we can move our frontend build creation script into the docker container and deploy through mina and mian-scp gems seamlessly.
Even if your code is not a legacy codebase it is always better to build your application into a docker container because
- You need to create build in a production like environments
- If you want to do a production system upgrades, it's lot more easier to test build creation in docker containers in advance
- Avoid any package missing or mis-match issues at runtime, specially on older node versions< 6.x.x.
Brief background
I had a angular 1.x
application running with node 6.x.x, things were all good until we noticed following
- First issue was previously on servers node-modulesand bower packages were moved on serversmanually
- We were having two(production/staging) servers with different node
5.x.x
and6.x.x
setup receptively. - In older
node <= 5.x.x
,npm install
does not supportpackage.json
- Package addition and updates were a nightmare
- We were frequently facing package related issues because of lack of proper
package.json
We resolved all above issues one by one after deciding to fix the node version and upgraded to version 6.x.x
and moved the build creation process into a docker container to have a standard build creation and deployment process.
Creating a build in docker container via docker compose
If you are new to docker and docker-compose, I would advise you to get your hands dirty in docker concepts by referring it here and here
Dockerfile
Dockerfile
: In this file we are only specifying required ubuntu, node, npm and yarn versions that are required for application and before that we are installing few dependencies.
FROM ubuntu:18.04SHELL ["/bin/bash", "-l", "-c"]RUN mkdir /usr/local/nvmENV NVM_DIR /usr/local/nvmENV NODE_VERSION 6.13.0ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION# install ubuntu related dependenciesRUN apt-get update -q && \ apt-get install -qy curl ca-certificates gnupg2 build-essential --no-install-recommends && apt-get cleanRUN apt-get install -y git zip# install nvmRUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash# install node version 6.13.0RUN source $NVM_DIR/nvm.sh \ && nvm install $NODE_VERSION \ && nvm alias default $NODE_VERSION \ && nvm use defaultWORKDIR /usr/bullet# copy dependencies json file to docker containerCOPY package.json $WORKDIRCOPY yarn.lock $WORKDIRCOPY bower.json $WORKDIR# install package managerRUN source $NVM_DIR/nvm.sh && npm install -g [email protected] source $NVM_DIR/nvm.sh && npm install -g [email protected] source $NVM_DIR/nvm.sh && npm install -g [email protected] source $NVM_DIR/nvm.sh && yarn install --quietRUN source $NVM_DIR/nvm.sh && bower install --quiet --allow-rootCOPY . $WORKDIR
Once the dockerfile is ready we can use this in docker-compose with following docker-compose.yml file to create multiple services
docker-compose.yml
version: '3.4'services: frontend-app: &frontend-app build: context: . dockerfile: ./Dockerfile image: my-frontend-app:0.0.1 volumes: - ./:/usr/frontend-app/ - ./static_build:/usr/frontend-app/static_build - nodemodules:/usr/frontend-app/node_modules - bowercomponents:/usr/frontend-app/bower_components start: <<: *frontend-app ports: - '8000:8000' command: bash -c "source /usr/local/nvm/nvm.sh && gulp dev" create_build: <<: *frontend-app command: bash -c "source /usr/local/nvm/nvm.sh && gulp prod && rm -f static_build/build.zip && zip -rq static_build/build.zip build/"volumes: nodemodules: {} bowercomponents: {}
In docker-compose file we are creating start
and create_build
services, start
is to start the application inside the docker container with following command
#Running build on port 8080source /usr/local/nvm/nvm.sh && gulp dev
create_build
is to create and compress a build with following command
#Creating prod build, removing old build and creating new buildsource /usr/local/nvm/nvm.sh && gulp prod && rm -f static_build/build.zip && zip -rq static_build/build.zip build/
Important to note that we have created shared volumes and folders between host and container, So that we can have build.zip
to be created on host and node modules can be shared across the builds if no changes to package.json
Once above setup is done we can easily create or start the build using following commands
#Starting a builddocker-compose up start#Creating a builddocker-compose up create_build
After above setup is verified we can deploy this build via mina and mina-scp gem as following
Note: Hope you have done basic setup which is needed for mina deployment.(Let me know if you need any help!)
Deploying static build to servers
Following is the sample mina deployment script which is copying and zipping build to the server.
config/deploy.rb
#Mina, version v0.3.8require 'mina/scp'# Basic settings like branch, server repo settings goes here# set :branch, staging# gem install mina -v 0.3.8 (you can use latest mina version)# gem install mina-scp -v 0.1.2# Put any custom mkdir's in here for when `mina setup` is ran.# all releases.task :setup => :environment doenddesc "create a build on the local"task :create_build => :environment do to :before_hook do # Put things to run locally before ssh queue! %[echo "-----> creating static build..."] queue! %[sudo docker-compose up create_build] endenddesc "Deploys the current version to the server."task :deploy => :environment do deploy do # Put things that will set up an empty directory into a fully set-up # instance of your project. invoke :'deploy:link_shared_paths' queue! %[echo "-----> uploading build domain: #{domain} branch: #{branch}, deploy_to: #{deploy_to}"] scp_upload('./static_build/build.zip', "#{deploy_to}/build.zip", verbose: true) invoke :'deploy:cleanup' to :launch do queue "echo '-----> Unziping build...'" queue "cp #{deploy_to}/build.zip #{deploy_to}/current/build.zip" queue "unzip -q #{deploy_to}/current/build.zip" queue "echo '-----> Unzip completed'" end endend
Here we are creating and deploying build via
mina create_buildmina deploy
If you have noticed we are copying build ./static_build/build.zip
created in docker via mina-scp's scp_upload
command and unzipping in deploy task.
scp_upload('./static_build/build.zip', "#{deploy_to}/build.zip", verbose: true)
That's it, We have successfully created and deployed static build with the help of docker and mina. If you have reached here and have any suggestions or thoughts let me know in comments section.
Original Link: https://dev.to/pramodshinde7/creating-legacy-static-build-via-docker-and-deploying-with-mina-scp-3hde
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To