Docker Community Forums

Share and learn in the Docker community.

Create API using docker


(Kosted) #1

Hello all, i’m trying to create an API system using docker and I don’t know if I can do what I want to do.

In the idea, I have my python source code in Eclipse che (Cloud IDE) and I want to run remotely my docker image as if i’m calling a function, and get the result.

For example, I’m writting my source code in python Say_Hello.py and I have a docker image project with a python file Hello_World printing “Hello World”.

I want in my file Say_Hello to call the docker image Hello_World, how could I do this please ? We are trying to create API with docker images we have in order to allow scientists to call these images like a function

Do you have any idea ?


(David Maze) #2

Taking this statement is isolation, this isn’t that difficult. Build your API system using ordinary tools and ignore Docker; since you’re in a Python environment I’d suggest Flask as a base library for a REST API server, py.test as a unit-test library, and requests as a client library. Once you’ve built it and tested it (locally, in a Python virtual environment, without Docker) package it using Docker, run the image somewhere using docker run -p ... to publish the API server’s port on the host machine, distribute the client library, and have users point the client at that host machine’s name and the port number you chose.

This isn’t how Docker is normally used, and “running a command” isn’t typically seen as the same as “calling a function”.

Say there was no Docker. How would you describe this problem? (For instance, what are you packaging, and what are its inputs and outputs?) How would you solve it without Docker?

For a setup like this, two more things are worth mentioning: Docker can be a little bit annoying to install and maintain and I’d hesitate to push it on people without the relevant experience; and since you must be root to run Docker images, you’re giving someone a lot of power over their system, especially if it’s a shared system.

The short real answer to your question is to use the Python subprocess library to run the long-winded docker run command and collect its stdout, or to use a prepackaged Docker library to do the same thing, but again, you must be (effectively) root to run the script to do this.