This write-up will demonstrate how to define multi-container applications with a “docker-compose.yml” file.
How to Define Multiple Container Applications With “docker-compose.yml” File?
Multi-container applications consist of multiple services, and each service is required to execute within a separate container. Moreover, defining multi-container applications involve three basic steps:
- Step 1: Create Dockerfile
- Step 2: Configure Services in “docker-compose.yml” File
- Step 3: Start the Containers
Step 1: Create Dockerfile
First, create a Dockerfile that is also referred to as the instruction file that contains instructions to generate the image to containerize the application. Paste the following code into “Dockerfile” to execute the Golang application on the web server:
ADD main.go .
RUN go build -o webserver .
In the above code:
- “FROM” command defines the base image for a container.
- “WORKDIR” command defines the working directory for the container.
- “ADD” command adds the specified file to the container path.
- “RUN” command executes the specified command.
- “EXPOSE” is utilized to allocate the exposing port to the container.
- “CMD” specifies the entry point or defaults for the container. Here, “CMD” defines the “./webserver” executable point:
Note: The name of the file must be “Dockerfile”, and do not attach any extension with the file name.
Step 2: Configure Services in “docker-compose.yml” File
In the next step, create a “docker-compose.yml” file to configure the multi-container application services. For instance, we have specified the following code:
According to the above-provided snippet:
- “services” key defines two services: “web” and “golang”.
- The “web” service uses a build of Dockerfile or Dockerfile as the base.
- “container_name” specifies the container’s name in which the “web” service will execute.
- “privileged” is used to allocate the host privileges to the container.
- “port” defines the exposing port for a container.
- The “golang” service simply uses the “golang:alpine” image as a base image:
Step 3: Start the Containers
Now, start the services specified in the “docker-compose.yml” file by utilizing the “docker-compose up” command:
From the below output, you can see that two services are started into different containers. The first container runs the “web” service of the Golang program. The second container executes the “golang” service:
For the verification, navigate to the specified port of the local host:
It can be observed that we have successfully defined the multi-container app with the “docker-compose.yml” file.
To configure the multiple containers application, first create a simple Dockerfile file referred to as an instruction file. Then, configure the application services into a “docker-compose.yml” file. Each individual service will execute in a separate container. After that, fire up the containers with the help of the “docker-compose up” command. This write-up has demonstrated how to configure the multi-container application with the “docker-compose.yml” file.