---
# System prepended metadata

title: Dockerfile
tags: [docker, Linux]

---

# Dockerfile

## Dockerfile的基本語法

```dockerfile
FROM 底層映像檔 (Apache、Nginx、CentOS、Ubuntu...)
USER 執行容器的使用者
WORKDIR 工作目錄 (若原先沒有,會自動生成)

LABEL 維護資訊

ENV 建立環境變數
ARG --build-arg 帶入變數，讓build可以結合外部指定建構時所需的參數

COPY 複製本機資料到容器
ADD 複製url或是tar檔(自動解壓縮)到目錄

VOLUME 建立一個可以從本幾對外掛載的目錄
EXPOSE 容器執行的port (web_server: 80 443...)

RUN 執行指令
CMD 容器啟動後會執行的命令，可以有多個CMD，不過只有最後一個會生效 (之前試過的 bash、sh...)
ENTRYPOINT 指定容器啟動程式及參數
```

## Dockerfile test

* 建立一個測試用的資料夾 (為方便之後下指令, Dockerfile 的D請大寫)
```bash=
[user@docker1 dockerfile_test]$ tree
.
├── Dockerfile
├── index.html
└── default.conf
```
* Dockerfile
```dockerfile= 
FROM nginx
WORKDIR /app
EXPOSE 80
COPY index.html /usr/share/nginx/html
```
* index.html
```html=
test for Dockerfile
```
* default.conf
```nginx=
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page 500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
```
* build (建立 image)
```bash=
$ sudo docker build -t test:0.7 .    # 依照編輯版本給tag
[+] Building 0.8s (9/9) FINISHED                                                                                    docker:default
 => [internal] load build definition from Dockerfile                                                                          0.0s
 => => transferring dockerfile: 158B                                                                                          0.0s
 => [internal] load .dockerignore                                                                                             0.0s
 => => transferring context: 2B                                                                                               0.0s
 => [internal] load metadata for docker.io/library/nginx:latest                                                               0.7s
 => [1/4] FROM docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee                0.0s
 => [internal] load build context                                                                                             0.0s
 => => transferring context: 363B                                                                                             0.0s
 => CACHED [2/4] WORKDIR /app                                                                                                 0.0s
 => CACHED [3/4] COPY index.html /usr/share/nginx/html                                                                        0.0s
 => [4/4] COPY default.conf /etc/nginx/conf.d/default.conf                                                                    0.0s
 => exporting to image                                                                                                        0.0s
 => => exporting layers                                                                                                       0.0s
 => => writing image sha256:e89cd8dba1433500120a042b19b7a6291f2ecfe5133ab9c1cb3a475f194411e8                                  0.0s
 => => naming to docker.io/library/test:0.7                                
```

* run
```bash=
[user@docker1 dockerfile_test]$ sudo docker run -d --rm --name www -p 8081:80 test:0.7
ad332a99a93b706f1a2ce9b4af568bbf6fa5cb460268976b4dc0b2a69010b889
[user@docker1 dockerfile_test]$ sudo docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED         STATUS                 PORTS                                   NAMES
ad332a99a93b   test:0.7                          "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes           0.0.0.0:8081->80/tcp, :::8081->80/tcp   www
```
![image](https://hackmd.io/_uploads/SkcWhveBT.png)

## Referrnce page
[DockerFile 教學](https://hi-founder.com/p/dockerfile-%E6%95%99%E5%AD%B8/#dockerfile-%E8%A8%AD%E5%AE%9A%E6%AA%94%E5%9F%BA%E6%9C%AC%E8%AA%9E%E6%B3%95)