Docker

Docker Compose Override Entrypoints

Dockerfile is a building block of the Docker forum that is utilized to build and manage the containers through Docker images. One of the key commands in a Dockerfile is “ENTRYPOINT”, which is used to specify the entrypoint for the container. This command defines the executables of the container and specifies what command or file will be executed first when the container starts at the top layer of the container.

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:
FROM golang:1.8

WORKDIR /go/src/app

COPY main.go .

RUN go build -o webserver .

EXPOSE 8080:8080

ENTRYPOINT ["./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:

> docker build -t go-img .

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:

version: "alpine"

services:

web:

container_name: web-container

image: go-img

command: ["./webserver"]

ports:

- "8080:8080/tcp"

golang:

image: "golang:alpine"

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:

> docker-compose up -d

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.

Conclusion

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.