安装 Python 3 时在 Docker 容器中出现错误,同时安装了 Mongo DB。

huangapple go评论71阅读模式
英文:

Error Installing Python 3 in Docker Container along with Mongo DB

问题

我正在尝试创建一个容器,在其中安装Mongo DB服务器以及带有pymongo模块的python 3.8,但不起作用。在dumps文件夹中,我尝试创建一个包含主机机器卷的tar.gz文件的备份,使用代码mongodump --archive=backup.tar.gz --gzip。我应该如何更改代码,以便能够创建容器而不出现故障?我还应该如何测试备份是否正常工作?

运行docker-compose up --build时出错:

#0 3.837 读取状态信息...
#0 3.895 E: 无法找到软件包python3.8
#0 3.895 E: 无法通过全局搜索找到任何软件包'python3.8'
#0 3.895 E: 无法找到软件包python3.8-dev
#0 3.895 E: 无法通过全局搜索找到任何软件包'python3.8-dev'
#0 3.895 E: 无法通过正则表达式找到任何软件包'python3.8-dev'

Dockerfile:

FROM mongo:4.0.4

# 安装 Python 3.8
RUN apt-get update && \
    apt-get install -y python3.8 python3.8-dev python3-pip && \
    ln -s /usr/bin/python3.8 /usr/local/bin/python3

# 安装 pymongo
RUN pip3 install pymongo

EXPOSE 27017

# 创建 src 和 dump 目录
RUN mkdir /app
VOLUME ["/app/src", "/app/dump", "/data/db", "/var/www/html"]

CMD ["/bin/bash"]

docker-compose.yml 文件:

version: '3.2'

services:
  py-mongo:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./src:/app/src
      - ./dump:/app/dump
      - ./mongo-data:/data/db
      - ./mongo-app:/var/www/html
    command: tail -f /dev/null
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=1234
    ports:
      - "27017:27017"

目录结构:

.
├── Dockerfile
├── README.md
├── docker-compose.yaml
├── dump
├── mongo-app
├── mongo-data
└── src
英文:

I am trying to make a container where I install Mongo DB server as well as python 3.8 with pymongo module but it does not work. In the dumps folder I am trying to create a backup of tar.gz file of which will have volumes with the host machine using the code mongodump --archive=backup.tar.gz --gzip . How would I be able to change the code so that the container could be created and wont be faulty. How would I also be able to test if the backup is properly working?

Error when run docker-compose up --build:

#0 3.837 Reading state information...
#0 3.895 E: Unable to locate package python3.8
#0 3.895 E: Couldn't find any package by glob 'python3.8'
#0 3.895 E: Unable to locate package python3.8-dev
#0 3.895 E: Couldn't find any package by glob 'python3.8-dev'
#0 3.895 E: Couldn't find any package by regex 'python3.8-dev'

Dockerfile

FROM mongo:4.0.4

# Install Python 3.8
RUN apt-get update && \
    apt-get install -y python3.8 python3.8-dev python3-pip && \
    ln -s /usr/bin/python3.8 /usr/local/bin/python3

# Install pymongo
RUN pip3 install pymongo

EXPOSE 27017

# Create src and dump directories
RUN mkdir /app
VOLUME ["/app/src", "/app/dump", "/data/db", "/var/www/html"]

CMD ["/bin/bash"]

docker-compose.yml file:

version: '3.2'

services:
  py-mongo:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./src:/app/src
      - ./dump:/app/dump
      - ./mongo-data:/data/db
      - ./mongo-app:/var/www/html
    command: tail -f /dev/null
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=1234
    ports:
      - "27017:27017"

Tree Structure

.
├── Dockerfile
├── README.md
├── docker-compose.yaml
├── dump
├── mongo-app
├── mongo-data
└── src

答案1

得分: 0

以下是翻译好的部分:

"Docker镜像旨在尽可能小。这就是为什么它们通常不包括我们工作站上已经有的软件。"

"在Mongo镜像中,没有包含python3.8的apt-get包。"

"您有两个选择:"

  • "为apt-get添加存储库"
  • "从tgz文件安装Python"

"请选择对您的目的更好的选项。"

"添加存储库:"

$ apt-get install software-properties-common
$ add-apt-repository ppa:deadsnakes/ppa

"Python安装:"

$ apt-get update
$ apt-get install python3.8

"然后将会正常工作。"

英文:

The Docker images aim to be as small as possible. That's why they often do not include software we have in our workstations.

In the Mongo image there's no repository with python3.8 package for apt-get.

You have 2 options:

  • Add a repository for apt-get
  • Install Python from tgz file

Choose which is better for your purpose.

Adding the repository:

$ apt-get install software-properties-common
$ add-apt-repository ppa:deadsnakes/ppa

The Python installation:

$ apt-get update
$ apt-get install python3.8

will work then.

答案2

得分: 0

以下是翻译好的部分:

一个 Docker 容器是一个单一进程的封装。此备份任务作为一个独立进程运行,所以可能需要在一个独立的容器中运行。相反,这意味着你可以只使用标准的 Docker Hub python 镜像来运行你的脚本,以及未经修改的 mongo 镜像来运行数据库。

在 Compose 文件中,你会为数据库和脚本有单独的容器。你需要将一些连接信息传递给这两个容器。我已将这些设置指定为环境变量;你的代码需要从 os.environ 中获取这些设置。

现在只需运行 docker-compose up -d 就可以启动整个系统。请注意,我们从未指定一个交互式 shell 作为主要容器命令,也没有使用 tail 来“保持容器活动”,反之,我们不需要 docker exec 或手动启动正在运行的容器内的进程。

你的问题中提到了一个 HTML 目录。无论是什么服务正在提供这个目录,都可以放入第三个容器中,可能是从 python 构建,并在其 requirements.txt 文件中包含像 Django 或 Flask 这样的服务器框架。

英文:

A Docker container is a wrapper around a single process. This backup task is running as a separate process and so it probably needs to run in a separate container. Conversely, this means you can just use the standard Docker Hub python image for your script, and the unmodified mongo image for the database.

FROM python:3.8
WORKDIR /app

# COPY requirements.txt ./
# RUN pip install -r requirements.txt
RUN pip install pymongo

COPY ./ ./
CMD ["./myscript.py"]

In the Compose file, you'd have separate containers for the database and script. You will need to pass along some connection information to both containers. I've specified these settings as environment variables; your code will need to get these settings back from os.environ.

version: '3.8'
services:
  db:
    image: mongo:4.0
    volumes:
      - ./mongo-data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=1234
    ports:
      - "27017:27017"
  script:
    build: .
    volumes:
      - ./dump:/app/dump
    environment:
      - MONGO_HOST=db
      - MONGO_USERNAME=root
      - MONGO_PASSWORD=1234

Now just running docker-compose up -d will start the whole thing. Notice that we've never specified an interactive shell as the main container command or used tail to "keep a container alive", and conversely, we don't need docker exec or to manually start processes inside running containers.

Your question has a very brief mention of an HTML directory. Whatever's serving this would go into a third container, again possibly built FROM python and including a server framework like Django or Flask in its requirements.txt file.

huangapple
  • 本文由 发表于 2023年6月15日 12:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定