Tutorial & Configuration
  • Home
  • Desktop
  • Server
  • Mikrotik
  • Tools
  • Home
  • Desktop
  • Server
  • VirtualBox
  • Cisco
  • Mikrotik
  • Tools
  • About
  • Contact

Minggu, 24 Juli 2022

Docker File

 maouam     Juli 24, 2022     DevOps     No comments   

#Docker Build
## docker build -t namauser/namaimage:tag namafolder
   Example: build -t irhamtaufik/test:1.0 testfolder
   ~ --progress=plain (Melihat progress build)
   ~ --no-cache (build tanpa menggunakan cache)

#Insturction Format
 Example: # Komentar (INSTURCTION arguments)

#From Instruction
   FROM image:version (Membuat build stage image yang kita tentukan)
   Example: FROM alpine:3
===form/Dockerfile===
FROM alpine:3
===End===
----------------

#Run Instruction (Di jalankan Saat di build)
   1. RUN Command
   2. RUN["executable","argument","..."]
   Example: RUN mkdir hello (Membuat folder)
===run/Dockerfile===
FROM alpine:3

RUN mkdir hello
RUN echo "Hello World" > "hello/world.txt"
RUN cat "hello/world.txt"
===End===
----------------

#Command Instruction (Saat container di jalankan)
   1. CMD commnad param param
   2. CMD ["executable","param","param"]
   3. CMD ["param","param"]
   Example: CMD cat "hello/world.txt"  
===command/Dockerfile===
FROM alpine:3

RUN mkdir hello
RUN echo "Hello World" > "hello/world.txt"

CMD cat "hello/world.txt"
===End===
----------------

#Label Instruction (Untuk menambahkan informasi saja)
   1. LABEL <key>=<value>
   2. LABEL key1>=<value1> <key2>=<value2>
   Example: LABEL author=irham
===label/Dockerfile
FROM alpine:3

LABEL author="IrhamTaufik"
LABEL company="Xtend" website="https://test.com"

RUN mkdir hello
RUN echo "Hello World" > "hello/world.txt"

CMD cat "hello/world.txt"
===End===
----------------

#Add Instruction (Digunakan untuk menambahkan file ke docker image termasuk url
#yg di download, jika di zip maka file di unzip )
   ADD source destination
   Example: ADD world.txt hello
            ADD *.txt hello/
===add/Dockerfile===
FROM alpine:3

RUN mkdir hello
ADD text/*.txt hello/


CMD cat "hello/world.txt"
===End===

===add/text/world.txt===
Hello world!
===End====
----------------

#Copy Instruction (Digunakan untuk menambahkan file dari source ke dalam folder
#destination di Docke image)
   COPY source destination
   Example: COPY world.txt hello # menambahkan file world.txt ke folder hello
            COPY *.txt hello
===copy/Dockerfile===
FROM alpine:3

RUN mkdir hello
COPY text/*.txt hello/

CMD cat "hello/world.txt"
==End===

===copy/text/world.txt===
Hello World!
==End===
---------------

#dockerignore file (Digunakan untuk mengabaikan file ataupun folder untuk di add
#ataupun di copy)  
===ignore/Dockerfile
FROM alpine:3

RUN mkdir hello
COPY text/* hello/

CMD ls -l hello
===End===

===ignore/.dockerignore===
text/*.log
text/temp
===End===

===ignore/text/world.txt===
Hello world!
===End===

===ignore/text/*.log
tulis apa saja
===End===

===ignore/text/temp/test.txt
apa aja
===End===
----------------

#Expose Instruction (Digunakan untuk memberikan instuksi bahwa container
#akan listen port pada nomor dan protocol tertentu, ini hanya digunakan
#untuk dokumentasi saja)
 Format Expose
 ~ EXPOSE port # defaultnya menggunakan TCP
 ~ EXPOSE port/TCP
 ~ EXPOSE port/UDP
===expose/Dockerfile
FROM golang:1.18-alpine

RUN mkdir app
COPY main.go app

EXPOSE 8080

CMD go run app/main.go
===End===

===expose/main.go===
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
----------------

#Environment Variable Instruction (Digunakan untuk mengubah environment variable,
#baik itu ketika tahapan build atau ketika jalan dalam docker container)
Format Environment
~ ENV key=value
~ ENV key1=value1 key2=value2
===env/Dockerfile===
FROM golang:1.18-alpine

ENV APP_PORT=8080

RUN mkdir app
COPY main.go app/

EXPOSE ${APP_PORT}

CMD go run app/main.go
===End===

===env/main.go===
package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    port := os.Getenv("APP_PORT")
    fmt.Println("Run app in port : " + port)
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":" + port, nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
----------------

#Volume Instruction
Format Volume
~ VOLUME /lokasi/folder
~ VOLUME /lokasi/folder1 /lokasi/folder2
~ VOLUME ["/lokasi/folder1","/lokasi/folder2",""]
===volume/Dockerfile====
FROM golang:1.18-alpine
ENV APP_PORT=8080
ENV APP_DATA=/logs

RUN mkdir ${APP_DATA}
RUN mkdir app
COPY main.go app/

EXPOSE ${APP_PORT}
VOLUME ${APP_DATA}
CMD go run app/main.go
===End===

===main.go===
package main

import (
        "fmt"
        "io/ioutil"
        "net/http"
        "os"
)

func main() {
        port := os.Getenv("APP_PORT")
        fmt.Println("Run app in port : " + port)
        http.HandleFunc("/", HelloServer)
        http.ListenAndServe(":"+port, nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s", r.URL.Path[1:])

        dataString := "Hello " + r.URL.Path[1:]
        dataBytes := []byte(dataString)

        destination := os.Getenv("APP_DATA")
        file := destination + "/" + r.URL.Path[1:] + ".txt"
        err := ioutil.WriteFile(file, dataBytes, 0666)
        if err != nil {
                panic(err)
        }
        fmt.Println("DONE Write File : " + file)
}
===End===
----------------

#Working Directory Instruction (Digunakan untuk menentukan
#direktori/folder untuk menjalakan instruksi RUN, CMD, ENTRYPOINT, COPY dan ADD)
Format Workdir
~ WORKDIR /app # artinya working directorynya adalah /app
~ WORKING docker # sekarang working directorynya adalah /app/docker
~ WORKING /home/app # sekarang working directorynya adalah /home/app
===workdir/Dockerfile===
FROM golang:1.18-alpine

WORKDIR /app
COPY main.go /app/

EXPOSE 8080
CMD go run main.go
===End===

===main.go===
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
-----------------

#User Insturction (Digunakan untuk menjalakan aplikasi dengan
#user tertentu ketika docker image di jalankan)
Format User
~ USER <user> # mengubah user
~ USER <user>:<group> # mengubah user dan user group
===user/Dockerfile===
FROM golang:1.18-alpine

RUN mkdir /app

RUN addgroup -S pzngroup
RUN adduser -S -D -h /app pznuser pzngroup
RUN chown -R pznuser:pzngroup /app

USER pznuser

COPY main.go /app/

EXPOSE 8080

CMD go run /app/main.go
===End====

===main.go===
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
-------------------

#Argument Instruction (Digunakan untuk mendefenisikan variabel yg bisa digunakan
#oleh pengguna utk dikirim ketika proses docker build menggunakan
#perintah --build-arg key=value)
 Format Argument
 ~ ARG key # membuat argument variable
 ~ ARG key=defaultvalue # membuat argument variable dgn default value jika tdk diisi
===arg/Dockerfile===
FROM golang:1.18-alpine

ARG app=main

RUN mkdir app
COPY main.go app/
RUN mv app/main.go app/${app}.go

EXPOSE 8080

ENV app=${app}
CMD go run app/${app}.go
===End===

===arg/main.go===
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
----------------

#Health Check Instruction (Digunakan untuk memberi tahu bagaimana
#utk mengecek apakah container masih berjalan dgn baik atau tidak.
#nilai awal starting kemudian healty dan unhealty)
 Format healty Check
 ~ HEALTHCHECK NONE # artinya disabled healty Check
 ~ HEALTHCHECK [OPTION] CMD command
   OPTION:
   ~ --interval=DURATION (default: 30s)
   ~ --timeout=DURATION (default: 30S)
   ~ --start-period=DURATION (default: 0s)
   ~ --retries=N (default: 3)
===health/Dockerfile===
FROM golang:1.18-alpine

RUN apk --no-cache add curl
RUN mkdir app
COPY main.go app/

EXPOSE 8080

HEALTHCHECK --interval=5s --start-period=5s CMD curl -f http://localhost:8080/health

CMD go run app/main.go
===End===

===health/main.go===
package main

import (
    "fmt"
    "net/http"
)

var counter = 0

func main() {
    http.HandleFunc("/", HelloServer)
    http.HandleFunc("/health", HealthCheck)

    http.ListenAndServe(":8080", nil)
}

func HealthCheck(w http.ResponseWriter, r *http.Request) {
    counter = counter + 1
    if counter > 5 {
        w.WriteHeader(500)
        fmt.Fprintf(w, "KO")
    } else {
        fmt.Fprintf(w, "OK")
    }
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
------------------

#Entrypoint Instruction (Digunakan untuk menentukan executable file
#yang akan dijalankan oleh container)
 Format Entrypoint
 ~ ENTRYPOINT ["executable","param1","param2"]
 ~ ENTRYPOINT executable param1 param2
 ~ CMD ["param1","param2"] # param tersebut akan dikirim ke ENTRYPOINT
 ===Dockerfile===

 ===End===
 -----------------

#Multi Stage build (Digunakan untuk membuat beberapa build stage atau tahapan build)
===multi/Dockerfile===
FROM golang:1.18-alpine as builder
WORKDIR /app/
COPY main.go /app/
RUN go build -o /app/main /app/main.go

FROM alpine:3
WORKDIR /app/
COPY --from=builder /app/main /app/
CMD /app/main
===End===

===mutli/main.go===
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}
===End===
-----------------

#Docker Hub Registry (Digunakan untuk mendownload atau mengupload docker image)
 ~ docker login -u namauser (masukkan password yg di buat dari menu security)
 ~ docker push namauser/namaimage (Push image ke Registry)


Source: ProgrammerZamanNow
Note: Hanya sebagai catatan saya
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Kirimkan Ini lewat EmailBlogThis!Bagikan ke XBerbagi ke Facebook
Posting Lebih Baru Posting Lama Beranda

0 komentar:

Posting Komentar

Popular Posts

  • Install zenchain node di Ubuntu-Linux (self-host node)
      Install zenchain node di Ubuntu-Linux (self-host node)   #Buat wallet baru di metamask dan import ke OKX juga (EVM) #WL di web (supa...
  • Install 0GDA Node & Client di Ubuntu-Linux
    Install 0GDA Node & Client di Ubuntu-Linux Spec vps CPU 8 core RAM 16 GB Contabo spec VPS-3 #Buat Wallet EVM (buat di OKX simpan p...
  • Install PWR node di Linux-Ubuntu
    Install PWR Node DC PWR: https://discord.com/invite/Mu3ktCk4 #Update paket sudo su apt update #Install java apt install openjdk-19-jre-headl...
  • INSTALL CHASM NODE DI LINUX/UBUNTU
      INSTALL CHASM DI LINUX/UBUNTU   #Buka web https://console.groq.com/keys   | daftar #Jika sudah daftar, buat api di https://console.g...

LABELS

  • AWS
  • Cisco
  • Cloud
  • Desktop
  • DevOps
  • Mikrotik
  • Network
  • Nginx
  • node2an
  • Proxmox
  • Server
  • Tools
  • VirtualBox
  • Zimbra

Sosial Media

Facebook 35.4Fans
Twitter 519Followers
Instagram 1060Followers
google+ 16Followers
Youtube 24Subscriber

Widget

  • Home
  • About Us
  • Contact Us

Copyright © Tutorial & Configuration