登录
首页 >  Golang >  Go问答

适用于 golang 和 python 的Dockerfile

来源:stackoverflow

时间:2024-02-17 21:06:29 482浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《适用于 golang 和 python 的Dockerfile》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

有一个 django 项目已经在运行。现在在代码中, 我需要在子进程模块中执行以下命令:

cmd = f's5cmd cp {obj1} {obj2}'

现在,这段代码在本地运行良好。但部署代码后无法找到 s5cmd。根据 s5cmd 文档,它是用 golang 编写的,并且在我的系统上安装了它,这就是它工作正常的原因。所以我更新了 dockerfile 但它仍然不起作用。

from python:3.6
copy ./requirements.txt /tmp/requirements.txt
run pip install --no-cache-dir -r /tmp/requirements.txt
copy . /apps/project/
workdir /apps/project/project/
expose 8000
cmd gunicorn project.wsgi:application --timeout 3000 --access-logfile '-' -w 3 -k gevent --bind=0.0.0.0:8000

这个 dockerfile 正在运行。 现在更新后的 dockerfile 看起来像这样,但它不起作用,即 s5cmd 命令不适用于 docker。

from python:3.6.7-alpine3.6

# author of file
label maintainer="baktawar"



# install native libraries, required for numpy
run apk --no-cache add musl-dev linux-headers g++
run apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go
run apk update && apk add ca-certificates wget && update-ca-certificates
run wget -o go.tgz https://golang.org/dl/go1.15.2.src.tar.gz
run tar -c /usr/local -xzf go.tgz
run cd /usr/local/go/src/
run ./make.bash
run export path="/usr/local/go/bin:$path"
run export gopath=/opt/go/
run export path=$path:$gopath/bin
run apk del .build-deps
run go version
run apk update && apk add git && go get github.com/peak/s5cmd && s5cmd
# upgrade pip
run pip install --upgrade pip

copy ./requirements.txt /tmp/requirements.txt
run pip install --no-cache-dir -r /tmp/requirements.txt && pip install psycopg2-binary
copy . /apps/project/
workdir /apps/project/project/
expose 8000
cmd gunicorn project.wsgi:application --timeout 3000 --access-logfile '-' -w 3 -k gevent --bind=0.0.0.0:8000

requirements.txt 文件

boto3==1.12.39
botocore==1.15.39
certifi==2020.6.20
chardet==3.0.4
Django==2.2
djangorestframework==3.11.0
docutils==0.15.2
gevent==20.5.1
greenlet==0.4.16
gunicorn==20.0.4
idna==2.9
jmespath==0.10.0
json-logging==1.2.0
numpy==1.19.0
pandas==1.0.5
python-dateutil==2.8.1
pytz==2020.1
requests==2.23.0
s3transfer==0.3.3
six==1.15.0
SQLAlchemy==1.3.16
sqlparse==0.3.1
urllib3==1.25.9
zope.event==4.4
zope.interface==5.1.0
pytest==6.0.1

解决方案


dockerfile 有几个问题:

  1. run cd /usr/local/go/src/ 不起作用,应将其替换为 workdir /usr/local/go/src 或与下一个命令内联放置
  2. run 导出 path ... (等)也不起作用,应替换为 env path ...

以下 dockerfile 应该可以工作(仅 go/s5cmd 部分)

RUN apk --no-cache add musl-dev linux-headers g++
RUN apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go
RUN apk update && apk add ca-certificates wget && update-ca-certificates
RUN wget -O go.tgz https://golang.org/dl/go1.15.2.src.tar.gz
RUN tar -C /usr/local -xzf go.tgz
RUN cd /usr/local/go/src/ && ./make.bash
ENV PATH "/usr/local/go/bin:$PATH"
ENV GOPATH "/opt/go/"
ENV PATH "$PATH:$GOPATH/bin"
RUN apk del .build-deps
RUN go version
RUN apk update && apk add git && go get github.com/peak/s5cmd && s5cmd

本篇关于《适用于 golang 和 python 的Dockerfile》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>