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


I’m currently learning docker and trying to deploy my django project to a local container with postgres and redis.

When I run docker compose the container builds and lets me connect to the django server but it is missing some of the pip requirements and libraries specified in the Dockerfile. If I run the commands from the Dockerfile in the CLI of the container they all work correctly.
The Dockerfile used in the docker compose, gcc library installs correctly but g++ and any of the other unixodbc libraries don’t exist in the container when checking from the CLI interface.

Here is the docker compose file,

version: '3.8'

      context: ./
    command: python runserver
    container_name: dashboard-server
      - dashboard-redis
      - dashboard-database
      - PGDATABASE=django
      - PGUSER=django
      - PGPASSWORD=2bi3f23f2938f2gdq
      - PGHOST=dashboard-database
      - PGPORT=5432
      - REDIS_URL=redis://dashboard-redis:6379/0
      - "80:8000"
    - ./:/usr/src/app

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

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


The Dockerfile in the same directory,

# pull official base image
FROM python:3.8

# set work directory
WORKDIR /usr/src/app

# set environment variables

# 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/\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/" >> /etc/odbcinst.ini

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

# copy project
COPY . .

The goal here is to get pyodbc installed but doesn’t work until these libraries are installed. I can do this manually in the container after it is built but would like some help finding out why the Dockerfile commands aren’t working.

Checking gcc in the shell

# 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.

Checking g++

# ldconfig -p | grep g++
# ldconfig -p | grep gcc (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/ (libc6,x86-64) => /lib/x86_64-linux-gnu/
# 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 buster/main amd64 libstdc++-8-dev amd64 8.3.0-6 [1532 kB]
Get:2 buster/main amd64 g++-8 amd64 8.3.0-6 [9752 kB]
Get:3 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

Running the same command as the one in the Dockerfile installs g++ and the other libraries correctly. Is there something that I’m missing in the docker workflow which causes these libraries to not install as part of the container build?


You can try this format, it works well for me(update with your libs):

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

Answered By – Gustavo F Martins

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published