简体   繁体   中英

Command not found in container

Inside of a docker container I run a python script. My Dockerfile looks as follows

FROM ubuntu:18.04

WORKDIR /ingestion
COPY /services/bcp /ingestion/services/bcp

RUN mkdir -p /tmp/ingestion && \
    chmod +x /ingestion/bcp-build.sh && \
    /ingestion/bcp-build.sh

ENTRYPOINT ["python3", "-m", "services.bcp"]

Inside of bcp-build.sh I install bcp tool for linux:

apt-get update
apt-get upgrade -y
apt-get install -y curl

curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | tee /etc/apt/sources.list.d/msprod.list 

apt-get update
ACCEPT_EULA=Y apt-get install -y mssql-tools unixodbc-dev 
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bash_profile
source ~/.bashrc

To run the container I use docker-compose.yml

version: "3"
services:
    bcp: 
        build: .

The python (a sample piece of code over here) looks like this:

import argparse

arg_parser: argparse = argparse. \
   ArgumentParser(
       description="Extract tables to CSV",
       usage="bcp.py [-h] [--database [str]]"
   )
required_args: argparse = arg_parser.add_argument_group("required arguments")
required_args.add_argument("--database", metavar="str", type=str, help="database from where to extract", required=True)

def foo():
   args: argparse = arg_parser.parse_args()
   database = args.database
   print(database)

   # build bcp cmd 
   cmd: str = f"bcp {database}.table out /tmp/ingestion/foo.csv"
   subprocess.run(cmd, shell=True) # execute it via subprocess

foo()

To create the image and use it, I run the following commands, docker-compose up -d --build -t foo . It works. But, when I try to execute python code it does not recognise the bcp command. It says bcp command not found . Could you please instruct, how to mitigate this issue?

The exact error message is "bcp command not found" .

Interestingly enough, when I run docker run -it --rm --entrypoint bash foo , and execute bcp then the command is available.

When you install software in your image, you try to set an alternate path in .bashrc , but most common paths of running a Docker image never read those dotfiles. The JSON-array form of ENTRYPOINT doesn't involve a shell at all, so nothing will ever read what you write out to the .bashrc or .bash_profile .

The source directives also have no effect: you're running it from a script so the environment variables that get set "expire" at the end of the bcp-build.sh script, and in any case a Dockerfile RUN directive doesn't notice or preserve changes in the environment.

If you need to modify environment variables in your image, you need to use the Dockerfile ENV directive. (The only other thing that could change the container's environment is a script that runs at container startup time.)

ENV PATH $PATH:/opt/mssql-tools/bin

In general Debian packages are required to install their software in the normal "system" directories, so you might file a bug that this support tool isn't being installed into /usr/bin like a normal binary would.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM