DEV Community

Cover image for Docker mount Local folder
JU DaDao
JU DaDao

Posted on • Edited on

Docker mount Local folder

Docker file

Dokcer file用來根據自己的需求從既有的image 創建 新的image

建立docker file

# 建立資料夾
mkdir pro_dock
cd pro_dock
Enter fullscreen mode Exit fullscreen mode

於目標資料夾中撰寫docker file,直接創建檔案,檔案名為Dockerfile,沒有副檔名。

以下示簡單的Dockerfile

# 下載指定的python 版本
FROM python:3.9-slim

# 指定工作目錄,之後創建container後資料會在其中進行處理
WORKDIR /code
Enter fullscreen mode Exit fullscreen mode

如果希望從,從這邊開始整體的安裝環境已經設置好。可以安置需要的程式

可以跑

RUN {執行需要的指令,如同在CMD中的指令}
# ex. RUN apt-get update && apt-get install -y python2.7
Enter fullscreen mode Exit fullscreen mode

如果希望從檔案中安裝,而不是一個個慢慢裝,可以準備一個requirements.txt

requirements.txt : (以flask為例子)

Flask==1.1.1
gunicorn==20.1.0
Enter fullscreen mode Exit fullscreen mode
RUN pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

實用技巧

一般來說docker內部,頃向不讓人永久保存資料在container中,因此大部分都會希望資料是在本機端的電腦中,而container處理環境問題。

為了將本機端的資料在container內能使用必須要有兩個

  1. mount: 將本機端的資料夾掛載進去container

  2. ln: (非必須)建立symbol link,在大型專案之中因為各module拆開,就算資料夾mount進去,你也不希望自己的folder汙染到現有的結構。因此從gitlab 或是github clone下來之後。在外部創建資料夾之後ln進去。

在一般的mout中

docker run -it -d -v (computer dir}:{container dir} --name {container name}
Enter fullscreen mode Exit fullscreen mode

可以將指定的本機端位置,掛載到指定的container 的dir

接著建立ln,進入docker container中

cd {你希望建立ln的資料夾}

ln -s {你mount進去,想要建立ln的原始資料夾位置}
Enter fullscreen mode Exit fullscreen mode

Image of Bright Data

Global Data Access Unlocked – Reach data across borders without restrictions.

Unlock the power of global data collection with our advanced proxy solutions. Ideal for market research and more.

Unlock Data Now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay