Docker understanding - docker

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.

Related

docker image ls is not showing the installed base images ("FROM" images)

I have the following Dockerfile and as you can see I'm using python:3.10.4 as a base image
FROM python:3.10.4
WORKDIR /app
COPY . .
CMD ["python", "bmi.py"]
After I build the image using docker build the image built successfully and I can see it when I list the images.
however the base image which is python with the tag 3.10.4 is not showing
note that when I build another image of my Dockerfile, I can see from the output that the base image python:3.10.4 is loaded from the cache.
And that means the base image python:3.10.4 is installed and cached successfully but why it is not showing when I list the images, even when I use the -a flag, like the following
docker image ls -a
my docker version is Docker version 20.10.13, build a224086
my OS is windows 10
It looks like you're using Docker buildkit.
While the traditional docker build mechanism would pull referenced images into the local image collection, buildkit has its own caching mechanism and the images it pulls won't show up in the output of docker image ls.

how can you put images in a container (image) in advance? [duplicate]

I am running a docker-in-docker container that always uses the same few images.
I would like to pre-pull those in my dind container so I don't have to pull them at startup.
How would I be able to achieve this?
I was thinking of building my own dind image along the lines of
FROM docker:18.06.1-ce-dind
RUN apk update && \
apk upgrade && \
apk add bash
RUN docker pull pre-pulled-image:1.0.0
Obviously above Dockerfile will not build because docker is not running during the build, but it should give an idea of what I'd like to achieve.
You can't do this.
If you look at the docker:dind Dockerfile it contains a declaration
VOLUME /var/lib/docker
That makes it impossible to create a derived image with any different content in that directory tree. (This is the same reason you can't create a mysql or postgresql image with prepopulated data.)
Here is some way to achieve this.
The idea is to save images that you want to use, and then to import this images during the container startup process.
For example, you can use a folder images where to store your images.
And you have to use a customized entrypoint script that will import the images.
So the Dockerfile will be :
FROM docker:19.03-dind
RUN apk add --update --no-cache bash tini
COPY ./images /images
COPY ./entrypoint.sh /entrypoint.sh
ENTRYPOINT ["tini", "--", "/entrypoint.sh"]
(tini is necessary to load images in background)
And the entrypoint.sh script :
#!/bin/bash
# Turn on bash's job control
set -m
# Start docker service in background
/usr/local/bin/dockerd-entrypoint.sh &
# Wait that the docker service is up
while ! docker info; do
echo "Waiting docker..."
sleep 3
done
# Import pre-installed images
for file in /images/*.tar; do
docker load <$file
done
# Bring docker service back to foreground
fg %1
FYI, an example on how to save an image :
docker pull instrumentisto/nmap
docker save instrumentisto/nmap >images/nmap.tar

Update Docker image when there are no changes to Dockerfile

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?

Multistage docker build with no-cache

I have a multistage Dockerfile which is like below. When one of the image referred in the Dockerfile got updated, how to make sure latest versions are pulled again/always pulled while building image based on this Dockerfile. Running docker build command with --no-cache is still referring older versions of image but not actually pulling latest from docker registry.
docker build --no-cache -t test_deploy -f Dockerfile
FROM myreg.abc.com/testing_node:latest AS INITIMG
....
....
RUN npm install
RUN npm run build
FROM myreg.abc.com/testing_nginx:latest
COPY --from=INITIMG /sourcecode/build/ /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
--no-cache tells docker not to re-use cached layers. It does not pull images if they are already existing locally.
You can either docker pull myreg.abc.com/testing_node:latest before building or, more conveniently, also add --pull when calling docker build.
See https://docs.docker.com/engine/reference/commandline/build/#options

Pre-pull images in Docker in Docker (dind)

I am running a docker-in-docker container that always uses the same few images.
I would like to pre-pull those in my dind container so I don't have to pull them at startup.
How would I be able to achieve this?
I was thinking of building my own dind image along the lines of
FROM docker:18.06.1-ce-dind
RUN apk update && \
apk upgrade && \
apk add bash
RUN docker pull pre-pulled-image:1.0.0
Obviously above Dockerfile will not build because docker is not running during the build, but it should give an idea of what I'd like to achieve.
You can't do this.
If you look at the docker:dind Dockerfile it contains a declaration
VOLUME /var/lib/docker
That makes it impossible to create a derived image with any different content in that directory tree. (This is the same reason you can't create a mysql or postgresql image with prepopulated data.)
Here is some way to achieve this.
The idea is to save images that you want to use, and then to import this images during the container startup process.
For example, you can use a folder images where to store your images.
And you have to use a customized entrypoint script that will import the images.
So the Dockerfile will be :
FROM docker:19.03-dind
RUN apk add --update --no-cache bash tini
COPY ./images /images
COPY ./entrypoint.sh /entrypoint.sh
ENTRYPOINT ["tini", "--", "/entrypoint.sh"]
(tini is necessary to load images in background)
And the entrypoint.sh script :
#!/bin/bash
# Turn on bash's job control
set -m
# Start docker service in background
/usr/local/bin/dockerd-entrypoint.sh &
# Wait that the docker service is up
while ! docker info; do
echo "Waiting docker..."
sleep 3
done
# Import pre-installed images
for file in /images/*.tar; do
docker load <$file
done
# Bring docker service back to foreground
fg %1
FYI, an example on how to save an image :
docker pull instrumentisto/nmap
docker save instrumentisto/nmap >images/nmap.tar

Resources