Recreate the container from the new image using Docker-Compose
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
- 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.
- 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 -d
run 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 down
to 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.
Related Articles
Exposing multiple ports in a Docker container
Publish Date:2025/03/24 Views:186 Category:Docker
-
There are different types of communication on the Internet, the most common ones include file transfers, sending emails, and serving web pages. To make this communication possible, we make use of port numbers that help identify the type of
Using Docker network host commands
Publish Date:2025/03/24 Views:51 Category:Docker
-
Docker containers work by leveraging network drivers that are created during the installation of Docker. The default drivers available to us include bridge and host networking. When we create containers without specifying a network, they ar
Clear Docker container logs
Publish Date:2025/03/24 Views:192 Category:Docker
-
Logs are information recorded by the application when a specific event or state occurs. They help us monitor the application and take necessary actions. For example, when we deploy an application to a production environment, logs can help u
Docker daemon log location
Publish Date:2025/03/24 Views:65 Category:Docker
-
The Docker daemon provides essential information about the general state of your microservices architecture. Unfortunately, container-centric logging techniques allow you to collect relevant data from your services but provide little inform
Difference between COPY and ADD commands in Dockerfile
Publish Date:2025/03/24 Views:140 Category:Docker
-
A Dockerfile is a text document that contains all the commands used to build a Docker image. Recently, we have seen Docker being widely used as the default tool for managing configurations and automating deployments. Advanced features such
The --rm flag in Docker
Publish Date:2025/03/24 Views:151 Category:Docker
-
Typically, when most developers start using docker, after going through all the processes of pulling images, building them, and running containers, removing a container defeats the purpose of doing so. However, for experienced developers, t
Setting environment variables in Docker
Publish Date:2025/03/24 Views:188 Category:Docker
-
Environment variables are used to add additional configuration or metadata to aid in the development of an application, and can exist in different forms. For example, when developing a Java application, we usually set an environment variabl
在 Linux 中托管 Docker Internal
Publish Date:2023/04/18 Views:221 Category:Docker
-
Docker 允许开发人员通过将应用程序包装在称为容器的标准化单元中来高效地构建、测试和部署应用程序。 在使用 Docker 容器时,您可能会遇到需要将容器与主机连接的场景。
在 Docker 中设置工作目录
Publish Date:2023/04/18 Views:234 Category:Docker
-
在 Docker 中,我们可以通过编辑 Dockerfile 并添加密钥 WORKDIR 来设置我们的工作目录。本文将讨论在 Docker 中更改我们当前和默认的工作目录。