问题
I have trying to build a Docker image using a Dockerfile available locally.
docker build -t newimage .
I have used this command multiple times earlier too, but somehow its not working currently and i am stuck finding the reason for it.
I will be really helpful if someone can help me with a possible solution or a possible area to look for an issue.
i already had a look over other posts that could have been related for example : Docker build tag repository name
回答1:
Okay! I found out the reason for issue.
DOCKER BUILD PROCESS
When we build a docker image, while creating a image several other intermediate images are generated in the process. We never see them in docker images
because with the generation of next intermediate image the earlier image is removed.And in the end we have only one which is the final image.
The tag we provide using -t
or --tag
is for the final build, and obviously no intermediate container is tagged with the same.
ISSUE EXPLANATION
When we try to build a docker image with Dockerfile sometimes the process is not successfully completed with a similar message like Successfully built image with IMAGEID
So it is so obvious that the build which has failed will not be listed in docker images
Now, the image with tag <none>
is some other image (intermediate). This creates a confusion that the image exists but without a tag, but the image is actually not what the final build should be, hence not tagged.
回答2:
There's nothing wrong with Docker.
An image can have multiple tags:
alpine 3.4 4e38e38c8ce0 6 weeks ago 4.799 MB
alpine latest 4e38e38c8ce0 6 weeks ago 4.799 MB
In this example the image with id 4e38e38c8ce0
is tagged alpine:latest
and alpine:3.4
. If you were to execute docker build -t alpine .
the latest
tag would be removed from the image 4e38e38c8ce0
and assigned to the newly built image (which has a different id).
If you remove the last tag
from an image, the image isn't deleted automatically. It shows up as <none>
.
Docker also uses a cache. So if you build an image with a Dockerfile, change that file, built the image again and than undo the change and build again you will have two images - the image you built in the first and last step are the same. The second image will be "tagged" <none>
.
If you want to keep multiple version of an image use docker build -t
tag:version
image:tag .
where version
tag
is changed every time you make some changes.
Edit: What I called tag is actually the image name and what I called version is called the tag: https://docs.docker.com/engine/reference/commandline/tag/
回答3:
One of walkaround solution for this case, run below command immediately after build command, if all other images has already been taged.
docker tag $(docker image ls | grep "<none>" | awk '{print $3}') newimage:latest
As I know this will work only in linux os.
来源:https://stackoverflow.com/questions/38787763/docker-build-command-with-tag-unable-to-tag-images