LoginSignup
10
12

More than 5 years have passed since last update.

Dockerのubuntuによるpython3.6のためのDockerfile

Last updated at Posted at 2017-02-28

はじめに

Python3.6 から追加された文法機能の記事を見かけ、
使ってみようと思った時、とりあえずちょろっと使うだけだからDocker(Ubuntu)で試そうとしました。

しかし、apt-getでpython3.6を入れるには、
- Ubuntu 14.04/16.04: repositoryを追加しなければなない。
- Ubuntu 16.10: repositoryを追加しなくても入れられるが、versionが3.6.0-b2-1
- pipは別途入れなければならない
など微妙に痒いところがあったので、ソースからmakeしてinstallする最低限のDockerfileをメモとして書いておきたいと思います。

※追記(2017/03/10): マルチバイト文字を使うとエラーを吐いたのでENV PYTHONIOENCODINGを追加しました。

Dockerfile

FROM ubuntu:16.10
MAINTAINER kekedadamama

RUN apt-get update -y && apt-get install -yq wget build-essential gcc zlib1g-dev

WORKDIR /root/
RUN wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz \
        && tar zxf Python-3.6.0.tgz \
        && cd Python-3.6.0 \
        && ./configure \
        && make altinstall
ENV PYTHONIOENCODING "utf-8"

WORKDIR /

ここではubuntu:16.10としていますが、ubuntu:16.04でも普通に使えることは確認できました。

※追記(2017/03/10): ENV PYTHONIOENCODINGを設定していない状態でマルチバイト文字を使用すると、以下のようなエラーを吐きます。

>>>
  File "<stdin>", line 0

    ^
SyntaxError: 'ascii' codec can't decode byte 0xe3 in position 7: ordinal not in range(128)

使ってみる

上記Dockerfileを実行したコンテナでpythonの3.6.0が入っていることを確認しておきます。

root@7787ab554c9f:/# python3.6 -V
Python 3.6.0
root@7787ab554c9f:/# python3.6
Python 3.6.0 (default, Feb 28 2017, 08:21:29)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> greet : str = "Hello"
>>> f"{greet}, guest"
'Hello, guest'

3.6で導入された書式化済み文字列リテラル変数のタイプヒンティングシンタックスも問題なく使えていそうです。

もちろんpipも一緒に入ります。

おわりに

apt-getでpython3.6が普通にinstallできるようになるまでの命なこのDockerfileですが、
初めてソースから入れたにしてはすんなり入ってくれていい子ですね。

10
12
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
10
12