Coder Social home page Coder Social logo

aminehy / tuto-python-anaconda-docker Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 9.0 10 KB

A project to quickly setup a python project using anaconda packages and Docker

Home Page: https://medium.com/towards-data-science/how-to-package-a-python-application-using-anaconda-and-docker-fc752ce47729

Dockerfile 80.84% Python 19.16%

tuto-python-anaconda-docker's Introduction

How to Package A Python Application using Anaconda and Docker

This is quick tutorial for packaging Python project using Anaconda with Docker

I write this quick tutorial for those who want to create a python app with anaconda packages and package the app using Docker.

A project to quickly setup a python project using anaconda packages and Docker GitHub is home to over 50 million... github.com

Project Structure

.
├── Dockerfile
├── environment.yaml
├── main.py
├── README.md
└── requirements.txt

Create a YAML file for Conda Environment

environment.txt

name: app

channels:
  - conda-forge
  - anaconda

dependencies:
  - flask
  - gunicorn
  - pandas
  - matplotlib
  - scikit-learn
  - pip:
      - -r file:requirements.txt
  • Name your conda environment, for instance app
  • Add a list of channels where the desired packages are hosted
  • List packages you want to install such as flask, gunicorn, numpy… under dependencies
  • You can also install pip packages from the YAML file. One way to it is by adding a pip command to install (pip) packages from a requirements.txt, (-r in the command stands for requirement)

For instance:

requirements.txt

numpy

Create a Python Script

Here we have created a test file main.py for testing the application.

main.py

import numpy as np


print('Hello World !')
print('Numpy version is ', np.__version__)

Dokerization

Create a file named Dockerfile (without extension) from which we build a docker image for our Python application.

Dockerfile

FROM  continuumio/miniconda3
LABEL Author, Amine HadjYoucef

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . $APP_HOME

# Prepare the environnement
RUN conda update --name base conda &&\
    conda env create --file environment.yaml
SHELL ["conda", "run", "--name", "app", "/bin/bash", "-c"]

ENTRYPOINT ["conda", "run", "--name", "app", "python", "main.py"]
  • For simplicity we start from the base image * of miniconda3 (line1)
  • Copy the files from the folder to docker * (line 6)
  • Create a conda environment and install the packages defined in YAML file (line 9)
  • Edit the shell command so we can launch python scripts from within the newly created conda environment (line 11)
  • Define an entry-point for the docker image such that it execute the main.py python script of the application (line 13)

Build a docker image

We build a docker image, named app, with the following commands

docker build --tag myapp .

… then wait for docker to build an image for your app (it might tight a take for downloading and update it the packages list).

Note: The Dockerfile must be located in the root directory of the application

Make sure Docker is installed on your machine (www.docker.com).

Run the container

To run the App from the docker image we created

docker run --rm -ti myapp

This should outputs:

Hello World !
Numpy version is  1.19.4

There you have it ✌️

tuto-python-anaconda-docker's People

Contributors

aminehy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.