Dockerfile 命令不是 运行 组合,而是在构建容器后在 CLI 中运行

Dockerfile commands not running on compose but working in the CLI after the container is built

我目前正在学习 docker 并尝试使用 postgres 和 redis 将我的 django 项目部署到本地容器。

当我 运行 docker 构建容器并让我连接到 django 服务器时,但它缺少 Dockerfile 中指定的一些 pip 要求和库。如果我 运行 来自容器 CLI 中 Dockerfile 的命令,它们都可以正常工作。 docker compose 中使用的 Dockerfile,gcc 库安装正确,但从 CLI 界面检查时,容器中不存在 g++ 和任何其他 unixodbc 库。

这里是 docker 撰写文件,

version: '3.8'

services:
  dashboard-server:
    build:
      context: ./
    command: python manage.py runserver 0.0.0.0:8000
    container_name: dashboard-server
    depends_on:
      - dashboard-redis
      - dashboard-database
    environment:
      - PGDATABASE=django
      - PGUSER=django
      - PGPASSWORD=2bi3f23f2938f2gdq
      - PGHOST=dashboard-database
      - PGPORT=5432
      - REDIS_URL=redis://dashboard-redis:6379/0
    ports:
      - "80:8000"
    volumes:
    - ./:/usr/src/app

  dashboard-redis:
    container_name: dashboard-redis
    image: redis:6-alpine

  dashboard-database:
    container_name: dashboard-database
    image: postgres:13-alpine
    environment:
      - POSTGRES_USER=django
      - POSTGRES_PASSWORD=2bi3f23f2938f2gdq
    ports:
      - "5433:5432"
    expose:
      - 5432
    volumes:
      - dashboard-database:/var/lib/postgresql/data

volumes:
  dashboard-database:

同目录下的Dockerfile,

# pull official base image
FROM python:3.8

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 and pyodbc dependencies
RUN apt-get update
RUN apt-get -y install postgresql
RUN apt-get -y install gcc # gcc installs correctly
RUN apt-get -y install g++ # g++ and the libraries below don't
RUN apt-get -y install unixodbc
RUN apt-get -y install unixodbc-dev
RUN apt-get -y install freetds-dev
RUN apt-get -y install freetds-bin
RUN apt-get -y install tdsodbc
RUN apt-get -y install --reinstall build-essential
RUN apk add build-base
RUN apt-get clean

RUN echo "[FreeTDS]\n\
Description = FreeTDS Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

此处的目标是安装 pyodbc,但在安装这些库之前不起作用。我可以在构建容器后在容器中手动执行此操作,但需要一些帮助来找出 Dockerfile 命令无法正常工作的原因。

检查 shell

中的 gcc
# apt-get -y install gcc
Reading package lists... Done
Building dependency tree
Reading state information... Done
gcc is already the newest version (4:8.3.0-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

正在检查 g++

# ldconfig -p | grep g++
# ldconfig -p | grep gcc
        libgccpp.so.1 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgccpp.so.1
        libgcc_s.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgcc_s.so.1
# apt-get -y install g++
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  g++-8 libstdc++-8-dev
Suggested packages:
  g++-multilib g++-8-multilib gcc-8-doc libstdc++6-8-dbg libstdc++-8-doc
The following NEW packages will be installed:
  g++ g++-8 libstdc++-8-dev
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.3 MB of archives.
After this operation, 44.8 MB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 libstdc++-8-dev amd64 8.3.0-6 [1532 kB]
Get:2 http://deb.debian.org/debian buster/main amd64 g++-8 amd64 8.3.0-6 [9752 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 g++ amd64 4:8.3.0-1 [1644 B]
Fetched 11.3 MB in 1s (15.4 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libstdc++-8-dev:amd64.
(Reading database ... 17270 files and directories currently installed.)
Preparing to unpack .../libstdc++-8-dev_8.3.0-6_amd64.deb ...
Unpacking libstdc++-8-dev:amd64 (8.3.0-6) ...
Selecting previously unselected package g++-8.
Preparing to unpack .../g++-8_8.3.0-6_amd64.deb ...
Unpacking g++-8 (8.3.0-6) ...
Selecting previously unselected package g++.
Preparing to unpack .../g++_4%3a8.3.0-1_amd64.deb ...
Unpacking g++ (4:8.3.0-1) ...
Setting up libstdc++-8-dev:amd64 (8.3.0-6) ...
Setting up g++-8 (8.3.0-6) ...
Setting up g++ (4:8.3.0-1) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode

运行 与 Dockerfile 中的命令相同的命令会正确安装 g++ 和其他库。我在 docker 工作流程中是否遗漏了什么导致这些库无法作为容器构建的一部分安装?

你可以试试这个格式,它很适合我(用你的库更新):

RUN apt-get update && apt-get install --yes libmagic-dev g++ \
    unixodbc unixodbc-dev curl gnupg gnupg1 gnupg2