Sunday, 11 April 2021

Interfaces in Go

 This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12098s

Interface is a collection of function prototypes. You can define interface like below:

type Writer interface {
Write([]byte) (int, error)
}

Interfaces should be implemented as methods.

type ConsoleWriter struct{}

func (cw ConsoleWriter) Write(data []byte) (int, error) {
n, err := fmt.Println(string(data))
return n, err
}

Polymorphism

You can use interface to realise polymorphism like below:

func main() {
var w Writer = ConsoleWriter{} // Here can use different structs
//with different implementation
w.Write([]byte("Hello go!"))

}

Implicit implementation

Except polymorphism, you can use interface to do implicit implementation. What does implicit implementation means? Since you don't need to use "implements" keyword to implement an interface like JAVA and some other languages, interfaces decouple the definitions from their implementations. There are a lot of packages which utilise structs with fields and methods. So you can design your own interfaces which can utilise the methods in the structs.

Use any type to implement interfaces

You don't have to use struct to implement interfaces. In fact, you can use any type to implement interfaces.

func main() {
myInt := IntCounter(0)
var inc Incrementer = &myInt
for i := 0; i < 10; i++ {
fmt.Println(inc.Increment())
}
}

type Incrementer interface { // define interface
Increment() int
}

type IntCounter int // define int type

func (ic *IntCounter) Increment() int { // implement interface
*ic++ // Here is important, it increment the type itself which is int
return int(*ic)
}

Interface embedding

You can embed interface into another interface like structs. Below is a writer example code from the tutorial video.

func main() {
var wc WriterCloser = NewBufferredWriterCloser()
wc.Write([]byte("Hello Go developers, this is a test"))
wc.Close()
}

type Writer interface {
Write([]byte) (int, error)
}

type Closer interface {
Close() error
}

type WriterCloser interface {
Writer // Here two interfaces are
Closer // embedded in composer interface
}

type BufferedWriterCloser struct {
buffer *bytes.Buffer
}

func (bwc *BufferedWriterCloser) Write(data []byte) (int, error) {
n, err := bwc.buffer.Write(data)
if err != nil {
return 0, err
}
v := make([]byte, 8)
for bwc.buffer.Len() > 8 {
_, err := bwc.buffer.Read(v)
if err != nil {
return 0, err
}
_, err = fmt.Println(string(v))
if err != nil {
return 0, err
}

}
return n, nil
}

func (bwc *BufferedWriterCloser) Close() error {
for bwc.buffer.Len() > 0 {
data := bwc.buffer.Next(8)
_, err := fmt.Println(string(data))
if err != nil {
return err
}
}
return nil
}

func NewBufferredWriterCloser() *BufferedWriterCloser {
return &BufferedWriterCloser{
buffer: bytes.NewBuffer([]byte{}),
}
}

Interface type conversion

Interface variable can be converted to the value type (structs or other types) using dot oeprtation.

func main() {
var wc WriterCloser = NewBufferredWriterCloser()
wc.Write([]byte("Hello Go developers, this is a test"))
//wc.Close()

bwc := wc.(*BufferedWriterCloser) // Here wc is converted to
// to struct directly, in
// this way you can access
// the buffer field directly
fmt.Println(bwc.buffer)
}

You can check the if the conversion is successful or failed by checking the second return value from the conversion operation.

func main() {
r, ok := wc.(io.Reader)
if ok {
fmt.Println(r)
} else {
fmt.Println("Conversion failed")
}
}

The result is:
Conversion failed

Empty interface and type conversion

Anything in Go can be an empty interface. This is useful when you want to get a value and then later want to covert it onto others. Because empty interface has no method in it. So in order to use it, you have to do a type conversion.
func main() {
var myObj interface{} = NewBufferredWriterCloser()
if wc, ok := myObj.(io.WriteCloser); ok { // Here do convertion
wc.Write([]byte("Hello Go developers, this is a test"))
wc.Close()
}

r, ok := myObj.(io.Reader)
if ok {
fmt.Println(r)
} else {
fmt.Println("Conversion failed")
}
}

Type switch

You can use switch to check the type of a variable.
func main() {
var i interface{} = 0
switch i.(type) {
case int:
fmt.Println("i is an integer")
case string:
fmt.Println("i is a string")
default:
fmt.Println("I don't know what it is")

}
}





References: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12098s

Wednesday, 7 April 2021

Turn off the startup chime permanently using terminal

 The startup chime is the only reason I want to smash my Macbook Pro sometimes. And Apple didn't provide an easy way to turn this thing off. I could not find the setting below in Apple support although it mentioned that it should be there for MacOS Big Sur 11 or later. However I am using the latest MacOS Catalina 10, but I still cannot find it.

But I found a solution that works for me.

You can try the below command to turn it off.

 sudo nvram StartupMute=%01 

If you want to turn it on.

 sudo nvram StartupMute=%00 

For me, I prefer to keeping it mute forever.










Sunday, 4 April 2021

Special handling of functions in Go

This is a study note for YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s

This post only talks about some special parts of Go functions.

Function name

Like variables, functions starting with lower case will keep private to the package, however functions starting with upper case will be exposed to the outside of the package.

Function parameters

1, if the function has parameters with same type, you can simply put the type at the last parameter in the function. For example.
func main() {
greet("Hello", "Tiffany")
}

func greet(greeting, name string) {
println(greeting, name)
}

The output is:
Hello Tiffany

2, you can use variadic parameters in Go.

func main() {
sum(1, 2, 3, 4, 5)
}

func sum(values ...int) {
sum := 0
for _, v := range values {
sum += v
}
fmt.Println("The sum is ", sum)
}

The output is:
The sum is 15

You can use variadic parameter with other parameter, but the function can only have one variadic parameter and it must be at the end.

Function return

1, When define function, put the return type after the closing bracket ")".

func main() {
fmt.Println("The result is: ", sum(1, 2, 3, 4, 5))
}

func sum(values ...int) int {
sum := 0
for _, v := range values {
sum += v
}
return sum
}

2, You can return a pointer from a function as well. Unlike in C/C++, when you turn a pointer in call stack, you can get as undefined behaviour because the stack will be destroyed when the function returns. Thus, you cannot control what is in the pointer now. However in Go, when Go detect you are returning a pointer from stack, it will automatically promote this variable from stack to heap, so you don't need to be worry about the value in the pointer is destroyed.

func main() {
fmt.Println("The result is: ", *sum(1, 2, 3, 4, 5))
}

func sum(values ...int) *int {
sum := 0
for _, v := range values {
sum += v
}
return &sum
}

3, You can use multiple returns in Go functions. The most useful cases of multiple returns is to add an error message in a function to indicate if the function execution is successful.

func main() {
result, err := divide(5.0, 0.0)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(result)
}

func divide(a, b float64) (float64, error) {
if b == 0.0 {
return 0.0, fmt.Errorf("cannot divide by zero")
}
return a / b, nil
}

Function as a variable

You can use function as a value to assign a variable.
func main() {
f := func() {
fmt.Println("Hello world")
}
f()
}

You can specify the signature of the function like below.

func main() {

var divide func(float64, float64) (float64, error)
divide = func(f1, f2 float64) (float64, error) {
if f2 == 0.0 {
return 0.0, fmt.Errorf("cannot divide by zero")
}
return f1 / f2, nil
}
result, err := divide(5.0, 0.0)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(result)
}

Method

You can create a function executing in a known context (like struct, int... any type actually, but most of cases are used with structs).

func main() {
g := greeter{
greeting: "Hello",
name: "Tiffany",
}
g.greet()

}

type greeter struct {
greeting string
name string
}

func (g greeter) greet() { // (g greeter) make this function a method
fmt.Println(g.greeting, g.name)
}




Reference:  https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s

Defer, panic and recover in Go

 This is a study note for YouTube: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s

Except conditional statement and looping, you can also use defer, panic and recover to manage control flow in Go.

Defer

You can use "defer" keyword to defer the execution of a function when the calling function exits but before it returns. For example:

func main() {
fmt.Println("Start!")
defer fmt.Println("Middle!")
fmt.Println("End!")
}
The result is:
Start! End! Middle!

Deferred functions are executed in LIFO (Last In First Out) order. For exampe:

func main() {
defer fmt.Println("Start!")
defer fmt.Println("Middle!")
defer fmt.Println("End!")
}

The result is:

End! Middle! Start!

Defer function is very useful, one example is that you can use refer function to do some connection closing, resource clean up tasks. For example, in below code, you can put a close function right after you establish the connection before you even start to use the connection. This is because the close function is deferred, it will only execute after the calling function exits.

func main() {
res, err := http.Get("http://www.google.com/robots.txt")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
robots, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%ss\n", robots)
}

Another important thing to mention is that the deferred functions only take the variable value from the time is deferred, the change after the deferred function call will not impact the input value in the deferred function. For example:

func main() {
a := "start"
defer fmt.Println(a)
a = "end"
}

The result is:

start

You can see that the value is still "start" even though variable "a" was changed to "end" later.

Panic

Panic is kinda like Exception in other programming language like JAVA. When panic function is executed, it will log the the position where causes the panic and then exit.

func main() {

fmt.Println("start")
panic("something bad happened")
fmt.Println("end")
}

The result is:

(base)MyLaptop$ go run src/github.com/mingdos/firstapp/Main.go
start
panic: something bad happened
goroutine 1 [running]:
main.main()
       /Users/caimingda/go/src/github.com/mingdos/firstapp/Main.go:10 +0x95

exit status 2

When works with deferred function, The execution order is like below:

Normal execution -> (defer function calling) -> normal execution -> (panic happens) -> defer function execution -> panic function execution

func main() {

fmt.Println("start")
    defer fmt.Println("this was deferred")
panic("something bad happened")
fmt.Println("end")
}

The result is:

(base)MyLaptop$ go run src/github.com/mingdos/firstapp/Main.go
start
this was deferred
panic: something bad happened

goroutine 1 [running]:
main.main()
       /Users/caimingda/go/src/github.com/mingdos/firstapp/Main.go:11 +0x95

exit status 2



Recover

In defer function, you can call recover() function to check if the current program is panic. The recover() function return nil if the program is not panic, and it will return the error message when it is panic.
func main() {

fmt.Println("start")
defer func() {
if err := recover(); err != nil {
fmt.Println("Error: ", err)
}
}()
panic("something bad happened")
fmt.Println("end")
}

The result is:

start Error: something bad happened

The recover() function does more than this actually. It will recover the execution from where after the caller function who cause the panic inside and continue to execute the rest.

func main() {

fmt.Println("start")
panicer()
fmt.Println("end")
}

func panicer() {
fmt.Println("about to panic")
defer func() {
if err := recover(); err != nil {
fmt.Println("Error: ", err)
}
}()
panic("something bad happened")
fmt.Println("Done panic")
}

The result is:

start about to panic Error: something bad happened end

If the defer function don't know how to handle the panic, it actually can redo a panic when it detect a panic. In this way, the caller function will still have the panic.


Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s

Friday, 2 April 2021

Special handing of conditional statements in Go

This is a study note of YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Here I will only discuss the special usage of conditional statements in Go compared to other traditional programming languages like Java, C/C++...

Initialiser syntax

Here I give an example directly from the tutorial.

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614273,
}
if pop, ok := statePopulations["Florida"]; ok {
fmt.Println(pop)
}
}

The result is:

20612439

There are two parts in above if statement if pop, ok := statePopulations["Florida"]; ok. The first part before the semicolon is the initialiser where you can initialise some variables. The second part after the semicolon is the condition. Be aware that the variables defined in the initialiser part are only alive in the if block. If you try to use the variable ("pop" and "ok") after the if statement, you will get an undefined variable error.

switch statement

Unlike other programming languages where switch statement needs to use "break" to skip the following cases. In Go, by default it only executes the statements in the case block, then exit the switch statement (In fact, in GO you can use "break" as well to exit early where you need it to).

func main() {
switch 2 {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
default:
fmt.Println("not one or two")
}
}

The result is:

two

You can also put multiple conditions in one case like below:

func main() {
switch 5 {
case 1, 5, 10:
fmt.Println("one, five or ten")
case 2, 4, 6:
fmt.Println("two, four or fix")
default:
fmt.Println("other number")
}
}

The result is:

one, five or ten

You can also use initialise in switch statement:

func main() {
switch i := 2 + 3; i {
case 1, 5, 10:
fmt.Println("one, five or ten")
case 2, 4, 6:
fmt.Println("two, four or fix")
default:
fmt.Println("other number")
}
}

The result is:

one, five or ten



Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Structs in Go

 This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

You can define a struct and initialise it using following example:

type Doctor struct {
Number int
ActorName string
Companions []string
}

func main() {
aDoctor := Doctor{
Number: 3,
ActorName: "Jon Pertwee",
Companions: []string{
"Liz Shaw",
"Jo Grant",
"Sarah Jane Smith",
},
}
fmt.Println(aDoctor)
fmt.Println(aDoctor.ActorName)
}

You can dot operator (".") to access the fields in a struct.

Anonymous struct 

You can create an anonymous struct for informal uses in Go.

func main() {
aDoctor := struct{ name string }{name: "John Pertwee"}
fmt.Println(aDoctor)
}


Struct is a value type

Unlike map is a reference type, struct is a value type. This means if you assign one struct to another, the struct will be made a copy and assign to the other. Any modification in one struct won't impact the others.

func main() {
aDoctor := struct{ name string }{name: "John Pertwee"}
anotherDoctor := aDoctor
anotherDoctor.name = "Tom Baker"
fmt.Println(aDoctor)
fmt.Println(anotherDoctor)
}

The result is:

{John Pertwee} {Tom Baker}

Same like array, if you want two struct refer to the same location, you can use pointer to do so.

func main() {
aDoctor := struct{ name string }{name: "John Pertwee"}
anotherDoctor := &aDoctor // Pass the address of aDoctor to anotherDoctor
anotherDoctor.name = "Tom Baker"
fmt.Println(aDoctor)
fmt.Println(anotherDoctor)
}

The result is:

{Tom Baker} &{Tom Baker}

Embedded type of struct

You can embed a struct in another struct by simply declaring the embedding struct in the other. For example:

type Animal struct {
Name string
Origin string
}

type Bird struct {
Animal
SpeedKPH float32
CanFly bool
}

func main() {
b := Bird{}
b.Name = "Emu"
b.Origin = "Australia"
b.SpeedKPH = 48
b.CanFly = false

fmt.Println(b)
}

The result is:

{{Emu Australia} 48 false}

You can also initialise the struct in a structured way, however you can still refer the fields in the embedded struct directly.

func main() {
b := Bird{
Animal: Animal{Name: "Emu", Origin: "Australia"},
SpeedKPH: 48,
CanFly: false,
}
fmt.Println(b.Name)
}

The result is:

Emu




Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Maps in Go

 This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Maps can be created in Go using type map[string]int{}.
func main() {

statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
fmt.Println(statePopulations)
}

You can also use make function to create map:

func main() {
statePopulations := make(map[string]int) // you can use make(map[string]int, 100)
                                             // to specify length as well
statePopulations = map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
fmt.Println(statePopulations)
}

Like dictionary in C#, you can simply access a map value using key - statePopulations["Ohio"].

You can also add key-value pair in map like dictionary in c#:

statePopulations["Georgia"] = 10210371

You can use delete built-in function to delete a key in map:

delete(statePopulations, "Texas")

The first parameter is the map, and second parameter is the key you want to delete.

What if you access a key which does not exist in the map?

Will this give an error or exception? The answer is neither.

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
fmt.Println(statePopulations["Georgia"])
}

The result is:

0

From the example above, you can see the value is 0 when key does not exist. This will cause problem if you use the value directly because you don't know "0" means key does's exist or the population is 0. However there is a solution: you can use comma separated expression to get another boolean value from map. 

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}

population, ok := statePopulations["Georgia"]
fmt.Println(population, ok)
}

The result is:

0 false

If you don't care about the value for now, you can simple use the write-only operator "_" to skip the value and get the true/false result only.

_, ok := statePopulations["Georgia"]
fmt.Println(ok)


Map is also a reference type, so if you have two map variable refer to the same memory, when one is modified, the other is modified as well.

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
a := statePopulations
delete(a, "Ohio")
fmt.Println(a)
fmt.Println(statePopulations)
}

In above example, you can see the result that, "Ohio" is removed from both "statePopulations" and "a" referencces.



Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Thursday, 1 April 2021

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 though only b was changed.

Slice operations


There are different ways to create slices.
func main() {

a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
b := a[:] // slice of all elements
c := a[3:] // slice from 4th element to end
d := a[:6] // slice first 6 elements
e := a[3:6] // slice the 4th, 5th and 6th elements

fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)

}
The result is:
[1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 6 7 8 9 10] [4 5 6 7 8 9 10] [1 2 3 4 5 6] [4 5 6]

To make easier to remember, if we represent the creation operation using format "[left:right]", it will get the data ranging from left to right. Value in left is inclusive while value in right is exclusive.

Here I emphasise again that slice is a reference type. No matter what above methods you use, all slice still refer to the same memory. Once you change value in one slice, it will be changed in all other slices.
For example:
func main() {

a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
b := a[:] // slice of all elements
c := a[3:] // slice from 4th element to end
d := a[:6] // slice first 6 elements
e := a[3:6] // slice the 4th, 5th and 6th elements

e[1] = 0 // Here modify the 2nd value to be 0
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
}
The result is:
[1 2 3 4 0 6 7 8 9 10] [1 2 3 4 0 6 7 8 9 10] [4 0 6 7 8 9 10] [1 2 3 4 0 6] [4 0 6]

The value in the same memory in different slices is changed as well.

Slice creation using make function


You can also use "make" function to create a slice. The make built-in function allocates and initialises an object of type slice, map, or chan(only). 

Make function can have two parameters or three parameters.

Two-parameter make function prototype is like below:
make(type, length)

func main() {

a := make([]int, 3)
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))

}
The result is:
[0 0 0] Length: 3 Capacity: 3

Three-parameter make function prototype is:
make(type, length, capacity)

func main() {

a := make([]int, 3, 100)
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))

}
The result is:
[0 0 0] Length: 3 Capacity: 100

Add elements in slice using append function

func main() {

a := []int{}
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))
a = append(a, 1)
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))
a = append(a, 2, 3, 4, 5)
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))
}
The result is:
[] Length: 0 Capacity: 0 [1]
Length: 1
Capacity: 1
[1 2 3 4 5] Length: 5 Capacity: 6

Append a slice to a slice

If you want to append a slice to another slice, you cannot do so by just using a = append(a, b) because append function only accepts the same type as the slice element - it cannot accept slice as an input.

However you can use spread operator to archive this - a = append(a, b...). It is same as a = append(a, 2, 3, 4, 5). 

func main() {

a := []int{1}
b := []int{2, 3, 4, 5}
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))
a = append(a, b...)
fmt.Println(a)
fmt.Printf("Length: %v\n", len(a))
fmt.Printf("Capacity: %v\n", cap(a))

}
The result is:
[1] Length: 1 Capacity: 1
[1 2 3 4 5] Length: 5 Capacity: 6

Remove operation in slice

To remove the first element.
func main() {

a := []int{1, 2, 3, 4, 5}
b := a[1:]
fmt.Println(b)
}
The result is:
[2 3 4 5]

To remove the last element.
func main() {

a := []int{1, 2, 3, 4, 5}
b := a[:len(a)-1]
fmt.Println(b)
}

The result is:
[1 2 3 4]

To remove the element in the middle.
func main() {

a := []int{1, 2, 3, 4, 5}
b := append(a[:2], a[3:]...)
fmt.Println(b)
}
The result is:
[1 2 4 5]

However, as mentioned before, slice is a reference type. When you modify a slice, it will impact all other slice variables that refer to it.
When you do fmt.Println(a), you will get result like below:
[1 2 4 5 5]

From above you can see that remove operation impact the original slice a as well. So be aware of this!



Docker registry

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

If you didn't login the private registry, it will tell you "the image cannot be found". So remember to login before push/pull to/from the private registry.

When you use AWS or GCP, they will provide a private registry to you automatically. If you want to deploy a private registry on your own, you can do so as well.

Registry in docker is actually a docker image exposing itself on port 5000. So you can use "run" command to expose the registry like below:

 docker run -d -p 5000:5000 --name registry registry:2  

Use "image tag" command to tag the image with the private registry url in it.

 docker image tag my-image localhost:5000/my-image 

Then use "push/pull" command to push/pull the image.

 docker push localhost:5000/my-image 

 docker pull localhost:5000/my-image 



Reference: https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4048s

Chapter 3 — The Molt

(This story is written by me and I will be posting it on Royal Road. Profile: https://www.royalroad.com/profile/1129550/fictions) Six fifty ...