What's the best docker image to have a python environment so I could run my scripts on there instead of on Windows? I'd prefer not to use a VM.
How to make lightweight docker image for python app with pipenv - Stack Overflow
Docker Python Image - Stack Overflow
How to install python in a docker image? - Stack Overflow
Videos
The problem comes when you need things like ciso8601, or some libraries, requiring build process. Build tools are not "incorporated" into the both slim and alpine variants, for low-size footprint.
So to install deps, you'll have to:
- Install build tools
- Deploy dependencies from Pipfile.lock system-wide
- Uninstall build tools and clean caches
And do that 3 actions inside a single RUN layer, like following:
FROM python:3.7-slim
WORKDIR /app
# both files are explicitly required!
COPY Pipfile Pipfile.lock ./
RUN pip install pipenv && \
apt-get update && \
apt-get install -y --no-install-recommends gcc python3-dev libssl-dev && \
pipenv install --deploy --system && \
apt-get remove -y gcc python3-dev libssl-dev && \
apt-get autoremove -y && \
pip uninstall pipenv -y
COPY app ./
CMD ["python", "app.py"]
- Manipulating build system would cost you around 300MiB and some extra time
- Uninstalling pipenv would save you another 20MiB (which is 10% of resulting size).
- Separating
RUNcommands would not delete data from layers, and would result in ~500MiB image. That's docker specifics.
So that would result in perfectly working ~200MiB sized image, which is
- 5 times less than original
python:3.7, (that is >1.0GiB) - Has no alpine incompabilities (these are typically tied to glibc replacement)
At the time, we're fine with slim (debian buster) build variants, preferring slim over alpine (for most compatibility). If you're really up to further size optimization, I'd recommend you to take a look at some excellent builds of these guys:
- Alpine Python
- 12.7MiB MariaDB
How about,
FROM python:3.7-alpine
WORKDIR /myapp
COPY Pipfile* ./
RUN pip install --no-cache-dir pipenv && \
pipenv install --system --deploy --clear
COPY src .
CMD ["python3", "app.py"]
- It utilises the smaller Alpine version.
- You won't have any unnecessary cache files left over using
--no-cache-diroption forpipand--clearoption forpipenv. - You also deploy outside of venv.
You can also add && pip uninstall pipenv -y after pipenv install --system --deploy --clear in the same RUN command to eliminate space taken by pipenv if that extra image size bothers you.
If your environment is restricted enough that you can't use sudo to install packages, you won't be able to use Docker: if you can run any docker run command at all you can trivially get unrestricted root access on the host.
My python scripts needs a bit more modules which I can't install due to lack of sudo access & moreover, I dont want to screw up with the default Py which is needed for host to function.
That sounds like a perfect use for a virtual environment: it gives you an isolated local package tree that you can install into as an unprivileged user and doesn't interfere with the system Python. For Python 2 you need a separate tool for it, with a couple of steps to install:
export PYTHONUSERBASE=$HOME
pip install --user virtualenv
~/bin/virtualenv vpy
. vpy/bin/activate
pip install ... # installs into vpy/lib/python2.7/site-packages
you can create a docker image on any standalone machine and push the final required image to docker registry ( docker hub ). Then in your laptop you can pull that image and start working :)
Below are some key commands that will be required for the same.
- To create a image, you will need to create a Dockerfile with all the packages installed
- Or you can also do
sudo docker run -it ubuntu:16.04then install python and other packages as required. - then
sudo docker commit container_id name sudo docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]sudo docker push IMAGE_NAME
Then you pull this image in your laptop and start working.
You can refer to this link for more docker commands https://github.com/akasranjan005/docker-k8s/blob/master/docker/basic-commands.md
Hope this helps. Thanks
RUN apt-get update
RUN apt-get install -y python3
As hinted by:
Acquire (13: Permission denied)
I believe this is due to your base image:
https://github.com/SeleniumHQ/docker-selenium/blob/master/NodeChrome/Dockerfile
As you can see it swaps from the default user context of 'root' to 'seluser'.
You can either:
- wear this as a consequence of the base image (i.e. use sudo)
- swap back:
USER root - or consider creating your own docker image to avoid swapping in the first place
Using sudo is best avoided in Dockerfiles where possible, so it would be preferable to go with option #2 or #3, rather than #1.
Hope that helps mate.
Note: Below commands may require root/administrative previleges.
- Download docker image
docker pull ubuntu - Start interactive container
docker run -it ubuntu /bin/bash
Note: By default you will be logged in inside container as root user if not then either elevate your privileges to root or use sudo before below listed commands
- Update container instance
apt-get update - For python 2.7
apt-get install python2 - For Python 3.x
apt-get install python3