Docker - Setup a Python development environment

Docker really is a good tools for Dev-ops , learning and R&D .

Want to try and learning Docker but alway drag until now.

After get back my new iMac need to re-setup all my development environment but I don't want too install too many package and module on the iMac system since is difficult to clean and this is not a good practice and this will cause the current MacOS system also.

Node.JS is more simple we can control the project Module inside a folder but Python is more different and many thing need to link with the system.

of course we can use the Python virtual environment but this also will cause the system and storage messy .

I want a Clean and Clear and consistent same system enviroment for my R&D and project so is time's to learn Docker.

Compare with Virtual Machine, Docker is more Light, Fast and easy way.

I try Virtual Machine with Ubuntu 20.04-desktop-amd64 on the latest macOS Catalina their have a-lot a issue when installing and I feel is slow when using the VirtualBox to development code.

Their are two way to using a Docker

1) Mount a local directory as a volume execute a script on local directory without rebuild.

2) Copy the script into the Docker container and rebuild the container image.

First method is more suitable for me since on development alway need to code and run.

Seconds method is more suitable for production.

Method 1 : Mount a local directory as a volume

1. Create a folder call "docker_dev"

2. Create a Dockerfile with below configuration

FROM python:3.8.5-slim  
WORKDIR /usr/src/app  
RUN python -m pip install \  
         parse \
         requests

FROM python:3.8.5-slim

Download the Python 3.8.5 slim version Images from docker hub

WORKDIR /usr/src/app

Set a working directory inside docker container

RUN python -m pip install \ parse \ requests

Install python module we need.

3.Build the image by Dockerfile

docker build -t python_env .

-t is give a tag name call python_env

. don't miss the dot is mean the current location

4.Create a source directory

create a folder calling app

5.Create a Python Scripts

create a python script call dev.py with the code

import requests

x = requests.get('https://www.python.org')  
print(x.status_code)

6 Confirm that parse module has been installed in the container

docker run -it --rm python_env

>>> import parse

>>> parse.__version__

you will see version like '1.16.0' return on the terminal.

7.Running Python Script Using Docker with Mount a local directory

Execute below command

docker run --rm -v /Users/YourWorkSpace/docker_dev/:/usr/src/app python_env python app/dev.py  

--rm option will clean up your container after use.

/Users/YourWorkSpace/docker_dev/ is the local directory

/usr/src/app is the docker container directory

python app/dev.py execute the python script

Run the command on terminal you should see status code 200 return if work.

Method 2 : Build everything inside Docker container image

1. Create a folder call "docker_dev"

2. Create a Dockerfile with below configuration

FROM python:3.8.5-slim  
WORKDIR /usr/src/app  
RUN python -m pip install \  
         parse \
         requests
COPY app/dev.py .  
CMD ["python", "dev.py"]

COPY app/dev.py .

Copy the dev.py to that working directory inside the container

CMD ["python", "dev.py"]

Download the Python 3.7.5 slim version Images from docker hub

3.Build the image by Dockerfile

docker build -t python_env .

*3.Running the Docker Images *

docker run --rm python_env

You should see status code 200 return on terminal when success.

Docker command

docker images Check all Docker images

docker images -a Check all Docker images

-a flag will show all images including intermediate image layers

docker build -t ImageName . Docker Build Images container

docker run --rm ImageName Run DockerImages container

docker run -it --rm ImageName Run Docker Images container with commands

docker rmi ImageName Delete a docker images

docker system prune Purging All Unused or Dangling Images, Containers, Volumes, and Networks

docker system prune -a To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command: (Delete everything)

docker ps -a Show both running and stopped containers

refer:

https://realpython.com/python-versions-docker/

https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes

https://docs.docker.com/engine/reference/commandline/container_ls/

https://hub.docker.com/