This blog will demonstrate how to override the “ENTRYPOINT” in Docker compose.
How to Override the Entrypoints in Docker Compose?
Both “CMD” and “ENTRYPOINT” commands specify the container’s executables. These commands can be overridden in Docker compose with the help of the “command” key. For the demonstration, take a look at the provided instructions.
Step 1: Create Dockerfile
Create a Dockerfile and copy the following commands into the file. Here:
- “FROM” is used to define the base image.
- “WORKDIR” specifies the working directory for the container.
- “COPY” copies the source file into the container working directory.
- “RUN” is utilized to run the specified command. This command will execute the “webserver”.
- “EXPOSE” specifies the exposing port for the container over a network.
- “ENTRYPOINT” is used for defining the executables for containers:
COPY main.go .
RUN go build -o webserver .
Step 2: Generate Docker Image
Next, generate the Docker image from the above specified Docker file through the mentioned command. Here, the “-t” option tags the Docker image:
Step 3: Override Entrypoint in Docker Compose
In order to override the ENTRYPOINT in Docker compose file, simply use the “command” key and step the entrypoint for the container as shown below:
In the above snippet:
- We have configured the “web” and “golang” two services.
- “container_name” sets the name of the container for the “web” service:
- The “image” is utilized to define the base image for the container. For this purpose, we have utilized the image created by Dockerfile in the previous section.
- “command” overrides the “ENTRYPOINT” in Docker compose. For instance, we have used the same entrypoint as in Dockerfile.
- “ports” defines the container’s exposed port on the host network:
Step 4: Run Docker Compose
Next, run the “docker-compose up” command to create and start the compose container:
In the above snipped, the “-d” option is utilized to deploy the container in detached mode:
For the verification, open the “localhost:8080” port on browser and check if the application is deployed or not:
Here, you can see we have successfully overridden the entrypoint in the compose file using the “command” key.
To override the entrypoint in the Docker compose command, first, create a “docker-compose.yml” file, configure the services into a file and utilize the “command” key to override the entrypoint in Docker compose. This blog has demonstrated how to override the entrypoint in Docker compose.