Should I define the command as docker-compose.yml or Dockerfile?

Asked 2 years ago, Updated 2 years ago, 73 views

Here is an example of running Django and PostgreSQL on Docker-Compose.

In this example, the command is defined in docker-compose.yml, but if you specify build in the service definition, you can also define the command in CMD in Dockerfile.

command for docker-compose CMD

in Dockerfile

docker-compose.yml

version: '2'
services:
  db:
    image —postgres
  web:
    build:.
    command —python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depend_on:
      - db

Dockerfile

 FROM python: 2.7
ENV PYTHONUNBUFFERED1
RUN mkdir/code
WORKDIR/code
ADD requirements.txt/code/
RUN pip install-r requirements.txt
ADD./code/
# CMD<- You can also define commands here

In such a case, should I define it as command in docker-compose.yml or CMD in dockerfile?
Please let me know if there are any advantages or disadvantages.

docker docker-compose

2022-09-30 19:33

1 Answers

If you write the command in docker-compose.yml, it will run when you run docker-compose up, and the CMD specified in Dockerfile will be ignored.

Therefore, if you want to run a specific command in docker run, you can write it in the CMD of Dockerfile, or if you want to run a command in docker-compose up, you can write it in docker-compose.yml.

(However, the docker run argument can override the actual command.)


2022-09-30 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.