0

There is a Dockerfile in the current directory. Initially we could:

  1. docker build to build the image
  2. make use of the built image to docker run to start the corresponding docker container

But often times there is need to update the Dockerfile. As a result, there would be the following steps to restart the docker container with the latest update

  1. docker build to build the image again
  2. kill/stop the running docker container
  3. remove the image
  4. docker run to start the docker container again with the updated docker image

But there are 4 steps involved =>

Question: How to minimize the 4 steps above, e.g. rebuild the docker image and restart the updated container in one go like the docker-compose up --build <sevice_name>

Rui
  • 3,454
  • 6
  • 37
  • 70
  • 1
    With a `Makefile` maybe? – Mark Setchell Aug 15 '23 at 20:34
  • I don't think Make is the right tool here (how would you express the file dependencies to restart the container?) but this seems like a simple enough shell script, or a basic Compose setup as hinted here. – David Maze Aug 16 '23 at 11:12

1 Answers1

0

I've made a simple script that will trigger an action once a watched file get modified.

The answer can be found here.

What you can do in this case, is simply write a small bash script with the 4 steps needed. and add it to be executed to the script whenever the dockerfile change occurs.

An alternative solution to reduce this commands series, docker-compse can be a good option as the docker-compose down command will stop containers and removes containers, networks, volumes, and images created by the docker-compose up command.

Lastley, as suggested in the comments, You can use makefile which defines a set of commands to run and the make utility runs them.

George
  • 2,292
  • 2
  • 10
  • 21
  • The question came out in light of the `docker-compose` command's `docker-compose up --build `. So similarly I wonder if it is possible to use `docker` command also to `build` and `run` in one go – Rui Aug 15 '23 at 20:59
  • I dont believe there is a docker-supported option for this, My guess is its only possible with docker commands using the makefile. And am not even familiar with the `makefile` that's what I get with the fast reading about. – George Aug 15 '23 at 21:08