<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://iheng.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://iheng.github.io/" rel="alternate" type="text/html" /><updated>2019-06-12T22:05:47+00:00</updated><id>https://iheng.github.io/feed.xml</id><title type="html">Heng Li blog</title><subtitle>Software Developer from USZCN.</subtitle><entry><title type="html">Running rail app in docker</title><link href="https://iheng.github.io/Running-rail-app-in-docker/" rel="alternate" type="text/html" title="Running rail app in docker" /><published>2019-06-12T00:00:00+00:00</published><updated>2019-06-12T00:00:00+00:00</updated><id>https://iheng.github.io/Running-rail-app-in-docker</id><content type="html" xml:base="https://iheng.github.io/Running-rail-app-in-docker/">&lt;h4 id=&quot;step-1&quot;&gt;Step 1:&lt;/h4&gt;
&lt;h5 id=&quot;first-using-dockfile-to-define-the-project&quot;&gt;First, using &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockfile&lt;/code&gt; to Define the project&lt;/h5&gt;
&lt;p&gt;we need to create Dockefile, you can cop sample 
dockerfile from offical websie
(This is sample &lt;a href=&quot;https://docs.docker.com/compose/rails/&quot;&gt;Dockerfile&lt;/a&gt; copy from docker offical website)&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FROM ruby:2.5
RUN apt-get update -qq &amp;amp;&amp;amp; apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT [&quot;entrypoint.sh&quot;]
EXPOSE 3000

# Start the main process.
CMD [&quot;rails&quot;, &quot;server&quot;, &quot;-b&quot;, &quot;0.0.0.0&quot;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h5 id=&quot;second-create-a-gemfile-to-just-load-rails&quot;&gt;second, create a Gemfile to just load rails.&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;source 'https://rubygems.org'
gem 'rails', '~&amp;gt;5' # specify rails version in here

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h5 id=&quot;third-create-an-empty-gemfilelock-to-build-dockefile&quot;&gt;Third, create an empty &lt;code class=&quot;highlighter-rouge&quot;&gt;Gemfile.lock&lt;/code&gt; To build &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockefile&lt;/code&gt;&lt;/h5&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;touch Gemfile.lock&lt;/code&gt;&lt;/p&gt;

&lt;h5 id=&quot;fourth-create-shell-script-named--entrypointsh-&quot;&gt;Fourth, create shell script, named  &lt;code class=&quot;highlighter-rouge&quot;&gt;entrypoint.sh &lt;/code&gt;&lt;/h5&gt;
&lt;p&gt;The purpose of entrypoint scipt is to fix a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Remove a potentially pre-existing server.pid for Rails.&lt;/span&gt;
rm &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; /myapp/tmp/pids/server.pid

&lt;span class=&quot;c&quot;&gt;# Then exec the container's main process (what's set as CMD in the Dockerfile).&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h5 id=&quot;finalycreate-docker-composeyml-file&quot;&gt;Finaly,&lt;code class=&quot;highlighter-rouge&quot;&gt;create docker-compose.yml&lt;/code&gt; file&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;version: '3'
services:
  db:
    image: mysql
    volumes:
      - ./tmp/db:/var/lib/mysql/data
  web:
    build: .
    command: bash -c &quot;rm -f tmp/pids/server.pid &amp;amp;&amp;amp; bundle exec rails s -p 3000 -b '0.0.0.0'&quot;
    volumes:
      - .:/myapp
    ports:
      - &quot;3000:3000&quot;
    depends_on:
      - db
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;step-2-build-the-project&quot;&gt;Step 2: Build the project&lt;/h3&gt;
&lt;p&gt;Now it’s time to generate the rails skelton app using docker compose run command:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose run web rails new . --force --no-deps --database=postgresql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you ever modify gem file. which you’ll do.
you need run:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose build&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;step-3-connect-the-database&quot;&gt;Step 3: Connect the database&lt;/h3&gt;
&lt;p&gt;This is tricky part. when I followed docker website. I was stuck in this step. the offical mysql docker image which is somehow can’t initial. I got&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt; [ERROR] --initialize specified but the data directory has files in it. Aborting.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After google for a while. I found a solution which is change db image source in &lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;from &lt;code class=&quot;highlighter-rouge&quot;&gt;image: mysql&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;image: mariadb&lt;/code&gt;
the detail discuss about this issue is &lt;a href=&quot;https://github.com/docker-library/mysql/issues/69&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can boot the app with &lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose up&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose up&lt;/code&gt;
Finally, you need to create the database. In another terminal, run:
&lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose run web rake db:create&lt;/code&gt;&lt;/p&gt;</content><author><name></name></author><summary type="html">Step 1: First, using Dockfile to Define the project we need to create Dockefile, you can cop sample dockerfile from offical websie (This is sample Dockerfile copy from docker offical website)</summary></entry><entry><title type="html">Flutter network connection issues</title><link href="https://iheng.github.io/Flutter-network-connection-issue/" rel="alternate" type="text/html" title="Flutter network connection issues" /><published>2019-05-14T00:00:00+00:00</published><updated>2019-05-14T00:00:00+00:00</updated><id>https://iheng.github.io/Flutter-network-connection-issue</id><content type="html" xml:base="https://iheng.github.io/Flutter-network-connection-issue/">&lt;h4 id=&quot;flutter-network-connection-issuesfaild-host-lookup&quot;&gt;Flutter network connection issues(faild host lookup)&lt;/h4&gt;
&lt;p&gt;I tried to call api from emulator to my host machine and I got following error message
&lt;code class=&quot;highlighter-rouge&quot;&gt;E/flutter (16241): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: Failed host lookup: 'us1.uszcn.local' (OS Error: No address associated with hostname, errno = 7)&lt;/code&gt;
It took me one hour to figure out what’s the issues is.&lt;/p&gt;

&lt;h4 id=&quot;step-1-check-whether-internet-pemisson-was-granted&quot;&gt;Step 1: Check whether internet pemisson was granted&lt;/h4&gt;
&lt;p&gt;Open your AdroidManifest.xml file check whether internet pemisson was granted. if not add following to the file ` &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;&gt;&lt;/uses-permission&gt;`&lt;/p&gt;

&lt;p&gt;If after edit AdroidManifest.xml not solve your issue. which didb’t solve my issue.
please process to step 2:&lt;/p&gt;

&lt;h4 id=&quot;step2-check-your-emulator-proxy-setting&quot;&gt;Step2: Check your emulator proxy setting.&lt;/h4&gt;
&lt;p&gt;According to the offical document. you need to set hostname as 10.0.2.2: port number is whichever you using in your local machine.&lt;/p&gt;

&lt;p&gt;Try again, if you can call you service. or in your emulator,open a webbrowser visit 10.0.2.2:portname(the port you’re using in your local machine). if you can visit, that means your proxy setting is working.&lt;/p&gt;

&lt;p&gt;if you still can’t error messge. look up your host file
in your local machine. you may setting up custom domain for it&lt;/p&gt;
&lt;h4 id=&quot;step3edit-your-emulator-host-file&quot;&gt;step3:Edit your emulator host file.&lt;/h4&gt;
&lt;p&gt;open a terminal and change your directory to /Library/Android/sdk/
default image is read only. so you have to change it to writeble. under sdk directory change to tools directory 
type command ` ./emulator -avd huawei_T3_10_API_24 -writable-system&lt;code class=&quot;highlighter-rouge&quot;&gt;
this will enable you copy your local host file to emulator. 
open another terminal. under /Library/Android/sdk/platorm_tools directory,type command: &lt;/code&gt;./adb root &amp;amp;&amp;amp; ./adb -s emulator-5554 remount &amp;amp;&amp;amp; ./adb -s &lt;your_device_name&gt; push /etc/hosts /system/etc/hosts`
if your don't what your device_name is. type './adb devices' in your terminal. You would see all your devices. This command will copy your local machine host file to emulator.&lt;/your_device_name&gt;&lt;/p&gt;

&lt;p&gt;now, you can check whether it successfully push to your emulator. type &lt;code class=&quot;highlighter-rouge&quot;&gt;./adb shell&lt;/code&gt; in your terminal, it will lunch a shell. type &lt;code class=&quot;highlighter-rouge&quot;&gt;cat etc/host&lt;/code&gt; you will see your host from your emulator.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/41117715/how-to-edit-etc-hosts-file-in-android-studio-emulator-running-in-nougat&quot;&gt;How to edit /etc/hosts file in Android Studio emulator running in nougat?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.android.com/studio/run/emulator-networking&quot;&gt;Set up Android Emulator networking&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><summary type="html">Flutter network connection issues(faild host lookup) I tried to call api from emulator to my host machine and I got following error message E/flutter (16241): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: Failed host lookup: 'us1.uszcn.local' (OS Error: No address associated with hostname, errno = 7) It took me one hour to figure out what’s the issues is.</summary></entry><entry><title type="html">Ruby symbol vs string!</title><link href="https://iheng.github.io/Hello-World/" rel="alternate" type="text/html" title="Ruby symbol vs string!" /><published>2019-05-10T00:00:00+00:00</published><updated>2019-05-10T00:00:00+00:00</updated><id>https://iheng.github.io/Hello-World</id><content type="html" xml:base="https://iheng.github.io/Hello-World/">&lt;p&gt;Ruby has this unique datatype called symbol. it’s very simlar to string. but there is one important difference it is immutable. The second thing is it share its memory with other symbol if they have same name. for Example: if you define :username. later you define again this symbol :username. it’s the same object id ,which means it share the same memory with the symbol we defined early.&lt;/p&gt;</content><author><name></name></author><summary type="html">Ruby has this unique datatype called symbol. it’s very simlar to string. but there is one important difference it is immutable. The second thing is it share its memory with other symbol if they have same name. for Example: if you define :username. later you define again this symbol :username. it’s the same object id ,which means it share the same memory with the symbol we defined early.</summary></entry></feed>