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

后端 未结 4 739
南旧
南旧 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条回答
  •  梦毁少年i
    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
    

提交回复
热议问题