This post has demonstrated how to rebuild a single Docker container.
How to Rebuild a Single Docker Container?
To rebuild a single Docker container, the docker-compose “–force-recreate” and “build” option is used. For this purpose:
- First, launch the Visual Studio code editor from the Windows Start menu, and build a simple container.
- Then, rebuild it using the “docker-compose up –force-recreate” command.
Follow the below-listed steps to build and rebuild the container from scratch.
Step 1: Create “docker-compose.yml” File
Create a simple “docker-compose.yml” file and paste the below-mentioned instructions to create an image that will build a new container:
services:
web:
build: .
ports:
- "8080:8080"
golang:
image: "golang:alpine"
Step 2: Create Program File
Next, add the given code into the “main.go” file to run a simple Golang program:
import (
"fmt"
"log"
"net/http"
)
func handler (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello! Welcome to LinuxHint Tutorial")
}
func main () {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}
Step 3: Build and Run Container
Build and execute the Docker container through the “docker-compose up” command. Here, the “-d” option is used to execute the container in the background:
Step 4: Rebuild or Recreate Docker Container
To rebuild or recreate the Docker container, utilize provided command along with “–force-recreate” and “–build” options:
After that, visit the “http://localhost:8080” URL and check if the container is running or not:
It can be observed that we have successfully deployed a simple Golang program in a Docker container:
Alternatively, the below-provided command is also used to rebuild a single Docker container:
Alternative Method: Rebuild a Single Docker Container
The alternative method to rebuild the docker container is to first stop and remove the container through “docker-compose”. Then, rebuild it using the “docker-compose create” command.
Look at the listed steps to rebuild a single Docker container.
Step 1: Stop Container
First, stop the container with the help of the “docker-compose stop” command:
Step 2: Remove Container
Next, remove the containers with the help “rm” command as follows:
Step 3: Rebuild Container
Make some modifications in configurations or program files. Then, rebuild the containers with the help of the “docker-compose create” command:
Step 4: Start Container
Next, start the container using the “docker-compose start” command:
Again, navigate the “localhost:8080” URL to check whether the container is started or not:
It can be observed that we have rebuilt a single Docker container.
Conclusion
To rebuild the Docker container, the “–force-recreate” and “–build” options are used along with the “docker-compose up” command. Another possible way is first to stop and remove the Docker container. After that, rebuild the container using the “docker-compose create” command. This write-up has demonstrated how to rebuild a single Docker container.