Posts

Showing posts from March, 2021

Docker compose

Image
This is a study note from a tutorial video in YouTube:  https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4723s Docker compose is used to run several differences services in one .yml file.   docker run WeibotopHistory   docker run mariadb  docker-compose.yml services:     web:          image: "WeibotopHistory"     database:          image: "mariadb" Use "docker-compose" command to bring up all the application stacks in yml files.  docker-compose up  How to compose yml files? To explain how to compose the yml files, we use the voting app example. The structure of voting app is like below: These connections between difference services can be described using "run --link" command like below:  docker run -d --name=redis redis   docker run -d --name=db postgres:9.4   docker run -d --name=vote -p 5000:80 --link redis:redis voting-app   docker run -d -...

Networking in docker

Image
This is a study note from a tutorial video in YouTube:  https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4723s There are three options of networks in docker. Bridge(default), host and none. Bridge  docker run ubuntu  Bridge is the default network in docker.  %3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%22172.17.0.3%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3Dnone%3BfillColor%3Dnone%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22380%22%20y%3D%22330%22%20width%3D%2240%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E Internally docker will give container IP addresses ranging with 172.17.0.X. Docker0(172.17.0.1) is a bridge which is used by all containers to communicate with each other. If you want the container to communic...

Differences between CMD and ENTRYPOINT in Docker

This is a study note from a tutorial video in YouTube:  https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4723s In Dockerfile, you can specify commands to execute like below: Dockerfile: FROM java:8 EXPOSE 8080 CMD ["java", "-jar", "/WeibotopHistorySpring.jar"] The example has a command to execute a java program using JSON format. The first parameter "java" is an executable and the others are the parameters of "java" program.  However in some cases we may want to change the parameter of the executable during a docker run. For example, you have below simple Dockerfile: FROM Ubuntu EXPOSE 8080 CMD ["sleep", "5"] When run above Dockerfile, it will run a container and sleep for 5 seconds. What if we want it to sleep 10 seconds instead? You can run a container with a sleep 10 command because the command appended in docker run command will replace the default command in Dockerfile.  docker run ubuntu-sleep sleep 10  Howeve...

Docker commands

Image
This post is written as a study note of docker tutorial in YouTube  https://www.youtube.com/watch?v=fqMOX6JJhGo&t=1182s , and also cheat sheet for future references during work.  run - start a container  docker run image_name  This command will try to find the image locally in the docker host first. If it cannot find it, it will look for it in docker hub. If image is found in docker hub, it will pull the image down from there and run it. Once the image is downloaded from docker hub, the second time run will not trigger the downloading again from docker hub. ps - list containers  docker ps  This command is used to list all the running containers with some basic information. It is kinda like "ls" command to list all the files in the current folder.  docker ps -a  This command is used to list all the running containers as well as the recent stopped containers. inspect - inspect container  docker inspect silly_sammet  You can use this co...

Array assignment in Go

Array assignment in Go is very different from some other programming languages like Java, C/C+++...  When you assign an array to another array variable in C/C++ or Java, it will not create new copy of the array, the two array variables are referencing the same memory for array. However in Go, Array in Go is considered as a value instead of a pointer like other programming languages. When assign an array variable to another array variable, it will create a copy of the first array, then assign it to the second array, which means the two array variables are referencing different memories. See the example below: package main import ( "fmt" ) func main () { var a [ 3 ] int = [...] int { 1 , 1 , 1 } var b [ 3 ] int b = a b[ 0 ] = 2 b[ 1 ] = 2 b[ 2 ] = 2 fmt. Println (a) fmt. Println (b) } The output is: [1 1 1] [2 2 2] This also applies to passing array to a function as parameters. It will also create a copy of array, then pass to functi...

Bit manipulation using Enum in Go

 This method is learned from Youtube online course  https://www.youtube.com/watch?v=YS4e4q9oBaU&t=5713s Sometimes developers may want to use one byte to represent a combination of settings. In this case, each bit represents "Yes" or "No".  So here you can use Enum in Go to define each bit with special meaning quickly. This allows us to manipulate each bit efficiently. Below is an example code from the video I mentioned in the top. package main import ( "fmt" ) const ( isAdmin = 1 << iota isHeadquarters canSeeFinancials canSeeAfrica canSeeAsia canSeeEurope canSeeNorthAmerica canSeeSouthAmerica ) func main () { var roles byte = isAdmin | canSeeFinancials | canSeeEurope fmt. Printf ( "%b \n " , roles) fmt. Printf ( "Is Admin? %v" , isAdmin&roles == isAdmin) } The output is: 100101 Is Admin? true This is a very good way to manage a set of settings like role management, a...

How to pass object to between Activity objects without using Serializable or Parcelable

Using Parcelable to pass objects between Activity objects is the correct way to do so. However, occasionally developers may encounter the scenario where the object you want to pass has its class defined in another library which you don't have access to the source code. In this case, you cannot implements Serializable or Parcelable interfaces. In this case, you can create a wrapper class implement the interfaces. But if you don't want to do this, there is another solution as well. You can use JSON to solve this problem. In sender activity, convert the object you want to pass to JSON string. And in receiver activity, convert the JSON string back to the object.  Code in sender activity: Intent intent = new Intent ( this , ReceiverActivity . class ); Bundle bundle = new Bundle (); GsonBuilder builder = new GsonBuilder (); Gson gson = builder . create (); String objectToPassStr = gson . toJson (objectToPass); // Convert object to JSON bundle . putString ( "param...

How to extend MySQL query in C# application

 In C# application, when select a pretty large table using MySQL library, you may get an exception "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding" . This is simply because the select operation take more than 30s, then it will give a timeout exception. 30s is the default setting for MySqlCommand . To extend the query command is very simple: just set the CommandTimeout property in MySqlCommand to a larger value. Below is the complete code for a query session where you can see that the timeout for query command is extended to the maximum of integer, and then load data into a DataTable object. string conStr = "Database=10.10.10.10;Data Source=testdb;User Id=testuser;Password=testpw" ; MySqlConnection connection = new MySqlConnection ( conStr ); connection . Open (); MySqlCommand cmd = new MySqlCommand ( "SELECT * FROM my_table" , connection ); cmd . CommandTimeout = int . MaxValue ;...

Calculate data size using Enum in Go

This method is learned from Youtube online course https://www.youtube.com/watch?v=YS4e4q9oBaU&t=5713s I found it quite interesting and a nice demo to understand Enum in Go. The code is like below: package main import ( "fmt" ) const ( _ = iota //ignore first value by assigning to blank identifier KB = 1 << ( 10 * iota ) MB GB TB PB ZB YB ) func main () { fileSize := 4000000000. fmt. Printf ( "%.2fGB" , fileSize/GB) } Output: 3.73GB Here constants KB, MB, GB, TB, PB, ZB and YB are used to calculate the responding size by dividing the constant value. Since they are written in upper case, which means they can be used in other packages as well. So it is a very useful tool.

Variable scope in Go

There are only three levels of variable scope in Go: package level, globe level and block level. Package Level: Package level variables are accessible inside the package. They are defined directly on the package level and started with lower case . For example: package main import "fmt" var counter int = 42 func main() {     fmt.Println(counter); } Globe Level: Globe level variables can be accessed outside of the package. Similar to package level variables, they are also defined directly on the package level, however they must be started in upper case . For example: package main import "fmt" var Counter int = 42 //Here Counter is accessible outside of the package func main() {     fmt.Println(Counter); } Globe level variables can be accessed by other packages using packagename.VariableName  after import the package . Block Level: Block level is like local variables in other programming languages. They are defined in a block like functions, if statement or else. p...