Posts

Slices in Go

This is a study note from YouTube Go tutorial  https://www.youtube.com/watch?v=YS4e4q9oBaU Slice is used in most of cases in Go instead of arrays because it is dynamic. This means you can increase the capacity of slice on the runtime. You can create a slice like below: Slice creation using []int a := [] int { 1 , 2 , 3 } It is very similar to array creation except no size in the square bracket "[]". Unlike array is a value type, slice is a reference type like list in Java. So when you create a slice and then assign it to another slice variable, both of them refer to the same memory. package main import ( "fmt" ) func main () { a := [] int { 1 , 2 , 3 } b := a b[ 1 ] = 5 fmt. Println (a) fmt. Println (b) fmt. Printf ( "Length: %v \n " , len (a)) fmt. Printf ( "Capacity: %v \n " , cap (a)) } Result: [1 5 3] [1 5 3] Length: 3 Capacity: 3 From the result, you can see both a and b has the same values even thou...

Docker registry

Image
 This is a study note based on YouTube docker tutorial:  https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4048s  docker run nginx  In docker "run" command, you can specify the image name like "nginx" above. However this is not a full name. The full name should be like below: By default, the registry name is "docker.io" which is the Docker Hub registry if you don't specify any. If you don't specify the user name, it will use the same name as the image name. There are some other registries except the Docker Hub registry like Google registry "gcr.io". Private Registry If you do not want to publish your registry, you can use private registry instead. In order to use private registry, firstly you need to login the private registry using below command:  docker login private-registry.io  Input the username and password in the console. Then you can run a container from the images in private registry.  docker run private-registry.io/apps/internal...

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...