1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

macOSにDocker+Go+Gin環境をインストールしました。

1
Posted at

概要

Docker,Go初心者がmacOSにDocker+Go+Gin環境をインストールしました。

環境

macOS Tahoe 26.6.2
docker.desktop 4.40.0
go version go1.26.4 darwin/arm64

やったこと

mkdir sleep_bak_go
cd sleep_bak_go
vi Dockerfile
Dockerfile
# 1. Goのバージョンを指定
FROM golang:1.26.4

# 2. 必要なパッケージをインストール
RUN apt-get update && apt-get install -y git

# 3. 作業ディレクトリを設定
WORKDIR /go/src/app

# 4. ホストマシンからアプリケーションファイルをコンテナにコピー
COPY ./app .

# 5. Goアプリケーションを実行
CMD ["go", "run", "."]
vi docker-compose.yml
docker-compose.yml
services:
  app:
    container_name: sleep-bak-go  # コンテナの名前を指定
    build:
      context: .
      dockerfile: ./Dockerfile
    tty: true  # コンテナを終了させない
    restart: always
    volumes:
      - ./app:/go/src/app  # ホストのappフォルダをコンテナにマウント
    ports:
      - xxxx:xxxx  # ローカルホストとコンテナのポートをバインド
    environment:
      - TZ=Asia/Tokyo    
mkdir ./app
cd ./app
go mod init sleep-bak-go
go get -u github.com/gin-gonic/gin
vi main.go
main.go
package main

import (
  "github.com/gin-gonic/gin"
)

func main() {
  // Ginのルーターを作成
  router := gin.Default()

  // ルートエンドポイントを定義し、Hello Worldを返す
  router.GET("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
      "message": "Hello World",
    })
  })

  // ポート7070でサーバーを起動
  router.Run(":7070")
}
go run main.go

立ち上がりました。

curl http://localhost:7070/
{"message":"Hello World"}%

確認できました。

cd ..
docker compose up --build

Dockerコンテナが立ち上がりました。

curl http://localhost:7070/
{"message":"Hello World"}%                                     

確認できました。

データーベースと接続するネットワークを作成してネットワークに参加させました。

docker network create sleep
docker network connect sleep ec_db
docker network connect sleep sleep-bak-go

Dockerhubでリポジトリー作成。

git init
git remote add origin git@github.com:tyoshida431/sleep_bak
vi ./.gitignore
.gitignore
.gitignore
git status
git stage -A
git commit
git push -u origin main

以上メモです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?