entrypoint defined in docker-compose.yml wipes out CMD defined in Dockerfile
Problem
Hi, I am following the example on https://docs.docker.com/compose/startup-order/ to make sure the database is running before I start the application. My `Dockerfile` contains the command [code block] and in my `docker-compose.yml` I have [code block] but after the postgres database starts the service container just exits straight away. [code block] Looking inside the running container I can see the entrypoint does not have the command appended [code block] If both command and entrypoint are in the same place (either in the `Dockerfile` or in the `docker-compose.yml`) the application starts up properly. [code block] Any idea is this is a bug or I am doing something wrong? Thanks
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix CMD Override Issue in Docker Compose
When both `entrypoint` and `command` are defined in `docker-compose.yml`, the `command` specified in the `Dockerfile` is overridden. This is why the application exits immediately after the database starts, as the container does not have a valid command to execute.
Awaiting Verification
Be the first to verify this fix
- 1
Remove CMD from Dockerfile
To ensure that the command defined in the Dockerfile is not overridden, you should remove the CMD instruction from the Dockerfile. Instead, define the command directly in the docker-compose.yml.
dockerfileFROM your_base_image # Other Dockerfile instructions # Remove CMD instruction - 2
Define Command in docker-compose.yml
Add the command you want to run in the `docker-compose.yml` file under the relevant service. This will ensure that the command is executed when the container starts.
yamlservices: your_service: image: your_image command: ["your_command", "arg1", "arg2"] - 3
Verify Entrypoint in docker-compose.yml
If you need to specify an entrypoint, ensure that it is defined correctly in the `docker-compose.yml`. This entrypoint should be compatible with the command you are running.
yamlservices: your_service: entrypoint: ["your_entrypoint"] - 4
Rebuild and Restart Containers
After making changes to the Dockerfile and docker-compose.yml, rebuild your images and restart your containers to apply the changes.
bashdocker-compose up --build
Validation
After applying the changes, run `docker-compose up` and ensure that the application container starts successfully and remains running. Check the logs for any errors and confirm that the expected command is executed.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep