This post will demonstrate how to get docker-compose to always recreate containers from the new images.
How to Create a Container Using Docker Compose?
To create a Docker container from fresh images using Docker compose, first, create a “docker-compose.yml” file. Then, execute the “docker-compose up” command.
To simply create a Docker container, go through the provided instructions.
Step 1: Create “docker-compose.yml” File
First, launch the Visual Studio code and open the project directory. After that, create a new file named “docker-compose.yml”. Next, paste the provided code into the “docker-compose.yml” file:
services:
web:
build: .
ports:
- "8080:8080"
golang:
image: "golang:alpine"
The added code will build the “golang:alpine” image that instructs the container to deploy and manage the project:
Step 2: Create “main.go” Project File
Next, create a program file “main.go” and paste the mentioned Golang code into the file. As a result, the message “Hello! Welcome to LinuxHint Tutorial” will be displayed on port “8080”:
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: Create Docker Container
Run and build the Docker container using the “docker-compose up” command:
After that, navigate to the “http:\\localhost:8080” URL on your browser:
The output shows that we have successfully created and run the container through “docker-compose”.
Step 4: List Down All Docker Containers
List down all Docker containers to verify whether the container has been created or not:
The output shows that the container is successfully created:
How to Get “docker-compose” to Always Re-create Containers From Fresh Images?
To use “docker-compose” to re-create a Docker container from a fresh image, the option “–force-recreate” can be used. For this purpose, take a look at the provided procedure.
Step 1: Recreate Docker Container
Recreate the Docker container using the “docker-compose up” command along with the “–force-recreate” option. The specified option will recreate the Docker container forcefully without creating any conflict:
Step 2: List Down Docker Container
Again, list down Docker containers and verify the new containers are created, or docker-compose has recreated the same container:
The output shows that docker-compose recreates the container from a fresh Docker image:
Alternative Method: How to Use “docker-compose” to Recreate Containers From Fresh Images?
The alternative method for Docker-compose to recreate the container is removing the first container. Then, pull the image and recreate the Docker container using the “docker-compose up –build” command.
Check out the listed steps to recreate the container from a fresh image.
Step 1: Remove Docker Container
Remove the Docker container using the “docker-compose rm” command. Here, the “-f” option is used to remove the container forcefully:
Step 2: Pull Docker Image
Next, pull the Docker image using the “docker-compose pull” command:
Step 3: Recreate Docker Container
Recreate the Docker container from a fresh image with the help of the provided command. The “–build” option is used to create or build the container, and “-d” is added to execute the container in the background:
Verify whether the container is running by navigating to the “http:\\localhost:8080” URL. The output shows that we have successfully recreated the Docker container:
Step 4: Stop Docker Container
In order to stop the Docker container, use the “docker-compose stop -t 1” command:
We have demonstrated the methods to get docker-compose to always re-create containers from fresh images.
Conclusion
To use “docker-compose” to always re-create a Docker container from a fresh image, the option “–force-recreate” is used along with the “docker-compose up” command. Another possible way is to remove the container first, then pull the docker image using the “docker-compose pull” command. After that, recreate the container using the “docker-compose up –build” command. This writeup has demonstrated how to get docker-compose to always re-create containers from fresh images.