「CentOS7」においてDockerでPythonを実行してみる

スポンサーリンク

前回、AmazonEC2「CentOS7」においてDockerでPythonの環境構築をしてみるという記事を公開しましたが、今回はPythonの環境を構築しましたので、実際にPythonを実行してみます。

Amazonマシンイメージ

「CentOS 7 (x86_64) – with Updates HVM」

Python3.7.5をインストールしたDockerのコンテナに接続する

AmazonEC2で作成した「CentOS7」にPuTTYを使用し接続。ターミナルウインドウを表示。

一般ユーザーからroot権限に切り替えます。

[root@ip-172-31-8-145 centos]# docker ps

上記のコマンドで、コンテナの稼働状況を確認してみます。

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
765722c3531f centos:centos7 “/sbin/init” 2 days ago Up 2 days centos7

確認したのですが、前回作成したはずのPythonをインストールしたコンテナが確認されません。

なぜ、稼働が確認できないのか、調べてみたのですが、「Dockerでpythonコンテナを作成してもすぐに落ちて 困った話」というページを発見して、これが参考になり、再度すべてのコンテナを表示するために、

[root@ip-172-31-8-145 centos]# docker ps -a

上記のコマンドを入力してみます。

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a201af084b6 python:3.7.5-buster "/bin/bash" 24 hours ago Exited (129) 24 hours ago pythontest
8d8f42113e88 python:3.7.5-buster "python3" 24 hours ago Exited (0) 24 hours ago eager_goldwasser
765722c3531f centos:centos7 "/sbin/init" 2 days ago Up 2 days centos7

入力してみると、今度は前回作成したコンテナ、「pythontest」が確認できました。

確認後、コンテナの起動のために、

[root@ip-172-31-8-145 centos]# docker start pythontest

上記のコマンドを入力します。入力後、コンテナが起動します。

起動後、もう一度、稼働状況を確認するために、

[root@ip-172-31-8-145 centos]# docker ps

上記のコマンドを入力します。

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a201af084b6 python:3.7.5-buster “/bin/bash” 24 hours ago Up 9 seconds pythontest
765722c3531f centos:centos7 “/sbin/init” 2 days ago Up 2 days centos7

入力すると、今度は前回作成したコンテナが稼働中であることが確認できました。

[root@ip-172-31-8-145 centos]# docker exec -it 0a201af084b6 /bin/bash

確認後、稼働したコンテナに接続するために、上記のコマンドを入力しますが、

Error response from daemon: Container aa12b0d7dadaa408f7e2ee37a3df61564a050b14ca3da4493ab89880ca8310ab is not running

上記のエラーが発生します。

[root@ip-172-31-8-145 centos]# docker exec -it pythontest /bin/bash

発生後、解決するために、試行錯誤した結果、上記の「CONTAINER ID」ではなく、「CONTAINER NAMES」で接続すると、

root@0a201af084b6:/#

コンテナに接続できることがわかりました。

Vimのインストールを行う

接続後、Dockerコンテナの中ではviエディタが使えないので、vimのインストールを行います。

root@0a201af084b6:/# apt-get update

まず、上記のコマンドを入力します。

Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://deb.debian.org/debian buster InRelease [121 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [204 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7905 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [7868 B]
Fetched 8355 kB in 2s (4534 kB/s)
Reading package lists… Done

上記の結果が出力されます。パッケージリストを読み込んで、完了(Done)となります。

root@0a201af084b6:/# apt-get install vim

次に、上記のコマンドを入力し、vimのインストールを行います。

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
libgpm2 vim-common vim-runtime xxd
Suggested packages:
gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
libgpm2 vim vim-common vim-runtime xxd
0 upgraded, 5 newly installed, 0 to remove and 59 not upgraded.
Need to get 7425 kB of archives.
After this operation, 33.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian buster/main amd64 xxd amd64 2:8.1.0875-5 [140 kB]
Get:2 http://deb.debian.org/debian buster/main amd64 vim-common all 2:8.1.0875-5 [195 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 libgpm2 amd64 1.20.7-5 [35.1 kB]
Get:4 http://deb.debian.org/debian buster/main amd64 vim-runtime all 2:8.1.0875-5 [5775 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 vim amd64 2:8.1.0875-5 [1280 kB]
Fetched 7425 kB in 0s (48.4 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package xxd.
(Reading database … 24526 files and directories currently installed.)
Preparing to unpack …/xxd_2%3a8.1.0875-5_amd64.deb …
Unpacking xxd (2:8.1.0875-5) …
Selecting previously unselected package vim-common.
Preparing to unpack …/vim-common_2%3a8.1.0875-5_all.deb …
Unpacking vim-common (2:8.1.0875-5) …
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack …/libgpm2_1.20.7-5_amd64.deb …
Unpacking libgpm2:amd64 (1.20.7-5) …
Selecting previously unselected package vim-runtime.
Preparing to unpack …/vim-runtime_2%3a8.1.0875-5_all.deb …
Adding ‘diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime’
Adding ‘diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime’
Unpacking vim-runtime (2:8.1.0875-5) …
Selecting previously unselected package vim.
Preparing to unpack …/vim_2%3a8.1.0875-5_amd64.deb …
Unpacking vim (2:8.1.0875-5) …
Setting up libgpm2:amd64 (1.20.7-5) …
Setting up xxd (2:8.1.0875-5) …
Setting up vim-common (2:8.1.0875-5) …
Setting up vim-runtime (2:8.1.0875-5) …
Setting up vim (2:8.1.0875-5) …
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for hicolor-icon-theme (0.17-2) …
Processing triggers for libc-bin (2.28-10) …
Processing triggers for mime-support (3.62) …

vimのインストールが開始され、途中「Do you want to continue? [Y/n] 」と質問されますので、「y」を入力しENTERキーを押すと、インストールは進み完了となります。

Pythonのコードを書いて実行してみる

vimのインストールが完了しましたので、Pythonのコードをviエディタで書いていきます。

まずは、「/home/」にtest.pyというファイルを作りますので、

root@0a201af084b6:/# cd home
root@0a201af084b6:/home# vi test.py

作業ディレクトリを移動するcdコマンドを入力し、「home」に移動します。

移動後、viコマンドでviエディタを起動させます。test.pyのファイルを編集していきます。

■test.py

#-*- coding: utf-8 -*-
print("hello, world")

編集は、「i」を押して、インサートモードに切り替えて、上記のコードを入力します。

入力が完了後はESCキーでインサートモードを終了し、「:wq」を入力し、保存してviエディタを終了します。

root@0a201af084b6:/home# python test.py

終了後、編集したtest.pyのファイルを上記のコマンドで実行してみます。

hello, world

実行してみると、「hello, world」と出力されました。

コメント

タイトルとURLをコピーしました