0

I'm trying to learn how to write a Dockerfile. Currently my strategy is as follows:

  1. Guess what commands are correct to write based documentation.
  2. Run sudo docker-compose up --build -d to build a docker container
  3. Wait ~5 minutes for my anaconda packages to install
  4. Find that I made a mistake on step 15, and go back to step 1.

Is there a way to interactively enter the commands for a Dockerfile, or to cache the first 14 successful steps so I don't need to rebuild the entire file? I saw something about docker exec but it seems that's only for running containers. I also want to try and use the same syntax as I use in the dockerfile (i.e. ENTRYPOINT and ENV) since I'm not sure what the bash equivalent is/if it exists.

Alex Li
  • 246
  • 4
  • 11
  • 2
    `to cache the first 14 successful steps so I don't need to rebuild the entire file` Docker does have caching, though. Unless you're changing the bottom layer of your image, you shouldn't have to rebuild the whole thing. – Nick ODell Apr 02 '22 at 01:57
  • 1
    `Is there a way to interactively enter the commands for a Dockerfile,` In general, you can get an interactive shell using `docker run -it /bin/bash` (Assuming bash is installed inside the container. Some images only have `/bin/sh`.) – Nick ODell Apr 02 '22 at 01:59
  • So to use docker run, will I need to make an image that compiles first and use it's name as , then run the commands that I want on it? – Alex Li Apr 02 '22 at 02:03
  • 1
    You don't need an image that compiles. You can use the image hash from the last stage that built successfully. You can use an image hash anywhere you'd use a tag. See this answer: https://stackoverflow.com/a/63098810/530160 – Nick ODell Apr 02 '22 at 02:26

1 Answers1

1

you can run docker-compose without the --build flag that way you don't have to rebuild the image every time, although as you are testing the Dockerfile i don't know if you have much options here; the docker should cache automatically the builds but only if there's no changes from the last time that you made a build, and there's no way to build a image interactively, docker doesn't work like that, lastly, the docker exec is just to run commands inside the container that was created from the build.

some references for you: docker cache, docker file best practices

Yago Biermann
  • 1,494
  • 1
  • 7
  • 20