Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Best practice on docker golang main process communicate with python subprocess

I know that a best practice of using docker is to have only one main process in each container and only one CMD line running.

My case is I have a Golang micro service and the functionality are implemented in python subprocess. Currently the main process just take API calls then invoke the python subprocess in exec and read the STDOUT and STDERR.

I want to optimize the architecture like to run python as a service (Flask) only on localhost inside docker. Then my main Golang process can use restful http call to communicate with the python process.

But that will let 2 service running in a same docker and it's not a main process and a subprocess. Will that be practically bad, Any idea?

Appreciate on all helps.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Usually when you have multi service, the best practice is not to deploy them in one container, you are suggested to deploy them in multi containers.

You may use docker-compose to help you:

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

For your scenario, give you a minimal example as next.

Folder structure:

.
├── docker-compose.yaml
├── flask
│   ├── app.py
│   └── Dockerfile
└── go
    ├── app.go
    └── Dockerfile

docker-compose.yaml:

version: '3'
services:
  flask_service:
    build: flask

  go_service:
    build: go
    ports:
    - "9500:9500"
    depends_on:
    - flask_service

go/app.go:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
        url := "http://flask_service:9600"
        ret, err := http.Get(url)
        if err != nil {
                panic(err)
        }
        defer ret.Body.Close()

        body, err := ioutil.ReadAll(ret.Body)
        if err != nil {
                panic(err)
        }
        fmt.Fprintf(w, string(body))
}

func main() {
        http.HandleFunc("/", handler)
        http.ListenAndServe(":9500", nil)
}

go/Dockerfile:

FROM golang:latest as build

WORKDIR /go/app
COPY ./app.go .
RUN go mod init app; go mod tidy; go build

CMD ["/go/app/app"]

flask/app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hey, we have Flask in a Docker container!'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=9600)

flask/Dockerfile:

FROM python:alpine3.7

WORKDIR /app

RUN pip install flask

COPY . /app

ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

Execution:

$ docker-compose up
Creating network "20211203_default" with the default driver
Creating 20211203_flask_service_1 ... done
Creating 20211203_go_service_1    ... done
Attaching to 20211203_flask_service_1, 20211203_go_service_1

Verify:

$ curl http://10.192.244.21:9500
Hey, we have Flask in a Docker container!

You could see we visit 9500 port which will route the request to golang container, and the golang container will then call flask service container with api, and finally get the content Hey, we have Flask in a Docker container! which produced by flask.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!