0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

実務で役立つかもしれないPython3のdockerイメージの作成の流れ

0
Posted at

Python3 のdocker イメージを用いたコンテナ起動とイメージ化の流れ

imageの取得

docker pull python

コンテナ起動

docker container run --name py3 -it -d python:latest /bin/bash

対話モードでゴニョゴニョする

docker exec -it py3 /bin/bash

ディレクトリ構成の確認

ls -al
cd /usr/local/src
ls -al
>>> ..
>>> .

別のターミナルで、ホストOS側のソースコードをコンテナ内の指定フォルダにコピーする

cd src
docker cp . py3:/usr/local/src/

コンテナに入って仮想環境作成

# 仮想環境を作成する
python3 -V
>>> 3.14
python3 -m venv .venv
. .venv/bin/activate
(venv)python3 -m pip install --upgrade pip
(venv)python3 -m pip install uv
(venv)uv pip install -r requirements.txt

起動スクリプトを仕込む

apt update
apt upgrade

apt install nano
touch start.sh
chmod 0755 start.sh
nano start.sh

nanoを起動後、スクリプトを作成する。

. .venv/bin/activate
python3 -m httpd

別のターミナルからコンテナをイメージとして保存しておく。

docker commit py3 httpd

最終的にコンテナを実行時に入力するコマンド

docker container run --name httpd-container -p 8099:8099 -w /usr/local/src/ -it -d httpd /bin/bash /usr/local/src/start.sh
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?