JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Rebuilding the container in the Docker-Compose file

Author:JIYIK Last Updated:2025/03/25 Views:

Before we implement our application, we need to understand the difference between Dockerfile and compose.yaml files.

When creating a custom image for our application, we typically use a file called a Dockerfile and docker runrun the container from it on the command line using the command. If we want to create multiple images, these commands can become too cumbersome and difficult to manage the containers, so this is where the compose.yaml file comes in.

The compose.yaml file builds the image, runs the containers, creates a network for those containers and adds them to the network. We can then access the network application using the name of the service.

These two commands must exist in the application so that docker compose upthe command can create and run the image.

If any changes are made to the image, these commands will pick up the changes, stop the container, and recreate the image. In this tutorial, we will see how to rebuild a single Docker container from multiple containers defined in a Docker file.


Create a new project

Open IntelliJ IDEA and select File > New > Project. In the window that opens, select Node.js and change the project name from Untitled to docker-rebuild-container or any preferred name.

Make sure you have the Node runtime environment installed so that the Node interpreter and package manager sections are automatically added from your computer. Finally, click on the Create button to generate the project.

Create a file named index.js in the docker-rebuild-container folder and copy and paste the following code into the file.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.json(
        [

            {
                name: "Java in action",
                author: "chad darby",
                price: 345
            },
            {
                name: 'React cookbook',
                author: "mary public",
                price: 600
            },
        ])
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

In this file, we created a sample Node.js application to create an image and run a container from it. This application exposes an API that can be accessed by making a get request to/on the browser.


Create a Dockerfile for the image

Create a file called Dockerfile, and then copy and paste the following instructions into the file.

FROM node:16.17.0-alpine
WORKDIR /com/app
ADD package*.json ./
RUN npm install
ADD . .
CMD node index.js

This file defines the instructions to build an image and run a container from it. The docker documentation provides a detailed description of how these instructions work.


Create a Compose file for the container

Create a compose.yaml file in the docker-rebuild-container folder and copy and paste the following instructions into the file.

services:
  service-one:
    restart: on-failure
    build: .
    hostname: service-one
    ports:
      - '3000:3000'
  service-two:
    restart: on-failure
    build: .
    hostname: service-two
    ports:
      - '5000:3000'

This file defines two services named service-one and service-two, which listen to port 3000 in the container from port 3000 and port 5000 of the host respectively.

  1. services - This defines the computing components of the application.
  2. restart - This defines the action to take when the container terminates.
  3. build - This defines the source of the Dockerfile containing the configuration for building the image.
  4. hostname - This sets a custom name to be used by the service.
  5. ports - This exposes ports on the host to serve applications.

The Docker documentation provides a docker compose file reference that explains these instructions in detail.


Run the container using compose

In this example, we will see how to build and run all the defined containers at once. To execute our ./build/run/containers docker compose.yamlfile, use the keyboard shortcut ALT + F12 to open a new terminal window in your development environment and then use the following commands to build and run the container.

~/WebstormProjects/docker-rebuild-container$ docker compose up --build --force-recreate --no-deps -d

With this command, running and managing containers becomes very easy. Before using this command, we run docker compose up -dto make sure we are rebuilding the image and recreating the container using this command.

 => CACHED [docker-rebuild-container_service-two 2/5] WORKDIR /com/app                                                          0.0s
 => CACHED [docker-rebuild-container_service-two 3/5] ADD package*.json ./                                                      0.0s
 => CACHED [docker-rebuild-container_service-two 4/5] RUN npm install                                                           0.0s
 => [docker-rebuild-container_service-two 5/5] ADD . .                                                                          1.2s
 => [docker-rebuild-container_service-one] exporting to image
  • --build - This ensures that the image is built before running the container.
  • --force-recreate - This will recreate the container regardless of its configuration and the state of the image.
  • --no-deps - This ensures that the linked service will not be started.

Rebuilding a single container using compose

To rebuild a single container from multiple containers defined in the compose.yaml file, use the command used in the previous example and add the name of the service to rebuild, as shown below.

~/WebstormProjects/docker-rebuild-container$ docker compose up --build --force-recreate --no-deps -d service-one

This command rebuilds the image and recreates the container named service-one because we only want to recreate one container. We can verify from the output of this command that only one container was recreated as shown below.

[+] Running 1/1
 ⠿ Container docker-rebuild-container-service-one-1  Started

To verify that the container is working as expected, make two requests on your browser, one on localhost:3000 (http://localhost:3000/) and another on localhost:5000 (http://localhost:5000/) and Notice that we can access the same application from both containers. Following is the JSON response returned by the container.

[
    {
    "name": "Java in action",
    "author": "chad darby",
    "price": 345
    },
    {
    "name": "React cookbook",
    "author": "mary public",
    "price": 600
    }
]

Summarize

In this article, we learned how to define an image using a Dockerfile, define a container using a compose.yaml file, run a container in a compose file, and recreate a single container in a compose file.

请注意,The syntax for running a container from a compose file is docker compose up [OPTIONS] [SERVICE...],The optional SERVICE parameter allows us to specify one or more services whose images we want to rebuild or containers we want to recreate.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:https://www.jiyik.com/en/xwzj/opersys_10075.html

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:122 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:141 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial