Python 2.7 is installed by default on Mac, so if you need a Python 3.x environment, you can use Pipenv or I think that you often build an environment using pyenv.
However, installation failed and it took a lot of time to set up, so here is a summary of how to build a Python 3.x environment using Docker.
Download and install "Docker Desktop for Mac" from the following URL. https://hub.docker.com/editions/community/docker-ce-desktop-mac
After the Docker installation is complete, run the "docker version" command to see if it's installed correctly.
$ docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
Create a folder of your choice as a Python project folder and place the Python source files in that folder.
$ cd /Users/username/work/python/helloworld
$ echo 'print("Hello World!")' > helloworld.py
Execute the "docker run" command to start the container that will be the execution environment for Python.
Specify the following options.
--- i (--interactive): Attach to the standard input of the container.
--- t (--tty): Assigns a pseudo terminal (pseudo-TTY).
----rm: Automatically delete the container when the container is closed.
--- v (--volume): Mount the local folder on the container. (
For the Docker image, use "python: 3.7-alpine" published on Docker Hub. To use.
"/ Bin / sh" at the end of the command is the command executed when the container is started.
$ docker run -it --rm -v /Users/username/work/python/helloworld:/helloworld python:3.7-alpine /bin/sh
After the container starts, execute the following command to check if it is working properly.
/ # ls
bin etc home media opt root sbin sys usr
dev helloworld lib mnt proc run srv tmp var
/ # python --version
Python 3.7.5
Go to your project folder and run the Python file.
/ # cd helloworld/
/helloworld # python helloworld.py
Hello World!
Executing the "exit" command will exit the container.
/helloworld # exit
Since the container is initialized at startup, you need to install the required packages at each startup.
That's a hassle, so create a new Docker image so that the required packages are installed when the container starts.
Create a Dockerfile in your project folder.
Dockerfile
#Specify the base Docker image
FROM python:3.7.5-alpine
#Specify the project folder
ARG project_dir=/helloworld/
# requirements.Copy txt to container
ADD requirements.txt $project_dir
# requirements.Install the package written in txt
WORKDIR $project_dir
RUN pip install -r requirements.txt
Run the "docker build" command to create a new Docker image "hello world".
$ docker build -t helloworld .
If you specify the newly created Docker image "hello world" and execute the "docker run" command, the package described in requirements.txt will be installed when the container is started.
$ docker run -it --rm -v /Users/username/work/python/helloworld:/helloworld helloworld /bin/sh
Recommended Posts