---
# System prepended metadata

title: Containarize an application
tags: [docker]

---

# Containarize an application

refer to [official documentation](https://docs.docker.com/get-started/workshop/02_our_app/)


## 創建container
基本上都照著[步驟](https://hlunlun.github.io/)做就行
- 創建映像檔
    ```
    sudo docker build -t <image name> .
    ```
- 
:warning: 如果使用者不是root,所有docker指令前面都要加`sudo`

## 開發在遠端Ubuntu，跑在Windows的瀏覽器
refer to: [Using localhost for to access running](https://forums.docker.com/t/using-localhost-for-to-access-running-container/3148) container
1. 執行剛剛創建好的映象檔
    ```
    sudo docker run -d -p 3000:3000 docker_app
    ```
2. 查看執行的process
    ```
    sudo docker ps
    ```
3. 到本地端的瀏覽器拜訪網址: [localhost:3000](http://localhost:3000/)
    

## Remove ids and process of docker
1. Remove process
    ```
    sudo docker stop <id of process>
    ```
2. 查看所有執行中或已停止的process
    ```
    sudo docker ps -aq
    ```
3. 移除所有已停止的container
    ```
    sudo docker container prune
    ```
    
4. 移除不管是在執行中還是停止的所有container
    ```
    sudo docker rm -f $(sudo docker ps -aq)
    ```



## Host vs. Bridge
1. 如果要讓外網可以連到docker，`network`要用`host`
    ```
    sudo docker run -d --network host -p 3000:3000 <image_name>
    ```
    