0

I am new to Docker and the process I use to create and execute a container from a Dockerfile is the following:

  1. docker build -t (IMAGE_NAME) .
  2. docker docker run -itd --name (CONTAINER_NAME) (IMAGE_NAME)
  3. docker exec -it (CONTAINER_NAME) bash

However, if I want to update my Dockerfile and run the container again I have to do the following:

  1. docker ps to get the ID of my container
  2. docker container stop (ID)
  3. docker rm (ID)

and then I have to repeat the first three steps. Is there a quicker way to do this or have to rerun these steps every time I update my Dockerfile? This is very tedious

0

If time is a concern, enable Buildkit, it is way faster to build.

export DOCKER_BUILDKIT=1

You already figured out to use name on all 3 commands, you can combine all 3 commands

docker rm NAME; docker build -t NAME /absolutepath && docker docker run -itd --name NAME && docker exec -it NAME bash

I use && (run next command when return code is 0=no error) on the execution commands and ; (continue to next command, even on error e.g 'container not found') on the initial removal of the previous container. Since this won't stop the chain in case you forgot you already removed the container. As you also see I encourage you to use absolute path for docker build so it works in any directory, should you happen to move around slightly.

2
  • How would the command be if I used the absolute path for docker build? Could you please write it for me? – kubo Sep 20 '20 at 22:54
  • Thats for you to know, simply as it is shown in your filemanager (full path) or shell, but one way you can get it without knowing where to find it; goto your Dockerfile directory with cd and type echo $PWD you can also pipe that to clipboard with xclip if you have Xorg gui running echo $PWD > xclip (might require a package, like xclip) – dza Sep 20 '20 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.