How to build an image with a custom name? - docker

When I build an image using docker build:
docker build .
I get an image with a random name.
How to create a docker image with a custom name?
I already know how to set the name in the Dockerfile, but I'm not sure how to use it in the build command.

You can use the -t flag, as shown in the documentation (or run docker build --help to learn about the options you have).
You should do:
docker build -t my-image .
Now the image is created with the name my-image:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-image latest 43070bef9dfa 2 minutes ago 464MB

Related

How do I take this docker image on github modify it, then upload it as a docker image to docker hub

So this is the image on GitHub that I wanted to push to my Docker Hub as an image.
I wanted to add a boostrap4 framework installation to the docker file.
You need to write a dockerfile with the image you want as base image. Install bootstrap4 and build the image. Then you can push the new image to your docker hub.
Dockerfile might look something like this -
FROM mattrayner/lamp:latest-1804
# Your commands to install bootstrap here
CMD ["/run.sh"]
docker build -t my_new_lamp_image:my_tag .
docker tag my_new_lamp_image:my_tag <my_docker_hub_path>/my_new_lamp_image:my_tag
docker push <my_docker_hub_path>/my_new_lamp_image:my_tag

how can i start using docker container using Dockerfile

I am using ubuntu 18.04
I have docker-ce installed
I have a file named Dockerfile
I didn't have any other files
how can I start using this container
Firstly you need to build an image from Dockerfile. To do this:
Go to the directory containing Dockerfile
Run (change <image_name> to some meaningful name): docker build -t <image_name> .
After image is built we can finally run it: docker run -it <image_name>
There multiple options how the image can be run so I encourage you to read some docs.

How to view logs for a docker image?

In the docker world, one can easily see logs for docker container (that is, a running image). But during image creation, one usually issues multiple commands. For example npm install commands in node projects. It would be beneficial to see logs for those commands as well. I quickly searched from the documentation, but didn't find how one can obtain logs for docker image. Is it possible?
Had the same problem, I solved it using
docker build --no-cache --progress=plain -t my-image .
Update: Since this question has been asked, it seems everyone is finding it after seeing the output changes from buildkit. Buildkit includes the following options (docker build --help to see them all):
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--no-cache Do not use cache when building the image
-o, --output stringArray Output destination (format: type=local,dest=path)
--platform string Set platform if server is multi-platform capable
--progress string Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
The option many want with buildkit is --progress=plain:
docker build -t my-image --progress=plain .
If you really want to see the previous build output, you can disable buildkit with an environment variable, but I tend to recommend against this since there are a lot of features from buildkit you'd lose (skipping unused build steps, concurrent build steps, multi-platform images, and new syntaxes for the Dockerfile for features like RUN --mount...):
DOCKER_BUILDKIT=0 docker build -t my-image .
The OP is asking to include the logs of their build within the image itself. Generally I would recommend against this, you'd want those logs outside of the image.
That said, the easiest method for that is to use tee to send a copy of all your command output to a logfile. If you want it attached to the image, output your run commands to a logfile inside of the image with something like:
RUN my-install-cmd | tee /logs/my-install-cmd.log
Then you can run a quick one-off container to view the contents of those logs:
docker run --rm my-image cat /logs/my-install-cmd.log
If you don't need the logs attached to the image, you can log the output of every build with a single change to your build command (instead of lots of changes to the run commands) exactly as JHarris says:
docker build -t my-image . | tee my-image.build.log
With the classic docker build command, if you build without using --rm=true, then you have all the intermediate containers, and each one of those has a log you can review with
docker logs $container_id
And lastly, don't forget there's a history of the layers in the image. They don't show the output of each command, but it is useful for all of those commands that don't log any output and knowing which build each layer comes from particularly when there's lots of caching being used.
docker history my-image
You can see the logs in powershell with this command
docker logs --details <containerId>
There other options with logs here.
Use This: https://github.com/jcalles/docker-wtee
Read instructions and please give me feedback.
Or...
If you need to get logs from running container, and container has volumes exposed, run this:
docker run --rm -it --name testlogs --link <CONTAINERNAME/ID> --network CONTAINERNETWORK -p PORT:8080 --volumes-from CONTAINERNAME/ID javiercalles/wtee sh

Docker image versioning and lifecycle management

I am getting into Docker and am trying to better understand how it works out there in the "real world".
It occurs to me that, in practice:
You need a way to version Docker images
You need a way to tell the Docker engine (running on a VM) to stop/start/restart a particular container
You need a way to tell the Docker engine which version of a image to run
Does Docker ship with built-in commands for handling each of these? If not what tools/strategies are used for accomplishing them? Also, when I build a Docker image (via, say, docker build -t myapp .), what file type is produced and where is it located on the machine?
docker has all you need to build images and run containers. You can create your own image by writing a Dockerfile or by pulling it from the docker hub.
In the Dockerfile you specify another image as the basis for your image, run command install things. Images can have tags, for example the ubuntu image can have the latest or 12.04 tag, that can be specified with ubuntu:latest notation.
Once you have built the image with docker build -t image-name . you can create containers from that image with `docker run --name container-name image-name.
docker ps to see running containers
docker rm <container name/id> to remove containers
Suppose we have a docker file like bellow:
->Build from git without versioning:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments
in here:
fecomments is branch name and comments is the folder name.
->building from git with tag and version:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments -t lordash/comments:v1.0
->Now if you want to build from a directory: first go to comments directory the run command sudo docker build .
->if you want to add tag you can use -t or -tag flag to do that:
sudo docker build -t lordash . or sudo docker build -t lordash/comments .
-> Now you can version your image with the help of tag:
sudo docker build -t lordash/comments:v1.0 .
->you can also apply multiple tag to an image:
sudo docker build -t lordash/comments:latest -t lordash/comments:v1.0 .

Update docker image resulting in orphan image

I can use
docker build -t yzx2003209/my_image .
to build a new image tagged by yzx2003209/my_image:latest
However, the original yzx2003209/my_image:latest will become a <none> image and I have to manually rmi this orphan image.
Is there any way I can just update a image without manually removing orphan images?
There is no way to update an image - images are immutable - when you execute docker build which produce different image it also moving tag there - nothing changed in old image itself - it just don't have a tag anymore. If you want to remove all such images you could execute next command after docker build:
docker build -t yzx2003209/my_image .
docker rmi `docker images -q --filter "dangling=true"`
This command will delete ALL images without tag. Or you could do it in a different order - remove your old image before docker build - while it is still have a tag:
docker rmi yzx2003209/my_image
docker build -t yzx2003209/my_image .
But if your build will fail you will have no image. You could get image id before build and remove image after successful build:
old=`docker images -q yzx2003209/my_image`
docker build -t yzx2003209/my_image .
docker rmi $old
You could also use --rm option to remove intermediate containers after a successful build:
docker build --rm -t yzx2003209/my_image .
docker rmi `docker images -q --filter "dangling=true"`

Resources