Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to iterate while building a docker container?

Tags:

docker

I'm trying to build a few docker containers and I found the iteration process of editing the Dockerfile, and scripts run within it, clunky. I'm looking for best practices and to find out how others go about.

My initial process was:

  1. docker build -t mycontainer mycontainer
  2. docker run mycontainer
  3. docker exec -i -t < container id > "/bin/bash" # get into container to debug
  4. docker rm -v < container id >
  5. docker rmi mycontainer
  6. Repeat

This felt expensive for each iteration, especially if it was typo.

This alternate process required a little bit less iteration:

  1. Install vim in dockerfile
  2. docker run mycontainer
  3. docker exec -i -t < container id > "/bin/bash" # get into container to edit scripts
  4. docker cp to copy edited files out when done.
  5. If I need to run any command, I carefully remember and update the Dockerfile outside the container.
  6. Rebuild image without vim

This requires fewer iterations, but is not painless since everything's very manual and I have to remember which files changed and got updated.

like image 200
dragonx Avatar asked Jul 24 '15 17:07

dragonx


1 Answers

I've been working with Docker in production since 0.7 and I've definitely felt your pain.

Dockerfile Development Workflow

Note: I always install vim in the container when I'm in active development. I just take it out of the Dockerfile when I release.

  1. Setup tmux/gnu screen/iTerm/your favorite vertical split console utility.
  2. On the right console I run:

    $ vim Dockerfile
    
  3. On the left console I run:

    $ docker build -t username/imagename:latest . && docker run -it -name dev-1 username/imagename:latest
    
  4. Now split the left console horizontally, so that the run STDOUT is above and a shell is below. Here you will run:

    docker exec -it dev-1
    

    and edits internally or do tests with:

    docker exec -it dev-1 <my command>
    
  5. Every time you are satisfied with your work with the Dockerfile save (:wq!) and then in the left console run the command above. Test the behavior. If you are not happy run:

    docker rm dev-1
    

    and then edit again and repeat step #3.

Periodically, when I've built up too many images or containers I do the following:

  • Remove all containers: docker rm $(docker ps -qa)
  • Remove all images: docker rmi $(docker images -q)
like image 120
Elijah Avatar answered Oct 13 '22 21:10

Elijah