Add a “–no-cache” Option to “docker-compose build”

Docker compose is one of the essential utilities of the Docker platform, which is used to configure services for applications. The docker-compose tool is utilized to specify and deliver multi-container applications. The services are configured in a Docker container through the “docker-compose.yml” file. These containers can be directly created and started by the “docker-compose up” command. The “docker-compose” command supports the different options to provide different functionalities such as “–force-recreate”, “–pull”, “–no-start”, “–no-cache”, and so on.

This write-up will demonstrate how to add the “–no-cache” option to the “docker-compose build” command.

How to Add the “–no-cache” Option to the “docker-compose build” Command?

However, in some cases, users must rebuild the fresh Docker image through the “docker-compose build” command. For this purpose, the “–no-cache” option is utilized to avoid the previous image cache and build a new image completely. Look at the provided instructions to add the “–no-cache” option to the “docker-compose build” command.

Step 1: Create “docker-compose.yml” File

First, create a simple “docker-compose.yml” file to containerize and configure the Golang application:

version: "alpine"

services:

web:

build: .

ports:

- "8080:8080"

golang:

image: "golang"

Step 2: Create and Start Container

To automatically build and execute the Docker image and Docker container, utilize the “docker-compose up” command:

> docker-compose up

The output indicates that the “golangapp-golang-1” container is created and started:

Open the browser and navigate to the specified port to verify if the service is executed. In our case, we navigate towards the “localhost:8080”:

Next, forcefully stop the container by utilizing the “CTRL+C” key:

Step 3: Modify “docker-compose.yml” File

Make modifications to the “docker-compose.yml” file to create the Docker image. For instance, we have modified the image name from “golang” to “golang:alpine”:

Step 4: Build docker-compose Using “–no-cache” Option

Now, rebuild the image without using the cache of the previous image. For this purpose, use the “docker-compose build –no-cache” command. The “–no-cache” option is used to avoid using a cache of the previous build while creating a new image:

> docker-compose build --no-cache

Step 5: Recreate and Start Container

Lastly, again utilize the “docker-compose up” command to recreate and start the Docker container:

> docker-compose up

It can be observed that we have successfully recreated a container and fresh image using the “–no-cache” option.

Conclusion

The “–no-cache” option is used with the “docker-compose build” command to rebuild the fresh image by avoiding the cache of the previous image. To use the “–no-option” option, utilize the “docker-compose build –no-cache” command. This write-up has demonstrated how to add a “–no-cache” option to the “docker-compose build” command.



from https://ift.tt/rUj8H6k

Post a Comment

0 Comments