Docker

Rebuild a Single Docker Container

Docker containers are the essential component of the Docker work environment. It is used to containerize an application. Docker containers encapsulate applications with all the relevant dependencies, packages, and source code. Therefore, docker makes it easy to deploy applications on servers and other machines.

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:

version: "alpine"
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:

package main

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:

> docker-compose up -d

Step 4: Rebuild or Recreate Docker Container
To rebuild or recreate the Docker container, utilize provided command along with “–force-recreate” and “–build” options:

> docker-compose up -d --force-recreate --build

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:

> docker-compose up -d --no-deps --build <service-name>

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:

> docker-compose stop

Step 2: Remove Container
Next, remove the containers with the help “rm” command as follows:

> docker-compose rm

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:

> docker-compose create

Step 4: Start Container
Next, start the container using the “docker-compose start” command:

> docker-compose start

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.

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.