How can I run a docker container and commit the changes once a script completes?

后端 未结 4 738
南旧
南旧 2021-02-14 23:01

I want to set up a cron job to run a set of commands inside a docker container and then commit the changes to the docker image. I\'m able to run the container as a daemon and ge

相关标签:
4条回答
  • 2021-02-14 23:27

    I didn't find any of these answers satisfying, as my goal was to 1) launch a container, 2) run a setup script, and 3) capture/store the state after setup, so I can instantly run various scripts against that state later. And all in a local, automated, continuous integration environment (e.g. scripted and non-interactive).

    Here's what I came up with (and I run this in Travis-CI install section) for setting up my test environment:

    #!/bin/bash
    
    # Run a docker with the env boot script
    docker run ubuntu:14.04 /path/to/env_setup_script.sh
    
    # Get the container ID of the last run docker (above)
    export CONTAINER_ID=`docker ps -lq`
    
    # Commit the container state (returns an image_id with sha256: prefix cut off)
    # and write the IMAGE_ID to disk at ~/.docker_image_id
    (docker commit $CONTAINER_ID | cut -c8-) > ~/.docker_image_id
    

    Note that my base image was ubuntu:14.04 but yours could be any image you want.

    With that setup, now I can run any number of scripts (e.g. unit tests) against this snapshot (for Travis, these are in my script section). e.g.:

    docker run `cat ~/.docker_image_id` /path/to/unit_test_1.sh
    docker run `cat ~/.docker_image_id` /path/to/unit_test_2.sh
    
    0 讨论(0)
  • There are following ways to persist container data:

    1. Docker volumes

    2. Docker commit

      a) create container from ubuntu image and run a bash terminal.

         $ docker run -i -t ubuntu:14.04 /bin/bash
      

      b) Inside the terminal install curl

         # apt-get update
         # apt-get install curl
      

      c) Exit the container terminal

         # exit
      

      d) Take a note of your container id by executing following command :

         $ docker ps -a
      

      e) save container as new image

         $ docker commit <container_id> new_image_name:tag_name(optional)
      

      f) verify that you can see your new image with curl installed.

         $ docker images           
      
         $ docker run -it new_image_name:tag_name bash
            # which curl
              /usr/bin/curl
      
    0 讨论(0)
  • 2021-02-14 23:42

    Try this if you want an auto commit for all which are running. Put this in a cron or something, if this helps

    #!/bin/bash
    for i in `docker ps|tail -n +2|awk '{print $1}'`; do docker commit -m "commit new change" $i; done
    
    0 讨论(0)
  • 2021-02-14 23:43

    Run it in the foreground, not as daemon. When it ends the script that launched it takes control and commits/push it

    0 讨论(0)
提交回复
热议问题