---
# System prepended metadata

title: Docker
tags: [infrastructure]

---

---
title: Docker
---

# Docker
###### tags: `infrastructure`

[Toc]

### build images
1. 寫dockerfile
範例如下，再根據需要調整
```
# syntax=docker/dockerfile:1

FROM golang:1.19.1-alpine3.16

WORKDIR /app

ADD * /app/

RUN go mod tidy

RUN go build

EXPOSE 3000

CMD [ "./api_serever" ]  # run application
```

2. build images
```
docker build --tag api_server .
```

### push image to hub
1. docker login -> 輸入帳密
2. 重新tag image, 預防之前的image tag不全 ``` docker tag api_server ronnieHubName/api_server```
3. docker push ronnieHubName/api_server



### docker network
垃圾海中找到的精華文章:
1. https://www.liyaxiaomishu.com/docker/docker-guides/networking-with-standalone-containers
2. https://www.digitalocean.com/community/questions/how-to-ping-docker-container-from-another-container-by-name

作為文章的補充：
1. 許多container不帶有ping, 也會鎖上sudo不讓使用者額外安裝，造成無法用文章中的ping做測試。 可以用curl代替。 ex: curl myes:9200 (service discovery 有效下用service name)

### container connect to host service
Example: connect to host redis:
```
host.docker.internal:6379
```
host.docker.internal 會解析到host
