简体   繁体   中英

Do I need to `docker commit` in order to push an image into a docker image registry (eg. docker hub)?

Usually according to docs In order to build a docker Image I need to follow these steps:

  1. Create a Dockerfile for my application.

  2. Run docker build . Dockerfile docker build . Dockerfile where the . is the context of my application

  3. The using docker run run my image into a container.

  4. Commit my image into a container

  5. Then using docker push push the image into a container.

Though sometimes just launching the image into a container seems like a waste of time because I can tag my images using the parameter -t into the docker build command. So there's no need to commit a container as an image.

So is neseserily to commit a running container as an image?

You don't need to run and commit. docker commit allows you to create a new image from changes made on existing container. You do need to build and tag your image in a way that will enable you to push it.

docker build -t [registry (defaults to docker hub)]/[your repository]:[image tag] [docker file context folder]

for example:

docker build -t my-repository/some-image:image-tag .

And then:

docker push my-repository/some-image:image-tag

This will build an image from a docker file found in the current folder (where you run the docker build command). The repository in this case is my-repository, the image name is some-image and it's tag is image-tag.

Also please note that you'll have to perform docker login with your credentials to docker hub before you are able to actually push the image.

You can also tag an existing image without rebuilding it. This is useful if you want to push an existing image to a different registry or if you want to create a different image tag. for example:

docker tag my-repository/some-image:image-tag localhost:5000/my-repository/some-image:image-tag

This will add a new tag to the image from the previous example. Note the registry part added (localhost:5000). If you call docker push on that tag (docker push localhost:5000/my-repository/some-image:image-tag) the image will be pushed to a registry found on localhost:5000 (of course you need the registry up and running before trying to push).

There's no need to do so. In order to prove that you can just tag the image and push it into the registry here's an example:

I made the following Dockerfile :

FROM alpine

RUN echo "Hello" > /usr/share/hello.txt

ENTRYPOINT cat /usr/share/hello.txt

Nothing special just generates a txt file and shows its content.

Then I can build my image using tags:

docker build . -t ddesyllas/dummy:201201241200 -t ddesyllas/dummy:201201241200

And then just push them to the registry:

$ docker push ddesyllas/dummy
The push refers to repository [docker.io/ddesyllas/dummy]
1aa99de3dbec: Pushed 
6bc83681f1ba: Mounted from library/alpine 
201908241504: digest: sha256:93e8407b1d52620aeadd769486ef1402b9e310018cae0972760f8c1a03377c94 size: 735
1aa99de3dbec: Layer already exists 
6bc83681f1ba: Layer already exists 
latest: digest: sha256:93e8407b1d52620aeadd769486ef1402b9e310018cae0972760f8c1a03377c94 size: 735

And as you can see from the output you can just build the tags and push it directly, good for your ci/cd pipeline. Though, generally speaking, you may need to launch the application into a container in order to do acceptance and other type of tests (eg. end-to-end tests).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM