how force deleting docker image affects existing containers using it - docker

When i try to delete an image via docker rmi that has an existing container, i get an error message. ( normal )
When i add force flag docker rmi -f the image is deleted and when i check containers state using docker ps -athe container is still there but image namebecomes an ID.
So my question, from where comes this ID ? Is it a copy of the image that is kept in the cache and used for existing containers cause when i check docker images i find an image with in repo and in name and its ID is the one that is newly affected to old existing containers.
Another question that follows :
Once a container is created, is changing anything on the existing ( local ) image affecting in any way the existing containers ?
Thanks.

A docker image has a name and an image ID. This blog describes where the image ID is comming from in pre-docker-v1.10 and after it.
If you perform docker rmi -f image of an image which is used by a running container you're actually not deleting the real image but deleting the name and tag on the image.
So indeed docker ps will show:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
578e28246977 96931e4c66bd "/bin/tini -- /usr/lo" 3 minutes ago Up 3 minutes 8080/tcp, 50000/tcp drunk_shannon
But docker images is still showing your image:
<none> <none> 96931e4c66bd 6 weeks ago 711.9 MB
It's just untagged. The image isn't also deleted after you delete this container. The image remains in the list. You can start new containers from it with docker run -d 96931e4c66bd (by using the ID)
You can even retag it:
docker tag 96931e4c66bd my-jenkins:1.0
Than docker images shows:
my-jenkins 1.0 96931e4c66bd 6 weeks ago 711.9 MB
Another question that follows : Once a container is created, is
changing anything on the existing ( local ) image affecting in any way
the existing containers ?
No, when you make an "update" on your image (same name), the running container will probably lose it's image-name (only has an ID, just like when you're deleting your image during the container is running).
You'll need to reexecute the run-command with the new image (which will have another image-ID after the update) to have a container running from the newest image.

Related

How to reconnect to default docker engine

I have installed docker on my Windows 10, then I've created some images.
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 2cb0d9787c4d 3 weeks ago 1.85kB
Then I created my first machine using docker-machine create connected to hyperv driver.
so when I do docker images I get:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
My question is how can I reconnect to the default docker engine so if I run docker images I get:
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 2cb0d9787c4d 3 weeks ago 1.85kB
try image instead of images
docker image list
There seems to be some inconsistency w.r.t the two i.e. image/images. Though one would expect the output to be same but there are few differences.

the approach to restore a pre-configured docker image

I am completely new to docker. I have a quick question about docker images.
Assume that I have setup a local docker image with certain software / server installed. So now I would need to set a checkpoint / snapshot here, then all the work done after this checkpoint is temporary; which means at a certain time, I would restore the original image (from that checkpoint) and overwrite everything in the temporary image.
My first question is if the above use-case make sense?
My second question, if the above make sense, what is the approach in doing that checkpoint (simply how, as I am keeping the checkpoint image in local diskspace only, no cloud repos involved) and how to restore the images to overwrite everything in the temporary image when needed.
Though I have read a bit of docker documentation, but am still struggling in the conceptual things.
It makes sense even though you could consider managing data in container or volume (or host folder mounted in the container).
That way the data remains persistent even when you stop and restart the container.
what is the approach in doing that checkpoint
If your container does not mount a volume, and has its data inside, then yes, stopping and removing a container will lose that data.
One possibility is to create that snapshot with docker commit.
That will freeze the container state as a new image, that you can run later.
Example:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3f279d17e0a ubuntu:12.04 /bin/bash 7 days ago Up 25 hours desperate_dubinsky
197387f1b436 ubuntu:12.04 /bin/bash 7 days ago Up 25 hours focused_hamilton
$ docker commit c3f279d17e0a svendowideit/testimage:version3
f5283438590d
$ docker images
REPOSITORY TAG ID CREATED SIZE
svendowideit/testimage version3 f5283438590d 16 seconds ago 335.7 MB
As far as I know there is nothing like checkpoint or so with respect to Docker. But you can save the actions performed on the container as Images to create a new Image.
Giving an example to have better understanding:
Lets run a container using an base Ubuntu Image and create a folder inside the container:
#docker run -it ubuntu:14.04 /bin/bash
root#58246867493d:/#
root#58246867493d:/# cd /root
root#58246867493d:~# ls
root#58246867493d:~# mkdir TEST_DIR
root#58246867493d:~# exit
Status of the exited container:
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58246867493d ubuntu:14.04 "/bin/bash" 2 minutes ago Exited (127) 57 seconds ago hungry_turing
Now you can commit the changes so that the container will be saved into a new Docker Image:
#docker commit 58246867493d ubuntu:15.0
Get docker Images
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
**ubuntu 15.0 acac1f3733b2 10 seconds ago 188MB**
ubuntu 14.04 132b7427a3b4 10 hours ago 188MB
Run the newly build Image to see the changes committed in the previous container.
# docker run -it ubuntu:15.0 /bin/bash
root#3a48af5eaec9:/# cd /root/
root#3a48af5eaec9:~# ls
TEST_DIR
root#3a48af5eaec9:~# exit
As you have asked, any further changes you do on this container either can be committed to form a new Image or can be ignored .
Hope it helps.

Docker save/load lose original image repository/name/tag

I'm using Docker 1.12.6.
I have pulled an image from the Docker registry.
I have exported the images as tar files using the docker save command.
I removed the original image and container and loaded the exported image using docker load -i myImage.tar.
Now, when running docker images I notice my image have lost its repository/tag information:
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 5fae4d4b9e02 8 weeks ago 581.3 MB
Why does it have this behavior and how do I keep the original image name?
Use
docker save -o filename.tar <repo>:<tag>
The command docker save <image id> removes the repository and tag names.
To solve this, use docker save <repo>:<tag> it will keep the repository and tag name in the saved file. For example:
docker save -o ubutu-18.04.tar ubuntu:18.04
I had the same problem, so I used the following command to fix it manually:
docker tag <Image-ID> <desired Name:Tag>
Reference
[NOTE]:
It's inconsistent: docker save image-repo-name -> docker load
restores name, docker save SHA -> docker load no names or tags,
docker save name:latest -> docker load no names or tags.
AND:
The current (and correct) behavior is as follows:
docker save repo
Saves all tagged images + parents in the repo, and creates a
repositories file listing the tags
docker save repo:tag
Saves tagged image + parents in repo, and creates a repositories file
listing the tag
docker save imageid
Saves image + parents, does not create repositories file. The save
relates to the image only, and tags are left out by design and left as
an exercise for the user to populate based on their own naming
convention.
Reference
A single image ID can have multiple names/tags,
so the way that you loose the the names and tags is
what I would expect to happen after saving and loading the image to/from a tar ball.
Mode details are in the discussion about it here
From docker documentation:
cat exampleimage.tgz | docker import - exampleimagelocal:new
root#mymachine:/tmp# cat myimage.tar | docker import --message "New image imported from tarball" - reponame:my-image-name
sha256:be0794427222dcb81182a59c4664b350ecb5ffb7b37928d52d72b31
root#mymachine:/tmp# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
reponame my-image-name be0794427222 6 seconds ago 4.31GB
This one worked for me.
This is a work around
Go to source docker host machine, create text file containing all the image details using the following command docker image ls > images.txt
The above command will produce a text file similar to the following
REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 293e4ed402ba 2 weeks ago 315MB <none> <none> d8e4b0afd6ba 2 weeks ago 551MB
Make necessary edits to set the tag by using docker image tag command
docker image tag 293e4ed402ba postgres:latest
docker image tag d8e4b0afd6ba wordpress:latest
I wrote a one-line script that importing a bunch of .tar files and immediately tagging the image.
for image in $(ls); do docker tag "$(echo $(docker import $image))" $image ; done
Note, that you should be inside the folder when all the tar files are located.

docker build creates another set of docker image even the build command is the same?

Assume that you can create a 500MB docker image with following command
docker build -t demo:1.0 .
It seems to me that the docker image (demo:1.0) is overwritten when I re-run the same command. However, I noticed that free disk space decreases as I re-run the command. Why does this happen? Is docker creating another set of the image somewhere in the file system? I'd like to know how I can find old-generations of the docker images and delete them.
It's standard docker behavior, if you run
docker images
you may see a dangling image like this
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo 1.0 a1b38444260e 6 days ago 500 MB
<none> <none> 46250b7172c5 6 days ago 500 MB
You can remove the dangling images with the command
docker rmi $(docker images -qa -f "dangling=true")
Docker doesn’t delete such images automatically, despite, it's a logical feature to have. For now this command can be used to do a manual deletion.
While the images are stored in /var/lib/docker/{driver-name}, you wouldn't remove images manually from that path.

Docker: How to save running instance?

I am running an instance of docker, and I would like to save my work - the docs just aren't 100% clear on how to do this, so I'm asking here. I opened the docker instance using:
docker run -it [public dockerhub name]
Now I would like to save all my work locally so that I can come back to it. I don't particularly want to check it into dockerhub, unless that's advisable.
Here's what I have done. I have opened a new docker CLI tab, and done docker ps there to find the ID of the running docker instance. Then in the same tab I tried doing this:
docker commit <docker-id> me/myinstance
This gave me a commit hash.
Can I now safely exit the running docker instance? What command would I use to open it again - do I need to store the commit hash, or can I just do docker run -it me/myinstance?
As the docs mention:
You pull an image from Docker hub
You run that image on a container using docker run <image>
When you make changes to a container, you're not changing the underlying image, so those changes are not persisted if the container is stopped. To persist the changes you've made to the container, you create a new image with docker commit <container_id>
In the example that is on Docker docs:
# What containers are running on my system?
$ docker ps
ID IMAGE COMMAND CREATED
c3f279d17e0a ubuntu:12.04 /bin/bash 7 days ago
197387f1b436 ubuntu:12.04 /bin/bash 7 days ago
# Create a new image called svendowideit/testimage, tag it as "version3"
$ docker commit c3f279d17e0a svendowideit/testimage:version3
f5283438590d
# What images do I have on my system?
$ docker images
REPOSITORY TAG ID
svendowideit/testimage version3 f5283438590d
This way, you have persisted the changes to container c3f279d17e0a, on a new image, called svendowideit/testimage:version3.
Now you have an image with your modification, so you can run it as many times as you want on a container:
$ docker run svendowideit/testimage:version3
Again, containers are stateless. Any change you make inside a container, is lost when that container stops. One way to persist data even after a container exists, is by using volumes. This way your container has access to a directory in the host filesystem, that you can read and write.
Changes made inside a container are not lost when the container exits and containers (container applications) are not stateless unless you have specifically separated the data storage from the application (by mounting folders from the host filesystem or sending data to a database outside of the container).
To see your changes persisted in a container, start the old container (docker start ~) instead of creating a new container (docker run ~).
This is easier to do if you name your containers.
ie.
docker run -it --name containerName imageName
do stuff to your container
docker kill containerName
docker start containerName
You will see that your changes are persisted in that container.
You can also commit your container as an image, which can be pushed to a registry or exported to a file.

Resources