[Docker] Error: Can’t not access internet from container in Ubuntu 14.04

When I use Docker, I create container from image (Ex: Ubuntu:14.04) and ssh to this container by command:

docker run -it <image_id> bash

When I ssh to container, I used command ping google.com 

but I get error:  ping: unknown host google.com

To fix it, I have 2 solutions, but first, you must get DNS of your machine by command

nm-tool 

Result in my machine:

DNS: 192.168.30.29
DNS: 192.168.30.66

=> I’ll use DNS: 192.168.30.29

1. Solution 1

– Go to file sudo nano /etc/docker/daemon.json and add your DNS to this file

{
    "dns": ["192.168.30.29", "8.8.8.8", "8.8.4.4"]
}

Save it and restart docker by command: sudo service docker restart

2. Solution 2

solution 1- Go to file sudo nano /etc/default/docker, uncomment and add your DNS to this row:

DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"

Save it and restart docker by command: sudo service docker restart

If when restart, you get error:

docker stop/waiting
start: Job failed to start

or error:

stop: Unknown instance: 
start: Job failed to start

– You shoud go to file sudo nano /etc/default/docker, and comment row

DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"

and try again with solution 1.

==> After you try adding DNS successfully, you can ssh to container and run

ping google.com once again. And this is my result after try fixing with solution 1

root@2beb109bc7b3:~# ping google.com
PING google.com (113.171.239.123) 56(84) bytes of data.
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=1 ttl=56 time=1.06 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=2 ttl=56 time=1.51 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=3 ttl=56 time=2.32 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=4 ttl=56 time=1.19 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=5 ttl=56 time=1.55 ms
.....

That’s all. Thank you for reading. 🙂

[Docker] Error Cannot connect to the Docker daemon. Is the docker daemon running on this host?

In Ubuntu, when I install Docker successfully, I try running with command:

docker images #show all images in local

but it has error

Cannot connect to the Docker daemon. Is the docker daemon running on 
this host?

because it doesn’t have permission of root. I can run it by command

sudo docker images

Another way, we can config it to needn’t use sudo

  • Step 1: go to /var/run

    cd /var/run
  • Step 2: Add permission for file docker.sock
    sudo chmod -R 776 docker.sock
    

And now, we can use command docker images for shorter code

 

Error: AbstractController::DoubleRenderError when render in Rails

When work, I’ve had bug when use 2 render error 404 and 422. 

When I go to link: http://localhost:3000/products/cloths.js. Firstly, controller will understand params {name=cloths.js} and will select in database with name=cloths.js and not found any record with that name, then the website will use function render_404.

After that, website will check 422 error because the link has .js and use function render_422. At this time, this error will appear:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like “redirect_to(…) and return”.)

To fix error, I use this code in function render after

self.response_body = nil

It will

# Clear the previous response body to avoid a DoubleRenderError
# when redirecting or rendering another view

Example for my problem:

In function 404, which render before:

def render_404
   if request.xhr? || request.format.json?
     render json: { meta: { message: "404 not found" } }, status: :not_found, layout: false
   else
     render file: path_to_error_file(404), status: :not_found, layout: false
   end
 end

And in function 422, which render after and has error  AbstractController::DoubleRenderError

def render_422
  self.response_body = nil
  if request.xhr? || request.format.json?
    render json: { meta: { message: "422 Error" } }, status: :422_error, layout: false
  else
   render file: path_to_error_file(422), status: :422_error, layout: false, 
      content_type: 'text/html'
  end
 end

Access remote server with local phpMyAdmin in Ubuntu

1

Default, the login page of phpmyadmin have only username & password. If you want to add some remote server you can follow by some steps:

  1. Find file “/etc/phpmyadmin/config.inc.php”
  2. Find row “/* Advance to next server for rest of config */” and add some configs below “$i++;”

/* Advance to next server for rest of config */
$i++;

/* some additional configs  */
$cfg[‘Servers’][$i][‘host’] = ‘52.69.150.47’;
$cfg[‘Servers’][$i][‘user’] = ‘mery_box’;
$cfg[‘Servers’][$i][‘password’] = ‘TewKszmceNQA’;
$cfg[‘Servers’][$i][‘auth_type’] = ‘config’;

And the result:

1

Show log of MySQL when running query in rails

When you run query to select, update, insert… in ruby, you can show log realtime when query is running.

First, go to MySQL and run:

show variables like '%log%';

And find record with variable_name =  general_log_file and get its value. In case, value is /var/lib/mysql/OFF016.log

mysql

Then, set

set global general_log = 1;

Then, open terminal and run:

sudo tailf <value>

In case is:

sudo tailf /var/lib/mysql/OFF016.log

Error: “#1071 – Specified key was too long; max key length is 767 bytes” when import mysql db

When import db to MySQL, if it has error: #1071 – Specified key was too long; max key length is 767 bytes.

Go SQL command to run some query to fix it.

use my_database_name;

set global innodb_large_prefix=on;

set global innodb_file_format=Barracuda;

set global innodb_file_per_table=true;

And import again. 🙂

=================================

You can also watch video to fix it easily:

Fixed: 767 bytes error video

Paperclip AV Transcoder can not upload video

When use gem paperclip-av-transcoder to upload video and get error:

Av::UnableToDetect - Unable to detect any supported library:
  av (0.9.0) lib/av/cli.rb:10:in `initialize'
  av (0.9.0) lib/av.rb:24:in `cli'
  paperclip-av-transcoder (0.6.4) lib/paperclip/paperclip_processors/transcoder.rb:14:in `initialize'

=> May be your computer missing ffmpeg and libav-tools

To install them:

// libav-tools
sudo apt-get install libav-tools

// ffmpeg
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg gstreamer0.10-ffmpeg

====================================================================

And if after install them, we get error:

Av::CommandError - error while running command avconv -loglevel quiet -i

Let’s uninstall them and reinstall ffmpeg static build

// Uninstall  
sudo apt-get remove ffmpeg  
sudo apt-get purge libav-tools

// install ffmpeg static build
wget http://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz
tar xf ffmpeg-git-64bit-static.tar.xz
cd ffmpeg-git-64bit-static
sudo mv ff* qt-faststart /usr/bin/

Can’t show group chat Skype in Ubuntu 14.04

I installed Skype in Ubuntu 14.04,  but when I logged in to Skype, I only show and chat with my friends and can’t show any groups, I fixed it by the way:

Open chat box with any my friend and type:

/dumpmsnp   #show details about your connection

And I showed log:

System: MSNP: Connection Data:
 * Status: LoggedOut

Ref:It’s LoggedOut   => It is problem

To solve it, type:

/msnp24

Then log out and restart Skype. After that, type again:

/dumpmsnp 

And I see:

System: MSNP: Connection Data (MSNP24):
 * Status: LoggedIn

And I also see all my groups in Skype, and the issue was solved.

Ref:  https://community.skype.com/t5/Mac-archive/New-chat-groups-not-showing-up-in-client/td-p/3206434

[ActiveAdmin] Conditionally show buttons on show page

For customize buttons in page show in ActiveAdmin, we use:

First, we remove the default buttons from the show page:

# Remove the buttons from the show page
config.action_items.delete_if { |item| item.display_on?(:show) }

Then, add Edit button with conditions

# Add Edit Button with conditional, in show page
action_item :edit,  only: [ :show ] do
  if condition # (Ex: product.is_published?)
    link_to "Edit Product", edit_product_path(resource)
  end
end

And, add Delete button

# and our Delete Button
action_item :delete,  only: [ :show ] do
  link_to "Delete", resource_path(resource), method: :delete, 
      data: { confirm: I18n.t('active_admin.delete_confirmation') }
end