JIYIK CN >

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

Recreate the container from the new image using Docker-Compose

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

As we develop our applications, we often make changes or add more features to make the application more effective for the different users who interact with the application.

When using Docker, we need to ensure that the changes or features we make are updated to the main application by rebuilding the image and running a new container from this image. This article will teach us how to use Docker Compose to recreate a container from a new image.


Create a new project

In WebStorm IDEA, create a new project by selecting File > New > Project . Then select Empty Project and change the project name from untitled to docker-compose-no-cache or any preferred name.

Press the button labeled "Create" to generate the project. Create a file called index.html in the current folder and copy and paste the following code.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"
          rel="stylesheet"
          integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
          crossorigin="anonymous">
</head>
<body>
<div class="card">
    <div class="card-header">
        Featured
    </div>
    <div class="card-body">
        <h5 class="card-title">Special title treatment</h5>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
        crossorigin="anonymous"></script>
</body>
</html>

This file uses Bootstrap to create a web page that displays a card when we run a container from our application's image. We will use this page to verify that our container is running.


Defining an image

Create a file called Dockerfile in your current folder and copy and paste the following instructions into it.

FROM nginx:1.22.0-alpine
COPY . /usr/share/nginx/html
  1. FROM - sets the base image on which we create our custom image with subsequent instructions. In this example, we set the base image to Nginx and use alpine to pull a lightweight version of Nginx.
  2. COPY - Copies the files and folders in the current directory to a file system location in our image. In this case, we have copied all the files in the current directory to /src/share/nginx/html.

Defining containers using Compose files

Create a file called compose.yaml in your current folder and copy and paste the following instructions into it.

services:
  web-app:
    restart: on-failure
    build: ./
    hostname: web-app-service
    ports:
      - '80:80'

This file defines a service called web-app that exposes port 80 in the host to listen on port 80 in the container. This file provides a way to manage containers more easily than on the terminal.


Build the image and run the container

Since this is our first time building an image, our base image will be pulled and used to create our custom image using the instructions defined in our Dockerfile. The containers defined in our compose.yaml file will also be created and added to the network.

Open a new terminal window on your computer using the keyboard shortcut ALT+F12 and execute the commands below to build an image and run a container from it.

~/WebstormProjects/docker-compose-no-cache$ docker compose up -d

This command executes the compose.yaml file to build an image named docker-compose-no-cache_web-app and runs a container named docker-compose-no-cache-web-app-1 as shown below.

 => CACHED [1/2] FROM docker.io/library/nginx:1.22.0-alpine@sha256:addd3bf05ec3c69ef3e8f0021ce1ca98e0eb21117b97ab8b64127e  0.0s
 => [2/2] COPY . /usr/share/nginx/html                                                                                     0.6s
 => exporting to image                                                                                                     0.8s
 => => exporting layers                                                                                                    0.6s
 => => writing image sha256:d72675b7a3e3a52dd27fe46f298dc30757382d837a5fbf36d8e36d646b5902d6                               0.1s
 => => naming to docker.io/library/docker-compose-no-cache_web-app                                                         0.1s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
 ⠿ Network docker-compose-no-cache_default      Created                                                                    0.3s
 ⠿ Container docker-compose-no-cache-web-app-1  Started

To verify that our container is running, open a browser and make a request to localhost:80 (http://localhost/#). The card we defined in the index.html page will be displayed on the browser.

Using the same terminal window, execute the following command to verify that our custom image was created.

~/WebstormProjects/docker-compose-no-cache$ docker image ls

Output:

REPOSITORY                        TAG       IMAGE ID       CREATED         SIZE
docker-compose-no-cache_web-app   latest    d72675b7a3e3   8 minutes ago   23.5MB

Rebuilding the image using the cache

To rebuild an existing image using the cache and running container, we first need to stop and remove the existing container using the command docker compose down.

Rebuilding the docker image using cache means the current container will be reused to create the new container. Using the same terminal window, execute the following command to rebuild the existing image using cache.

~/WebstormProjects/docker-compose-no-cache$ docker compose build

This command uses the existing image to rebuild a new image from which we can docker compose up -drun a container using .

To verify that existing images are being reused to create new containers, execute the following command and notice that there are no additional images besides the one created in the previous section.

~/WebstormProjects/docker-compose-no-cache$ docker image ls

Output:

REPOSITORY                        TAG       IMAGE ID       CREATED         SIZE
docker-compose-no-cache_web-app   latest    d72675b7a3e3   8 minutes ago   23.5MB

Rebuilding the image without cache

Rebuilding an image without cache means creating a new image without reusing the previous one. We can do this by adding the --no-cachedocker build option when building the image using the command .

Using the same terminal window, execute the following command to rebuild the image without cache. Make sure docker compose downto remove the running container using the command.

~/WebstormProjects/docker-compose-no-cache$ docker compose build --no-cache

This command rebuilds a new image without caching, which means that the previous image is not reused. To verify that the previous image is not reused to create the current image, execute the following command and notice that the previous image has no name, as <none>shown.

The new image uses the name of the previous image.

~/WebstormProjects/docker-compose-no-cache$ docker image ls

Output:

docker-compose-no-cache_web-app   latest    f111dde0e704   3 seconds ago    23.5MB
<none>                            <none>    1c05a46fe049   13 minutes ago   23.5MB

Since the previous image and the new image cannot have the same name, the current image uses the name of the previous image, and the previous image gets none, indicating that it has no name.


Summarize

We have learned how to recreate a container from a new image using Docker Compose. To achieve this, we learned how to rebuild an image with a cache and how to rebuild an image without a cache.

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:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial