Docker uses (1) generate, start, update (container pause, delete, regenerate)


To create an image, you can follow these steps:

Write a Dockerfile

  1. Write a Dockerfile: A Dockerfile is a text file that defines the image building process. In a Dockerfile, you can specify base images, install software, copy files, and other operations.
    Insert image description here
    Insert image description here
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

Build image

Build the image: Use the docker build command to build the image. At the command line, change into the directory where the Dockerfile is located and run the following command:

   docker build -t <镜像名称> .

Among them, <image name> is the name you want to give the image, and . represents the current directory. [Note points cannot be omitted]
Insert image description here

Build failed

Insert image description here

failed commit on ref "layer-sha256:7264a8db6415046d36d16ba98b79778e18accee6ffa71850405994cffa9be7de": "layer-sha256:7264a8db6415046d36d16ba98b79778e18accee6ffa71850405994cffa9be7de" failed
 size validation: 0 != 3401613: failed precondition

It may be that the docker version is wrong or there is a download error
. Mine was just downloaded. It is probably not a version problem, so I cleared the docker cache.

   docker system prune -a

Insert image description here
After that, continue with the docker build command

  docker build -t <镜像名称> .

Build successful

Insert image description here

Run image

Run the image: After the build is successful, you can use the docker run command to run the image. For example:

   docker run <镜像名称> 

or

docker run -dp 3000:3000 <镜像名称> 

This will run your image in the container and perform the actions defined in it.
Insert image description here

Run successfully

Insert image description here

Build again after modifying the code

The code has been modified at this time

Please do not build directly. Delete or pause the original old container.

Use the command to get the ID of the container

docker ps

Insert image description here
docker stop
my container-id is 9c845a155512

docker stop <the-container-id>

stop successfully

Insert image description here
Remove it using command
docker rm 9c845a155512
Insert image description here

successfully deleted

Insert image description here
You can also delete it directly from the dashboard
Insert image description here

Build again and build successfully!

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/m0_54765221/article/details/132668358