[Mac] Build a Python 3.x environment at the fastest speed using Docker

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.

Operating environment

Build a Python 3.x environment with 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

Install packages when container starts

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

[Mac] Build a Python 3.x environment at the fastest speed using Docker
Create a Python development environment locally at the fastest speed (for beginners)
Build a Python development environment on Mac OS X
Build a Python environment on your Mac using pyenv
Build a go environment using Docker
Build a Flask development environment at low cost using Docker
I tried using PyCaret at the fastest speed
[Python] Build a Django development environment with Docker
Build a Python development environment on your Mac
Build a Django development environment using pyenv-virtualenv on Mac
Build a machine learning Python environment on Mac OS
How to build a Django (python) environment on docker
Build a Python development environment using pyenv on MacOS
Build a lightweight Fast API development environment using Docker
Build a Python environment offline
Build a web API server at explosive speed using hug
Easily build a GCP environment for Kaggle at high speed
Build a python data analysis environment on Mac (El Capitan)
Build a Python environment and transfer data to the server
Build a python environment with pyenv (OS X El Capitan 10.11.3)
Create a Python environment on Mac (2017/4)
Build Mysql + Python environment with docker
Build a python3 environment on CentOS7
Build a local development environment for Lambda + Python using Serverless Framework
Try using virtualenv, which can build a virtual environment for Python
Continuation ・ Notes on preparing the Python development environment on Mac OS X
The simplest way to build a Spleeter usage environment using Windows
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build and try an OpenCV & Python environment in minutes using Docker
Build a Python execution environment using GPU with GCP Compute engine
[Mac] Create a Python3 execution environment from the fully initialized state
Using venv in Windows + Docker environment [Python]
Behind the flyer: Using Docker with Python
Create a python environment on your Mac
[Python] Create a Batch environment using AWS-CDK
I want to build a Python environment
[Mac] Building a virtual environment for Python
Build Python environment with Anaconda on Mac
[Linux] Build a jenkins environment with Docker
Build a python virtual environment with pyenv
Build a Python + OpenCV environment on Cloud9
Build a modern Python environment with Neovim
[Linux] Build a Docker environment with Amazon Linux 2
After buying a new Mac, use pyenv + poetry to build a Python environment.
Installation method using the pip command of the Python package (library) Mac environment
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
I tried to build a Mac Python development environment with pythonz + direnv
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
[Python] How to save the installed package and install it in a new environment at once Mac environment
Create a Python development environment in 10 minutes (Mac OS X + Visual Studio Code)
Build Python + django + nginx + MySQL environment using docekr
Build a LAMP environment on your local Docker
Prepare the execution environment of Python3 with Docker
Build the fastest Django development environment with docker-compose
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Simply build a Python 3 execution environment on Windows
Build a python environment with ansible on centos6
How to build an environment for using multiple versions of Python on Mac
ffmpeg-Build a python environment and split the video
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]