0

I've been trying to comprehend the difference between command: and command:- in my docker compose file:

Here is my dockerfile:

ENV PYTHONUNBUFFERED=1
COPY requirements.txt .
COPY dbenv .
RUN python3 -m pip install pymysql
RUN pip install mysql-connector-python
RUN pip install openpyxl
RUN pip install lxml
RUN pip install -r requirements.txt
RUN pip install pandas
RUN pip install requests
RUN pip install beautifulsoup4
COPY . .
WORKDIR /MY_DATABASE

And here is my docker compose file:

version: '3.6'

services:

  db:
    image: mysql:latest
    environment: 
      - MYSQL_ROOT_PASSWORD=postgres
      - MYSQL_DATABASE=webscrape
      - MYSQL_USER=django
      - MYSQL_PASSWORD=djangodb
    ports: 
      - 3306:3306
    restart: always
    # cap_add:
    #   - SYS_NICE

  scraper:
    build:
      context: .
      dockerfile: dockerfile
    environment: 
      - MYSQL_ROOT_PASSWORD=postgres
      - MYSQL_DATABASE=webscrape
      - MYSQL_USER=django
      - MYSQL_PASSWORD=djangodb
      - MYSQL_PORT=3306
      - MYSQL_HOST=db
    # working_dir: /MY_DATABASE

    restart: always
    depends_on: 
      - db
    # command: python Security_table/initialize_securities_table.py
    working_dir: /MY_DATABASE
    command: 
      - python Security_table/initialize_securities_table.py

Now if comment the last 2 lines and I uncomment the 3rd last line like below

command: python Security_table/initialize_securities_table.py

Everything will work just as exepected. But if I try to put it in a list format, it won't work.

command: 
  - python Security_table/initialize_securities_table.py

It will tell "no such files or directory: unknown"

What I am missing here?

Paul Marmagne
  • 153
  • 10
  • 1
    If you want to provide your command as a list, each element of your command line needs to be in is own item: `command: ['python', 'Security_table/initialize_securities_table.py']` https://docs.docker.com/compose/compose-file/compose-file-v3/#command – Zeitounator Sep 12 '21 at 11:21
  • 1
    This is basically a question about YAML syntax. If your `command` is a list, it should be one _token_ per item, not one command per item (similar to the difference between `CMD python file.py` and `CMD ["python", "file.py"]` in your `Dockerfile`. – tripleee Sep 12 '21 at 11:25

1 Answers1

0

The syntax you've tried would be for multiple commands, which is not its goal.

(1 entrypoint, 1 command)

https://docs.docker.com/compose/compose-file/compose-file-v3/#command

You can either write it this way

command: python Security_table/initialize_securities_table.py

or:

command: ["python", "Security_table/initialize_securities_table.py"]

And if you want to run multiple commands (bash script example)

command: ["/bin/bash", "/path/to/script/with/multiple/commands"]

When you run a docker image, for instance:

docker run -it debian:10 /bin/bash

/bin/bash is the command part.

Etienne Dijon
  • 1,093
  • 5
  • 10