iT邦幫忙

0

撰寫一個可以讓 vscode 連入已安裝 python 的 ubuntu 的 dockerfile

  • 分享至 

  • xImage
  •  

前言

因為工作需求要去了解如何使用 python 去操作 Linux kernel 中的 inotify。所以需要在 Linux 環境下使用 python。
這時候去運行一個 Linux 的 container 用來做實驗會很方便。因此首先要撰寫 dockerfile。

前置動作

  • 安裝 docker desktop
  • 在本地端有 vscode

撰寫 dockerfile

FROM ubuntu:latest

# Install dependencies
RUN apt-get update && apt-get install -y \
    software-properties-common \
    wget \
    build-essential \
    openssh-server

# Install python 3.12
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update && apt-get install -y python3.12 python3.12-venv python3.12-dev python3-pip

WORKDIR /app

RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/^#\?PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]`
  • 為了讓使用者可以使用 ssh 登入,需要安裝 openssh-server,讓運行的容器成為 SSH server
  • 為了以 root 身份來登入,並且密碼為 root
    • 設定密碼
    • 使用 Linux 的 sed 指令去改寫 sshd 的設定檔。在設定檔搜尋 PermitRootLogin, PasswordAuthentication 的 pattern 並改成我們想要的

Build & Run

docker build -t my-python-ubuntu .
docker run -d -p 2222:22 --name my-python-container my-python-ubuntu

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
Penut Chen
iT邦新手 2 級 ‧ 2024-07-15 08:19:55

這邊推薦一下 VSCode 的 Container Development 可以參考看看~

我要留言

立即登入留言