docker use (1)
To create an image, you can follow these steps:
Write a Dockerfile
- 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.
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]
Build failed
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
After that, continue with the docker build command
docker build -t <镜像名称> .
Build successful
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.
Run successfully
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
docker stop
my container-id is 9c845a155512
docker stop <the-container-id>
stop successfully
Remove it using command
docker rm 9c845a155512
successfully deleted
You can also delete it directly from the dashboard
Build again and build successfully!