How can you override a command that will be executed in a container of a specific service in Docker Compose?
Experience Level: Senior
Tags: Docker Compose
Answer
You can do this by modifying the entrypoint: property of your service.
version: "3.9"
services:
my-service:
image: busybox
entrypoint: ["/bin/sh", "-c", "sleep 1d"]
Setting entrypoint both overrides any default entrypoint set on the service’s image with the ENTRYPOINT Dockerfile instruction, and clears out any default command on the image - meaning that if there’s a CMD instruction in the Dockerfile, it is ignored.
Related Docker Compose job interview questions
How can you run a one off command using Docker Compose?
Docker Compose SeniorHow can you define what labels will be used for an image built using Docker Compose?
Docker Compose SeniorHow do you map ports to a specific service container in Docker Compose?
Docker Compose SeniorHow do you comment out lines that you don't want to be processed by Docker Compose?
Docker Compose SeniorHow do tell Docker Compose to use custom .env2 file without passing it in command-line argument?
Docker Compose Senior