|
| 1 | +--- |
| 2 | +title: "(Docker) Go Docker 빌드 및 실행" |
| 3 | +categories: |
| 4 | + - Docker |
| 5 | +tags: |
| 6 | + - Go |
| 7 | + - Docker |
| 8 | +comments: true |
| 9 | +--- |
| 10 | + |
| 11 | +# (macOS) Docker 설치 |
| 12 | +Install Docker Desktop on Mac |
| 13 | +https://docs.docker.com/docker-for-mac/install/ |
| 14 | + |
| 15 | +# Docker image 빌드 및 실행 |
| 16 | +- (Dockerfile 없으면) Dockerfile 생성 |
| 17 | + |
| 18 | +``` |
| 19 | +# Dockerfile References: https://docs.docker.com/engine/reference/builder/ |
| 20 | +
|
| 21 | +# Start from the latest golang base image |
| 22 | +FROM golang:latest |
| 23 | +
|
| 24 | +# Add Maintainer Info |
| 25 | +LABEL maintainer="mioscode <mioscode@email.com>" |
| 26 | +
|
| 27 | +# Set the Current Working Directory inside the container |
| 28 | +WORKDIR /app |
| 29 | +
|
| 30 | +## Copy go mod and sum files |
| 31 | +# COPY go.mod go.sum ./ |
| 32 | +
|
| 33 | +## Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed |
| 34 | +# RUN go mod download |
| 35 | +
|
| 36 | +# Copy the source from the current directory to the Working Directory inside the container |
| 37 | +COPY . . |
| 38 | +
|
| 39 | +# |
| 40 | +RUN go get github.com/labstack/echo \ |
| 41 | + && go get github.com/dgrijalva/jwt-go |
| 42 | +
|
| 43 | +# Build the Go app |
| 44 | +RUN go build -o main . |
| 45 | +
|
| 46 | +# Expose port 8080 to the outside world |
| 47 | +EXPOSE 8080 |
| 48 | +
|
| 49 | +# Command to run the executable |
| 50 | +CMD ["./main"] |
| 51 | +``` |
| 52 | + |
| 53 | +- build the Docker image |
| 54 | + |
| 55 | +``` |
| 56 | +$ docker build -t project_name . |
| 57 | +``` |
| 58 | + |
| 59 | +- You can list all the available images by typing the following command |
| 60 | + |
| 61 | +``` |
| 62 | +$ docker image ls |
| 63 | +REPOSITORY TAG IMAGE ID CREATED SIZE |
| 64 | +go-docker latest ed03a0732734 21 seconds ago 830MB |
| 65 | +golang latest 2422e4d43e15 |
| 66 | +``` |
| 67 | + |
| 68 | +- Running the Docker image |
| 69 | + |
| 70 | +``` |
| 71 | +$ docker run -d -p 8080:8080 project_name |
| 72 | +84e43b3651aa0ebf7b1ed843127dd2f4d179bab006e6bcf55a873772f838e99c |
| 73 | +``` |
| 74 | + |
| 75 | +- Finding Running containers |
| 76 | + |
| 77 | +``` |
| 78 | +$ docker container ls |
| 79 | +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 80 | +84e43b3651aa project_name "./main" 8 minutes ago Up 8 minutes 0.0.0.0:8080->8080/tcp practical_shirley |
| 81 | +``` |
| 82 | + |
| 83 | +- Stopping the container |
| 84 | + |
| 85 | +``` |
| 86 | +$ docker container stop 84e43b3651aa |
| 87 | +84e43b3651aa |
| 88 | +``` |
0 commit comments