# Docker實作營

## 實作步驟及摘要

1  環境準備

2  搜尋並取得映像檔&#x20;

3 利用映像檔建立容器

* 網路設定
  * 建立容器時，使用網路預設值（即bridge）
  * 建立容器時，設定port mapping
  * 建立容器時，使用host network
* 同步容器資料
  * 同步本機資料
* 傳入環境變數

4 撰寫及使用Dockerfile

* git clone from Github
* 調整Dockerfile後，建立映像檔

5 Docker Compose

6 Docker Push到倉庫

## 實作1 環境準備

* 申請Docker帳號（<https://hub.docker.com/>）
* 登入`Play with Docker`（<https://labs.play-with-docker.com/>）

![](/files/-MCzKUrQ4AP641LvgyvL)

* 按下`Start`開始

![](/files/-MCzMFZqNEKyFXv5vHhy)

* 按下左邊的 `+ ADD NEW INSTANCE`，即可開始作業

![](/files/-MCzMjJkDg1ugiXY5bwH)

## 實作2 搜尋並取得映像檔

&#x20;用`docker search`搜尋`nginx`&#x20;

```
$ docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13505               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1841                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   780                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   122                                     
bitnami/nginx                      Bitnami nginx Docker Image                      87                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   85                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   72                                      
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   71                                      [OK]
```

* 在`Docker Hub`搜尋`nginx`，並過濾`tag = 1.13-alpine`，找到`docker pull`指令

![](/files/-MCzPUg5ANwWDxxb15-0)

* 執行`docker pull nginx:1.13-alpine`下載`nginx`映像檔

## 實作3 利用映像檔建立容器

* 利用`docker pull`下載`cutejaneii/docker.python_demo`映像檔
* 將上述映像檔，再上一個新的tag : `myapp`
* 利用`docker images`查看所有映像檔

![](/files/-MDDhbNkUtJ4DAXe6tJd)

* 用`myapp`映像檔建立容器
  * 建立容器時，使用網路預設值（即bridge）
  * 建立容器時，設定port mapping
  * 建立容器時，使用host network

建立完成後，可用`curl`測試看看是否成功

```
$ curl 127.0.0.1:8080
Hello World! (Python 2.7.16) everyone!!
$ curl 127.0.0.1:80
Hello World! (Python 2.7.16) everyone!!
```

* 同步容器資料
  * 同步本機資料

本機建立一個app資料夾，加入以下的main.py，再用-v的方式建立容器，`docker run -d -p 8888:80 -v /app:/app myapp`

```
import os
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Good Morning!!!!! " + os.environ['owner']

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True, port=80)
```

最後用`curl`測試看看是否成功

```
$ curl 127.0.0.1:8888
Good Morning!!!!! everyone!!
```

* 傳入環境變數 `docker run -d -p 9999:80 -e "owner=Jennifer" myapp`

用`curl`測試看看是否成功

```
$ curl 127.0.0.1:9999
Hello World (Python 2.7.16) Jennifer
```

## 實作4 撰寫及使用Dockerfile

* git clone from Github

```
$ git clone https://github.com/cutejaneii/docker.python_demo.git
Cloning into 'docker.python_demo'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (7/7), done.
Receiving objects: 100% (10/10), done.
remote: Total 10 (delta 1), reused 0 (delta 0), pack-reused 0
Resolving deltas: 100% (1/1), done.
```

* 調整Dockerfile後，建立映像檔

```
$ docker build -t myapp .
```

## 實作5 Docker Compose

撰寫docker-compose.yml

```
  version: '3'
  services:
    app:
      image: myapp
      ports:
        - 5566:80
      restart: always
      environment:
        - owner=LIAO
```

執行`docker-compose`

```
$ docker-compose -f docker-compose.yml up
Recreating root_app_1 ... done
Attaching to root_app_1
app_1  |  * Serving Flask app "main" (lazy loading)
app_1  |  * Environment: production
app_1  |    WARNING: This is a development server. Do not use it in a production deployment.
app_1  |    Use a production WSGI server instead.
app_1  |  * Debug mode: on
app_1  |  * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
app_1  |  * Restarting with stat
app_1  |  * Debugger is active!
app_1  |  * Debugger PIN: 162-450-668
```

用`curl`看看

```
$ curl 192.168.0.8:5566
Hello World! (Python 2.7.16) LIAO
```

## 實作5 Docker Push到倉庫

Docker Push到公用倉庫Docker Hub

* Docker Login

```
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: cutejaneii
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
```

* Docker Tag：在推送到自己的repository之前，要先tag

```
$ docker tag myapp cutejaneii/myapp
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cutejaneii/myapp    latest              7aba68965779        40 minutes ago      696MB
myapp               latest              7aba68965779        40 minutes ago      696MB
```

* Docker Push

```
$ docker push cutejaneii/myapp
The push refers to repository [docker.io/cutejaneii/myapp]
4e5b25e56255: Pushed 
27ca79121151: Pushed 
e1a81b1589ce: Pushed 
f8093b983c20: Pushed 
ebac8a5e2f5b: Pushed 
ebd94299f5d0: Pushed 
ff1c70957ceb: Pushed 
1bb42387f7cb: Pushed 
94119923fb5e: Pushed 
a11003618325: Pushed 
668c51fd0cef: Pushed 
latest: digest: sha256:cd0d1e36474560d95f081fb96efe9d3182802a196a89fd9b2f617b058515d807 size: 2635
```

成功後在Docker Hub上就可以看到這個映像檔

![](/files/-MDIBkHN2NUWd74G2MMC)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cutejaneii.gitbook.io/docker/lets-docker/docker-shi-zuo-ying.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
