I have a question about docker. If I do
docker build -t my_image .
(after a few updates)
docker build -t my_image .
Then, the size of my_image is not changed, when I check with 'docker images'.
However, if I do
docker build -t my_image .
docker save -o my_image.tar my_image
(after a few updates)
docker build -t my_image .
then, the size of my image is doubled. It looks like a new image contains both the old and the new one.
Does anybody know why this happens, and how to resolve this issue?
The save -o command should output a .tar file in your directory.
If you in your Dockerfile has something like ADD . /app in it, this saved image will also get added, and the image size be doubled because of it.
What you could do is make sure that you do not add the saved my_image.tar when building the image, either by adding some more selection as to what is included in the image, or by saving the .tar somewhere else.
Related
very new to Docker here. I am trying to use a maven 2.1.0 zip to create a docker image.
my
dockerfile.docker file is :
# syntax=docker/dockerfile:1
FROM scratch
LABEL maintainer="Myname"
LABEL maintainer="myemail"
RUN wget HTTP://archive.apache.org/dist/maven/binaries/apache-maven-2.1.0-bin.zip
RUN unzip
I am not exactly sure if I am doing this right
docker build -t apache-maven:2.1.0 .
Essentially I just wanted to create this image locally so I could then push it out to my targeted endpoint. Any help realizing what I did wrong would be appreciated. Whenever I run this build command it tells me it failed to read the dockerfile and that there's no such file or directory.
By default, it will try to find the file with the exact name Dockerfile.
If for any reason, you want to have a different file name like your scenario, you should use next:
docker build -f dockerfile.docker -t apache-maven:2.1.0 .
Detail refers to Specify a Dockerfile (-f)
I have following images inside my docker registry:
Lets assume that file-finder image is derived from ls_files. If so, can I tell that file-finder shares 996MB of disk storage with ls_files image and has only 58,72MB of his own storage?
No, you assumption is incorrect.
I think your Dockerfile is probably like this:
FROM ls_files
RUN # Your commands, etc.
Then you run:
docker build -t file-finder:1.0.0 .
Now the image file-finder is a complete and stand-alone image. You can remove ls_files with no issue since the image ls_files is now included and downloaded into the file-finder image.
When you build an image on top of another, the base image then has nothing to do with the new image and you can remove the base.
Example
FROM alpine:latest
RUN apk add nginx
ENTRYPOINT ["nginx", "-g", "daemon off"]
Let us run:
docker build -t my_nginx:1 .
Now let us remove alpine:latest image.
docker image rm alpine:latest
Now let's run my_nginx:1 image and you should see no error.
Let's say I created a docker image using a command like
docker build -t myimage .
In the current directory where I built the image, I have
ls
Dockerfile
myscript.py
Later, I made changes to ONLY the "myscript.py" file. How do I update the image without needing to rebuild?
I have already created an image locally and it contains two layers
$ docker images inspect existingimagename
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e21695bdc8e8432b1a119d610d5f8497e2509a7d040ad778b684bbccd067099f",
"sha256:3ff73e68714cf1e9ba79b30389f4085b6e31b7a497f986c7d758be51595364de"
]
},
Now i am building another image and want to save space. The first layer of the previous image is the main file system. So i decided to use it
FROM sha256:e21695bdc8e8432b1a119d610d5f8497e2509a7d040ad778b684bbccd067099f
ENV LANG=en_US.UTF-8
CMD ["/usr/bin/bash"]
Then i try to build the new image
$ docker build -t newimage -f Dockerfile .
Sending build context to Docker daemon 443.5MB
Step 1/3 : FROM sha256:e21695bdc8e8432b1a119d610d5f8497e2509a7d040ad778b684bbccd067099f
pull access denied for sha256, repository does not exist or may require 'docker login'
it gives error.
So how to deal with this.
An easy way to profit from image layer cache is to create a base image with just the first layer.
Then use FROM <base image> in your other Dockerfiles.
This way, disk space will be spared as multiple images will share the same layer and also builds will be faster.
Dockerfile-base:
FROM scratch
ADD ./system.tar.gz /
docker build -f Dockerfile-base -t base .
Dockerfile-1:
FROM base
COPY ./somefiles /
docker build -f Dockerfile-1 -t image1 .
Dockerfile-2:
FROM base
COPY ./otherfiles /
docker build -f Dockerfile-2 -t image2 .
Recommended reads
Best practices for writing Dockerfiles § Leverage build cache
From my tutorial, it creates clone Dockerfile (Dockerfile2) and build the second Docker.
docker build . -f Dockerfile2 -t
But I don't understand what . does.
According to Docker documentation.:
-t : tag ...
-f : file...
What is this command doing? - Thanks
Obviously the tag is missing. Anyway you're telling Docker:
Hey Docker, the current directory is your build context so copy everything from this location (except the files and directories mentioned in the .dockerignore file) and build the image for me using the instructions from the Dockerfile2 file. Also, please tag it using the provided tag so I can reference it easily.
If you ommit the file (drop the -f argument), then the default Dockerfile file is assumed.
. means the current directory where you are. And the Dockerfile is in it. If you do not in the directory of the Dockerfile, you will get the error.
The full command : docker build path -f Dockfile -t containerName. Also the document docker build [OPTIONS] PATH | URL | -.
From the Docker build documentation:
Usage:
docker build [OPTIONS] PATH | URL | -
build: Build an image from a Dockerfile
.: Specifies that the PATH is ., and so all the files in the local directory get tar d and sent to the Docker daemon. The PATH specifies where to find the files for the “context” of the build on the Docker daemon.
--file , -f: Name of the Dockerfile (Default is ‘PATH/Dockerfile’).
--tag , -t: Name and optionally a tag in the ‘name:tag’ format. You can apply multiple tags to an image.
So, what is happening is: Docker I want to build an image from a Dockerfile called Dockerfile2 tagged with the value (you didn't set the value of the tag) in the current path.