Today I Learn - 45

leliko·2021년 4월 21일
0

클라우드 엔지니어

목록 보기
46/53
post-thumbnail

Golang with Docker Image

sudo apt install golang

고랭을 설치한다.

mkdir go-hello && cd go-hello

디렉토리를 만들어서 go 파일을 생성하고 vi로 Hello Go가 나오도록 코드를 작성한다.

hello.go

package main

import "fmt"
func main() {
  fmt.Println("Hello Go!")
}
go run hello.go

코딩한 파일을 실행하여 본다.

go build -o hello hello.go 

코딩한 파일을 빌드한다.

Dockerfile

FROM scratch
COPY hello /hello
CMD ["/hello"]
docker build -t gohello .
docker run gohello

도커파일을 작성하여 고랭으로 코딩한 파일을 실행하는 이미지를 만든다.

Multi Stage Docker Build

FROM golang
ADD main.go /root
WORKDIR /root
RUN go build -o /root/mainAPP /root/main.go

#FROM alpine:latest
FROM scratch
WORKDIR /root
COPY --from=0 /root/mainApp .
CMD ["./mainApp"]
  • C언어 스태틱 빌드 --멀티 스테이지--> 이미지 생성
FROM gcc
WORKDIR /usr/local/src
ADD hello.c .
RUN gcc hello.c --static -o hello

FROM scratch
COPY --from=0 /usr/local/src/hello /hello
CMD ["./hello"]
  • Go언어 ---멀티 스테이지--> 이미지 생성
FROM golang as gobuilder
WORKDIR /go/src
ADD hello.go .
RUN go build -o hello hello.go

FROM scratch
COPY --from=gobuilder /go/src/hello /hello
CMD ["./hello"]

Go with Web

package main

import (
	"fmt"
	"net/http"
)

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

func HelloServer(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello world\n")
}
FROM golang as gobuilder
ENV CGO_ENABLED 0
WORKDIR /go/src
ADD web.go .
RUN go build -o web web.go

FROM scratch
COPY --from=gobuilder /go/src/web /web
CMD ["./web"]
EXPOSE 3000

.dockerignore

https://docs.docker.com/engine/reference/builder/#dockerignore-file

ADD 또는 COPY 할 때 제외할 파일을 지정

  • 로그
  • 임시
  • 민감한 인증/정보 파일
  • 패키지
mkdir logs
touch logs/web.log
touch logs/err.log
mkdir temp
touch temp/abc.txt temp/xyz.txt
touch hello.go
touch a b c

Dockerfile

FROM ubuntu:focal
RUN mkdir /source
WORKDIR /source
ADD . .

.dockerignore

Dockerfile
logs/
temp/
*.go
docker build --no-cache -t ignore .
docker run -it ignore bash
> ls -a

Docker Client with Mac

wget https://download.docker.com/mac/static/stable/x86_64/docker-20.10.6.tgz
tar xf docker-20.10.6.tgz
sudo cp docker/docker /usr/local/bin

Docker Client and Remote Daemon with Windows

choco install docker-cli

systemctl status docker
cat /etc/systemd/system/multi-user.target.wants/docker.service

Docker Client --> Socket File --> Docker Daemon

/run/containerd/containerd.sock
/var/run/docker.sock
sudo vi /etc/systemd/system/multi-user.target.wants/docker.service
13: ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
>>
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
sudo systemctl daemon-reload
sudo systemctl restart docker
systemctl status docker -l

Windows 에서...

docker -H tcp://192.168.100.10:2375 ps
setx DOCKER_HOST "tcp://192.168.100.10:2375"
docker ps

Docker Compose

Docker Compose Install

https://docs.docker.com/compose/install/#install-compose-on-linux-systems

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Docker Compose with Wordpress

mkdir wp-compose && cd wp-compose

https://hub.docker.com/_/wordpress

vi docker-compose.yml
version: '3.1'

services: # docker run
  wordpress:
    image: wordpress
    restart: always
    ports: # -p
      - 80:80
    environment: # -e
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wpadmin
      WORDPRESS_DB_PASSWORD: qwer1234
      WORDPRESS_DB_NAME: wp
    volumes: # -v
      - wordpress:/var/www/html
    networks: # --network
      - wp-net

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: wp
      MYSQL_USER: wpadmin
      MYSQL_PASSWORD: qwer1234
      MYSQL_ROOT_PASSWORD: qwer1234
    volumes:
      - db:/var/lib/mysql
    networks:
      - wp-net

volumes: # docker volume create
  wordpress: # docker volume create wordpress
  db:		 # docker volume create db
  
networks: # docker network create
  wp-net: # docker network create wp-net
docker-compose up -d
docker-compose ps
docker ps
docker volume ls
docker network ls
docker-compose down

Docker Compose with Dockerfile Image Build

mkdir ~/go-web-compose

web.go

package main

import (
        "fmt"
        "net/http"
)

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

func HelloServer(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello world\n")
}

Dockerfile

FROM golang as gobuilder
ENV CGO_ENABLED 0
WORKDIR /go/src
ADD web.go .
RUN go build -o web web.go

FROM scratch
COPY --from=gobuilder /go/src/web /web
CMD ["./web"]
EXPOSE 3000

docker-compose.yml

version: '3.1'

services:
  goweb:
    build: .
    ports:
      - 80:3000
docker-compose up -d
docker-compose ps
docker ps
docker-compose down

Docker Compose Scaling

mkdir compose-scaling
cd compose-scaling

docker-compose.yml

version: '3.1'

services:
  web:
    image: httpd
docker-compose up -d
docker-compose ps
docker-compose scale web=10
docker-compose ps
docker ps	
docker-compose down
profile
클라우드 엔지니어 지망자

0개의 댓글