Stop using virtualenv, pyenv, nvm, goenv and Use Docker images
Tags: docker, webdev, devops, productivity
Published: 2021-02-24 12:35:45

## Notes

* Requires Docker and at least 4 GB RAM (see comment section bellow for further details)

* Not applicable for single app

## Why I use Docker images 

![Image](https://miro.medium.com/max/1400/1*m8MURCq1z0CnKoRrWW6PjQ.png)

Using version managers like pyenv and nvm are ~~obsolete~~. 

For example, virtualenv/pyenv only encapsulates Python dependencies.

Docker containers encapsulate an entire OS with several dependencies.

Docker already allows to pull several one-click images like python, nodejs, golang, etc  ...

```bash
docker pull python:3.9.0
```

```bash
# Dockerfile
FROM python:3.9.0
```

## Docker images and containers

![image](https://miro.medium.com/max/1400/1*p8k1b2DZTQEW_yf0hYniXw.png)

A Docker image includes the elements needed to run an application as a container -- such as code, config files, environment variables, libraries and run time.

A Docker container can be seen as a replica or as a printout of that image.

For example, you can have one python image and multiple containers related to this image.

![image](https://www.docker.com/sites/default/files/d8/styles/large/public/2018-11/container-what-is-container.png?itok=vle7kjDj)

## Isolation with the OS (Clean Install)

You may not have python installed on your OS and you can pull the python 3.9 docker image.

So you can only use python inside a docker container.

```bash
docker pull python:3.9.0
```

You may have the python 2.7 installed on your OS and pull the python 3.9 docker image.

They will not conflict together, but you can only use python 3.9 inside a docker container.

If you don't want the python 3.9 anymore , you just have to delete the docker container related to the python 3.9 image and the python 3.9 image itself.

## Multiple Versions

You can pull the python 2.7 docker image and the python 3.9 docker image.

They will not conflict together.

## Multiple Layers

You can pull the python 2.9 docker image and install additional dependencies.

```bash
# Dockerfile
FROM python:3.9.0

...

COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
```

## Upgrade - Switch versions

You can pull the python 3.8 docker image and then switch to the 3.9 image.

```bash
# Dockerfile
FROM python:3.8.0 -> FROM python:3.9.0
```

## Example : python

```bash
# Dockerfile
FROM python:3.9.0
```

```bash
# Dockerfile
FROM python:3.9.0-alpine
```

## Example : node

```bash
# Dockerfile
FROM node:14.16.0
```

```bash
# Dockerfile
FROM node:14.16.0-alpine
```

## Hub

* [https://hub.docker.com/](https://hub.docker.com/)

* [https://artifacthub.io/](https://artifacthub.io/)

## Links

* [https://www.docker.com/resources/what-container](https://www.docker.com/resources/what-container)

* [https://searchitoperations.techtarget.com/definition/Docker-image](https://searchitoperations.techtarget.com/definition/Docker-image)

* [https://towardsdatascience.com/building-a-data-science-development-environment-with-docker-compose-1407aa5147b9](https://towardsdatascience.com/building-a-data-science-development-environment-with-docker-compose-1407aa5147b9)

* [https://stackoverflow.com/questions/50974960/whats-the-difference-between-docker-and-python-virtualenv](https://stackoverflow.com/questions/50974960/whats-the-difference-between-docker-and-python-virtualenv)