Docker-Compose負責對container做快速編排。設定檔預設名稱為docker-compose.yml,在檔案中,可以透過COMPOSE_FILE或-f對設定進行定義。
想要將專案透過Docker-Compose部署,需要創建docker-compose.yml和Dockerfile。
#取得golang
FROM golang:1.15
# 為image設定必要的環境變量
ENV GO115MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
GOPROXY="xxx"
# 移動到工作目錄:your_file_path
WORKDIR your_file_path
# 將程式碼複製到容器中
COPY . .
# 將我們的程式碼編譯成二進位執行檔 執行檔名為 app
RUN go build -o app .
# 聲明服務端口
EXPOSE 8080
# 啟動容器時執行的命令
CMD ["./app"]
version: '3'
services:
mysql:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
hostname: mysqlServiceHost
network_mode: bridge
ports:
- "3306:3306"
#restart: always
restart: on-failure
volumes:
- ./mysql:/var/lib/mysql
- ./my.cnf:/etc/mysql/conf.d/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d/
- ./your_sql_name.sql:/docker-entrypoint-initdb.d/your_sql_name.sql
environment:
- MYSQL_ROOT_PASSWORD=your_root_password
- MYSQL_USER=your_user_name
- MYSQL_PASSWORD=your_password
- MYSQL_DATABASE=your_database_name
redis:
image: redis:3
container_name: redis
hostname: redisServiceHost
network_mode: bridge
restart: on-failure
ports:
- "6379:6379"
golang:
build: .
restart: on-failure
network_mode: bridge
ports:
- "8080:8080"
links:
- mysql
- redis
volumes:
- your_file_path:/go
tty: true
開啟終端機,進入到“your_file_path”後,執行“docker-compose up -d”建立container。
建構完成後,執行“docker-compose ps”,查看是否建立成功。