0
1

More than 1 year has passed since last update.

『kaggleで勝つデータ分析の技術』を学ぶためのdocker環境の作り方・入り方

Last updated at Posted at 2023-06-18

(2023年6月25日:追記)内容に一部誤りがあったので修正。

状況

データ分析技術を身に付けるため、以下の書籍『kaggleで勝つデータ分析の技術』を学ぼうとしている。xgboost等のライブラリをインストールする必要があったのだが、pipで自分のパソコンに直接インストールするのは何らかの副作用があると考え、pythonの仮想環境を導入してその中で実施することにした。

pythonの仮想環境はvenvで簡単に行うことができるが、dockerの扱いに慣れるためdockerで仮想環境を作成することにした。

手法

初めにdockerfileとrequirements.txtを作成し、以下の通りに記述する。そして下記のコマンドを入力することで仮想環境でpythonのコードを実行できる。

dockerfile

# ベースイメージを指定
FROM python:3.9

# 作業ディレクトリを設定
WORKDIR /app

# 必要なライブラリをインストール
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# ソースコードをコピー
COPY . .

# コンテナ実行時のデフォルトのコマンドを指定
CMD ["bash"]

requirements.txt

numpy==1.21.0
pandas==1.3.0
scikit-learn==0.24.2
matplotlib==3.4.2
seaborn==0.11.1

コマンド

以下のコマンドを入力することで、コンテナ内に作成されたpythonの仮想環境に入ることができる。もし足りないライブラリが存在したら、pip install 欲しいライブラリでインストールする。

# コンテナ立ち上げ
sudo docker build -t my-python .

# pythonコンテナをbashで起動
sudo docker run -it -v /mnt/c/Users/自分の名前/Documents/python/kaggle/kagglebook-master/kagglebook-master:/app my-python
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1