With docker-compose
By specifying dockerfile
in build
, you can create an image from Dockerfile and create a container.
At this time, there is a CMD that is executed by default in this Dockerfile.
Dockerfile
CMD ["uvicorn", "code.main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"]
With this Dockerfile, try writing another command to command
in docker-compose.yml.
docker-compose.yml
version: "3.0"
services:
fastapi:
image: "image"
container_name: "container"
#Build
build:
dockerfile: ./docker/fastapi/Dockerfile
context: .
restart: always
tty: true
ports:
- "8000:8000"
volumes:
- .:/app
command: /bin/bash -c "sleep 10 && uvicorn code.main:app --reload --reload-dir code --host 0.0.0.0 --port 8000"
At this time, the command of docker-compose.yml is executed with priority. Therefore, it sleeps for 10 seconds before running uvicorn.
Considering that Dockerfile already exists and you can easily change the behavior with yml, I thought it was a natural behavior.