Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Server \ Automate deployment of Docker-based Rails applications     - ASP.NET 5 tutorial series (Server)

- JQuery implements the same content merge cells (Programming)

- Mysql binlog resolve the garbage problem decryption (Database)

- NaSC using simple mathematical operations on Ubuntu and Elementary OS (Linux)

- CentOS 6.6 compile and install phpMyAdmin configuration PostgreSQL9.4.1 (Database)

- Talking about the shortcomings and deficiencies of the firewall (Linux)

- Solve ORA-01012: not logged on (Database)

- expdp / impdp use version parameter data migration across versions (Database)

- Kickstart automated installation and deployment RHEL 7.0 (Linux)

- Installation Flow N Play- interface stylized video player on Ubuntu (Linux)

- Linux Shell debugging (Programming)

- To install Ganglia (Linux)

- Ubuntu 14.04 CodeBlocks configuration manually compile Boost 1.57.0 (Linux)

- Nginx load balancing configuration (http proxy) (Server)

- Linux operating system security can not be ignored (Linux)

- Linux system versions organize local root password cracking method (Linux)

- Linux System Getting Started Learning: DeVeDe installed on Linux to create a video DVD (Linux)

- Python KNN algorithm of actual realization (Programming)

- Linux Getting Started tutorial: build your own Vim (Linux)

- Linux network security probing tools Hping2 (Linux)

 
         
  Automate deployment of Docker-based Rails applications
     
  Add Date : 2017-08-31      
         
         
         
  Basic Rails application

Now let's start a basic Rails application. For better presentation, I use Ruby 2.2.0 and Rails 4.1.1

Running at the terminal:

$ Rvm use2.2.0
$ Rails new && cd docker-test
To create a basic controller:

$ Rails g controller welcome index
..., and then edit routes.rb so that the root of the project points to our newly created welcome # index method:

Root 'welcome # index'
Run the rails in the terminal, and then open the browser, log http: // localhost: 3000, you will enter into the index interface. We are not going to add anything magical to the application, this is just a basic example, when we are going to create and deploy the container, use it to verify that everything is working properly.

 

Install the webserver

We intend to use Unicorn as our webserver. Add gem 'unicorn' and gem 'foreman' to the Gemfile and bundle it (run the bundle install command).

Start the Rails application, you need to configure the Unicorn, so we will be a unicorn.rb file on the config directory. Here is an example of a Unicorn configuration file that you can copy and paste directly into Gist.

Next, add a Procfile in the root directory of the project so that you can use foreman to start the application with the following contents:

Web: bundle exec unicorn -p $ PORT -c ./config/unicorn.rb
Now run the foreman start command to start the application, everything will work fine, and you will see a running application on http: // localhost: 5000.

 

Build a Docker image

Now we are building a mirror to run our application. In the root of the Rails project, create a file named Dockerfile, and paste it into the following:

# Based on mirror ruby 2.2.0
FROM ruby: 2.2.0
# Install the required libraries and dependencies
RUN apt-get update && apt-get install -qy nodejs postgresql-client sqlite3 --no-install-recommends && rm -rf / var / lib / apt / lists / *
# Set the Rails version
ENV RAILS_VERSION 4.1.1
# Install Rails
RUN gem install rails --version "$ RAILS_VERSION"
# Create the directory where the code is run
RUN mkdir -p / usr / src / app
WORKDIR / usr / src / app
# Make the webserver accessible outside the container
EXPOSE 3000
# Set the environment variable
ENV PORT = 3000
# Enable the web application
CMD [ "foreman", "start"]
# Install the required gems
ADD Gemfile / usr / src / app / Gemfile
ADD Gemfile.lock /usr/src/app/Gemfile.lock
RUN bundle install --without development test
# Add the rails project (same directory as Dockerfile) to the project directory
ADD ./ / usr / src / app
# Run the rake task
RUN RAILS_ENV = production rake db: create db: migrate
Using the above Dockerfile, execute the following command to create a mirror (make sure boot2docker is up and running):

$ Docker build -t localhost: 5000 / your_username / docker-test.
Then, if everything is fine, the last line of the long log output should look like this:

Successfully built 82e48769506c
$ Docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Localhost: 5000 / your_username / docker-test latest 82e48769506cAbout a minute ago 884.2 MB
Let's run the container and try it out!

$ Docker run -d -p 3000: 3000 - name docker-test localhost: 5000 / your_username / docker-test
Through your boot2docker virtual machine port 3000 (I was http://192.168.59.103:3000), you can observe your Rails application. (If you do not know your boot2docker virtual address, type the $ boot2docker ip command.)

Use shell scripts for automated deployment

The previous articles (Articles 1 and 2) have shown you how to push newly created mirrors into a private registry and deploy them on the server, so we skipped this part and started the automation process.

We will define three shell scripts, and then use rake to bind them together.

 

Clear

Whenever we create a mirror,

Stop and restart boot2docker;
Remove Docker Orphan Mirrors (those that are unlabeled and no longer used by the container).
Enter the following command in the clean.sh file in your project root directory.

Echo Restarting boot2docker ...
Boot2docker down
Boot2docker up
Echo ExportingDocker variables ...
Sleep 1
Export DOCKER_HOST = tcp: //192.168.59.103: 2376
Export DOCKER_CERT_PATH = / Users / user / .boot2docker / certs / boot2docker -vm
Export DOCKER_TLS_VERIFY = 1
Sleep 1
Echo Removing orphaned images without tags ...
Docker images | grep "< none>" | awk '{print $ 3}' | xargs docker rmi
To execute permissions on the script:

$ Chmod + x clean.sh
 

Construct

The build process is basically similar to what we did before (docker build). In the project root directory to create a build.sh script, fill in the following:

Docker build -t localhost: 5000 / your_username / docker-test.
Remember to give the script execute permission.

 

deploy

Finally, create a deploy.sh script that fills in the following:

# Enable the SSH connection from boot2docker to the private registry
Boot2docker ssh "ssh -o 'StrictHostKeyChecking no' -i /Users/username/.ssh/id_boot2docker -N -L 5000: localhost: 5000 root@your-registry.com &" &
# Make sure that the SSH tunnel is open before pushing it.
Echo Waiting5 seconds before pushing image.
Echo 5 ...
Sleep 1
Echo 4 ...
Sleep 1
Echo 3 ...
Sleep 1
Echo 2 ...
Sleep 1
Echo 1 ...
Sleep 1
# Push image onto remote registry / repo
Echo Starting push!
Docker push localhost: 5000 / username / docker-test
If you do not understand the meaning of this one, please carefully read this part of the second part.

Add execute permission to the script.

Use rake to bind all of the above

The current situation is that each time you want to deploy your application, you need to run these three separate scripts.

Clean
Build
Deploy / push
This is not a lot of effort, but in fact developers than you think to be too lazy! Then we simply to be lazy!

We finally put a good finishing some work, we are now three scripts to be tied together by rake.

For simplicity, you can add a few lines of code to the Rakefile that already exists in the project root directory, open the Rakefile file, and paste the following into it.

Namespace: docker do
Desc "Remove docker container"
Task: clean do
Sh './clean.sh'
End
Desc "Build Docker image"
Task: build => [: clean] do
Sh './build.sh'
End
Desc "Deploy Docker image"
Task: deploy => [: build] do
Sh './deploy.sh'
End
End
Even if you do not know rake grammar (in fact, you really should go to find out, this thing is too cool!), The above content is also very clear. We declare three tasks in a namespace (docker).

The three tasks are:

Rake docker: clean
Rake docker: build
Rake docker: deploy
Deploy is independent of build, and build is independent of clean. So every time we enter the command to run.

$ Rake docker: deploy
All scripts are executed in order.

 

test

Now let's see if everything works, you just need to make a small change in the app's code:

$ Rake docker: deploy
The next step is to witness the miracle of the moment. Once the image file is uploaded (the first may take a long time), you can ssh login product server, and (through the SSH pipeline) to pull the docker image to the server and run. How simple!

     
         
         
         
  More:      
 
- Build and verify MongoDB3.0.7 version (shard + replica) Cluster (Database)
- Ubuntu 14.04 / 14.10 how to install Mate 1.10.0 (Linux)
- Servlet 3.0 interfaces of AsyncListener (Programming)
- How to configure SNMPv3 on Ubuntu, CentOS and Cisco systems (Linux)
- Oracle 11g logical standby achieve BI needs (Database)
- Linux operating system boot process analysis (Linux)
- Android media library of analysis: MediaProvider (Programming)
- Analysis: Little Notebook facing a major security threat secure online (Linux)
- open V switch port mirror in OpenStack neutron (Server)
- Puppet centralized configuration management system (Server)
- Linux open handle limit adjustment (Linux)
- How to install MySQL on Linux Dock (Database)
- Ubuntu install Geary (Linux)
- Limit the use of the request being Nginx Flood attack (Linux)
- Use Vagrant build cross-platform development environment for Python (Server)
- Mutt - an email client that sends messages through the terminal (Linux)
- How to Disable Linux IPv6 (Linux)
- File SUID, SGID, Sticky property (Linux)
- PostgreSQL 9.4.3 Installation and Configuration under CentOS 6.5 (Database)
- Install apr support for Tomcat on Linux (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.