0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonの簡易http.serverをDockerでたててファイルを公開する。

Posted at

この記事の目的

  • Pythonの簡易WEBサーバーをDockerコンテナで起動し、ファイルを公開するためのメモ。

手順

Dockerfileを作成する

FROM python:latest #任意のバージョンを指定してください

WORKDIR /publish_data

EXPOSE 50080

CMD [ "python", "-m", "http.server", "-b", "0.0.0.0", "50080" ]

公開するファイルを格納するディレクトリと確認用ファイルを作成する

mkdir publish_data
touch ./publish_data/test.txt

Dockerイメージをビルドする

docker image build \
-f ./Dockerfile \
-t python_web_server:1.0.0 \
.

Dockerコンテナを起動する

docker container run \
-itd \
--name python_web_server \
-p 10080:10080 \
-v ${PWD}/publish_data:/publish_data \
python_web_server:1.0.0

接続確認をする

(base) user@host ~ % curl hostname:50080
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Directory listing for /</title>
</head>
<body>
<h1>Directory listing for /</h1>
<hr>
<ul>
<li><a href="test.txt">test.txt</a></li>
</ul>
<hr>
</body>
</html>
(base) user@host ~ %

WEBブラウザから確認する

image.png

本番運用はしないように公式ドキュメントで警告されています。
しかしながら、チームや組織内で一時的にファイルを公開する必要がある時などは、便利かも知れません。
以上です。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?