Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

docker-compose rebuild image of one service and use it

I'm trying to get docker-compose to:

  • rebuild the image of a certain service (only source code has changed, not the Dockerfile)
  • re-"up" just that service within docker-compose, using the new image

I find I have to perform a docker build on that service's Dockerfile, then perform a docker-compose up <service-name> for the new image to be used.

Is there a way I can do this just using docker-compose?

Below is the long meandering path I took to get the new image to be used in docker-compose.


Journey to get docker-compose to use new image

This is the current docker-compose setup.

 Container          Repository          Tag       Image Id       Size
-----------------------------------------------------------------------
books-server   books-server            v06      c35c65375942   13.37 MB
hasura-proxy   hasura/graphql-engine   v1.3.2   a499c4b91ba4   48.34 MB
nginx-proxy    jwilder/nginx-proxy     latest   fcb5a96e19c1   160.6 MB
postgres       postgres                12       7b9cf7d827b4   314 MB

I make a change to books-server, the underlying source code, which gets compiled into an executable, and I want that image rebuilt and used in docker-compose.

If I issue a:

> docker-compose up --detach --build books-server

Result:

books-server is up-to-date

Try:

> docker-compose build --no-cache books-server

Result:

books-server uses an image, skipping

Try:

> docker-compose up --build --no-deps --force-recreate -d books-server

Result:

Recreating books-server ... done

Check:

> docker-compose images

Result

 Container          Repository          Tag       Image Id       Size
-----------------------------------------------------------------------
books-server   books-server            v06      c35c65375942   13.37 MB
hasura-proxy   hasura/graphql-engine   v1.3.2   a499c4b91ba4   48.34 MB
nginx-proxy    jwilder/nginx-proxy     latest   fcb5a96e19c1   160.6 MB
postgres       postgres                12       7b9cf7d827b4   314 MB

Image id of books-server is still c35c65375942

Try:

> docker-compose restart books-server
Restarting books-server ... done

Check images:

Still using image Id c35c65375942

> docker-compose images

 Container          Repository          Tag       Image Id       Size
-----------------------------------------------------------------------
books-server   books-server            v06      c35c65375942   13.37 MB
hasura-proxy   hasura/graphql-engine   v1.3.2   a499c4b91ba4   48.34 MB
nginx-proxy    jwilder/nginx-proxy     latest   fcb5a96e19c1   160.6 MB
postgres       postgres                12       7b9cf7d827b4   314 MB

Try:

I change to the books-server directory, perform a docker build on its Dockerfile using the same tag (books-server:v06)

> cd ..ooks-server
> docker build -t books-server:v06 .

Result:

A new image is built 55ea56a70aa9 and tagged

Sending build context to Docker daemon  73.43MB
Step 1/21 : FROM google/dart-runtime-base AS dart-runtime
...<snip>...
Removing intermediate container bff4c43f6bf0
 ---> 55ea56a70aa9
Successfully built 55ea56a70aa9
Successfully tagged books-server:v06

I check docker images:

And see the tag has been removed from image c35c65375942 and given to 55ea56a70aa9

> docker images

REPOSITORY                        TAG                 IMAGE ID            CREATED              SIZE
<none>                            <none>              d50d6ccf14a1        About a minute ago   798MB
books-server                      v06                 55ea56a70aa9        About a minute ago   13.4MB
<none>                            <none>              c35c65375942        31 minutes ago       13.4MB

I check docker-compose images:

 Container          Repository          Tag       Image Id       Size
-----------------------------------------------------------------------
books-server   <none>                  <none>   c35c65375942   13.37 MB
hasura-proxy   hasura/graphql-engine   v1.3.2   a499c4b91ba4   48.34 MB
nginx-proxy    jwilder/nginx-proxy     latest   fcb5a96e19c1   160.6 MB
postgres       postgres                12       7b9cf7d827b4   314 MB

books-server is still using the old image, now running anonymously, since the image tag has been reappropriated to 55ea56a70aa9

I try a restart of the service in docker-compose:

> docker-compose restart books-server
Restarting books-server ... done

I check docker-compose images, and see the old image c35c65375942 still being used.

 Container          Repository          Tag       Image Id       Size
-----------------------------------------------------------------------
books-server   <none>                  <none>   c35c65375942   13.37 MB
hasura-proxy   hasura/graphql-engine   v1.3.2   a499c4b91ba4   48.34 MB
nginx-proxy    jwilder/nginx-proxy     latest   fcb5a96e19c1   160.6 MB
postgres       postgres                12       7b9cf7d827b4   314 MB

I re-up docker-compose and finally see the new image (55ea56a70aa9) in use:

> docker-compose up -d books-server
Recreating books-server ... done
> docker-compose images
 Container          Repository          Tag       Image Id       Size
-----------------------------------------------------------------------
books-server   books-server            v06      55ea56a70aa9   13.37 MB
hasura-proxy   hasura/graphql-engine   v1.3.2   a499c4b91ba4   48.34 MB
nginx-proxy    jwilder/nginx-proxy     latest   fcb5a96e19c1   160.6 MB
postgres       postgres                12       7b9cf7d827b4   314 MB

Summary

Is there a docker-compose command that can rebuild the service image and restart that service or must it be done using a docker build Dockerfile and docker-compose up <service>?

Thanks for any insights.

question from:https://stackoverflow.com/questions/65891675/docker-compose-rebuild-image-of-one-service-and-use-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to timsmelik comment I realized I wasn't using a build: directive in my books-server service in docker-compose.yml and only a tagged image name, which docker-compose doesn't easily replace/use when a new version is produced.

Removing the image: directive and replacing it with a relative build: target, a simple docker-compose up --build -d books-server is rebuilding the service and using it within the docker-compose project.

  books-server:
    # ↓ new build directive
    build: ../books-server
    # ↓ old image directive, removed
    # image: books-server:v06
    container_name: books-server
    <other settings>...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...