Docker image doesnt retain changed information - docker

OK so my requirement is to modify the docker image. Following are my steps:
Pull the docker image
Run the container.
Step into the container and modified the file.
Create an image from container
Stop the container
Rerun the container with new image.
I was expecting the file i had modified should be updated, but its not. Is there anything i am missing ?

You need to delete you image docker with docker rmi <imagename> for list all images run docker images

Related

Rename the IMAGE tag of a running container with docker

Just created a docker image with a Dockerfile and started a new docker container using the image previously built. After running the container, I execute "docker ps" and I see the name of the image used by the container in the IMAGE column. This is expected.
After that, I renamed the TAG of my docker image:
docker image tag <old-image> <new-image>
At this point, "docker images" reports two images with exactly the same IMAGE_ID, but different TAG.
Now I remove the initial old-image image.
docker rmi <old-image>
Now "docker images" reports just a single image with TAG new-image. At this point, when I execute docker ps, I see my container is using the IMAGE_ID on the IMAGE column, not the image TAG new-image.
I know if I create a new container, that new container will use the proper image TAG new-image. However, I would like to learn if it's possible to change this, without creating a new container.
Is this possible in some way? How can this be done? Thanks!

is it possible recover the deleted docker image

I execute the docker(Docker version 19.03.1, build 74b1e89) clean command in the server:
docker system prune
to my surprise, this command delete the container that stopped. That's the problem, some container stopped by some reason but I still want to use it in the furture. Now it is deleted, is it possible to recover the mistaking deleted container that stopped?
No, it is not possible. You have to repull the image docker pull image, or rebuild the image.
Docker images and containers are not the same. See this answer.
docker system prune tells you:
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N]
So it should be no surprise that it removed your container and possibly also the image it was based on (if no other container was running based on that same image).
I believe it is not possible to recover your image or container, however you can rebuild them. Depending on how the image was obtained you have to run:
docker pull <image> # for an image on dockerhub or other registry
docker build <arguments> # for an image based on a dockerfile
After that you will have your image and you can run a container again with:
docker run <image>

what is the correct way to update a docker image

I created an ubuntu docker container, copied a mirror of a c++ library (sized 1.2 gb) I'll be using on it (on the docker container's home directory), built it on it and forgot to remove the mirror before creating the image and pushing to docker hub.
I then tried to re-run the container from the image to remove the c++ mirror so i can commit the new image , but the new image didn't downsize for a reason i ignore . After i run docker images i still have :
REPOSITORY TAG IMAGE ID CREATED SIZE
tawfik741/opencascade-build amd64 74f333aa7293 16 minutes ago 1.79GB
The commands i ran are :
Creating the container with :
docker run --name opencascade-build -it ubuntu:latest bash
Copying the C++ library mirror so i build it on the container :
docker cp opencascade-dev-mirror [my-container-id]:/home
After building my library i did :
sudo docker commit --author "Tawfik" --message "opencascade-build" [my-container-id] tawfik741/opencascade-build:amd64
and pushed it to a private repo with :
docker push tawfik741/opencascade-build:amd64
after figuring up that i forgot to remove that opencascade-dev-mirror from the container i decided to run the container , remove it , and save the new image , i tried in to save a new image but it's exactly the same size as the old one :
I ran the docker container from the image with :
docker run -it tawfik741/opencascade-build:amd64 /bin/bash
I updated the container then ran :
docker commit --author "Onboard SARL" --message "opencascade-build" [my-container's id] tawfik741/opencascade-build:correction-amd64-correction
but the tawfik741/opencascade-build:amd64-correction has the same size as the tawfik741/opencascade-build:amd64 image .
You cannot edit, modify, or update an image once you've created it.
You can create a new image based on an existing image. The way Docker works internally, the new image always contains the entire old image, plus a description of what changed from the old image. Doing this never makes the new image smaller, only larger.
You need to start over and create a new image starting from the original base image. If you use the standard docker build command and Dockerfile system, it should be enough to delete the COPY line that adds the large file to the image and rebuild. If you're using docker commit, you need to completely start over and hope you repeat the same manual commands in the same way; even if you're trying to "iterate rapidly" you'll be much better off switching to a Dockerfile.

Updating a docker image without the original Dockerfile

I am working on Flask app running on ec2 server inside a docker image.
The old dev seems to have removed the original Dockerfile, and I can't seem to find any instructions on a way to push my changes into to the docker image with out the original.
I can copy my changes manually using:
docker cp newChanges.py doc:/root/doc/server_python/
but I can't seem to find a way to restart flask. I know this is not the ideal solution but it's the only idea I have.
There is one way to add newChanges.py to existing image and commit that image with a new tag so you will have a fall back option if you face any issue.
Suppose you run alpine official image and you don't have DockerFile
Everytime you restart the image you will not have your newChanges.py
docker run --rm -name alpine alpine
Use ls inside the image to see a list of existing files that are created in Dockerfile.
docker cp newChanges.py alpine:/
Run ls and verify your file was copied over
Next Step
To commit these changes to your running container do the following:
Docker ps
Get the container ID and run:
docker commit 4efdd58eea8a updated_alpine_image
Now run your alpine image and you will the see the updated changes as suppose
docker run -it updated_alpine_image
This is what you will see in your update_alpine_image with having DockerFile
This is how you can rebuild the image from existing image. You can also try #uncletall answer as well.
If you just want to restart after docker cp, you can just docker stop $your_container, then docker start $your_container.
If you want to update newChanges.py to docker image without original Dockerfile, you can use docker export -o $your_tar_name.tar $your_container, then docker import $your_tar_name.tar $your_new_image:tag. Later, always reserve the tar to backup server for future use.
If you want continue to develop later use a Dockerfile in the future for further changes:
you can use docker commit to generate a new image, and use docker push to push it to dockerhub with the name something like my_docker_id/my_image_name:v1.0
Your new Dockerfile:
FROM my_docker_id/my_image_name:v1.0
# your new thing here
ADD another_new_change.py /root/
# others
You can try to examine the history of the image, from there you can probably re-create the Dockerfile. Try using docker history --no-trunc image-name
See this answer for more details

What's the difference between the docker commands: run, build, and create

I see there are three docker commands that seem to do very similar things:
docker build
docker create
docker run
What are the differences between these commands?
docker build builds a new image from the source code.
docker create creates a writeable container from the image and prepares it for running.
docker run creates the container (same as docker create) and runs it.
docker build . converts your Dockerfile into an image.
docker create your-image creates a container from your image from step 1.
docker start container_id starts the container from step 2.
docker run image is a shortcut for 2. and 3. (docker create image and
docker start container_id).
Here is the difference between image and container:
Image
An image is a specified snapshot of your filesystem and includes the starting command of your container. An image occupies just disk-space, it does not occupy memory/cpu. To create an image you usually create instructions how to build that image in aDockerfile. FROM and RUN commands in the docker file create the file-snapshot. One may build an image from a docker file with docker build <dockerfile>
Container
You can create new containers with an image. Each container has a file-snapshot which is based on the file-snapshot created by the image. If you start a container it will run the command you specified in your docker file CMD and will use part of your memory and cpu. You can start or stop a container. If you create a container, its not started by default. This means you can't communicate to the container via ports etc. You have to start it first. One may create an container from an image by docker create <image>. When a container has been created it shows the id in the terminal. One may start it with docker start <container_id>.

Resources