mirror of https://github.com/docker/docs.git
Merge pull request #6571 from vbatts/vbatts-raw_json
raw json for `docker save`
This commit is contained in:
commit
a5f5d5e8d7
|
|
@ -860,7 +860,7 @@ func getContainersByName(eng *engine.Engine, version version.Version, w http.Res
|
|||
}
|
||||
var job = eng.Job("container_inspect", vars["name"])
|
||||
if version.LessThan("1.12") {
|
||||
job.SetenvBool("dirty", true)
|
||||
job.SetenvBool("raw", true)
|
||||
}
|
||||
streamJSON(job, w, false)
|
||||
return job.Run()
|
||||
|
|
@ -872,7 +872,7 @@ func getImagesByName(eng *engine.Engine, version version.Version, w http.Respons
|
|||
}
|
||||
var job = eng.Job("image_inspect", vars["name"])
|
||||
if version.LessThan("1.12") {
|
||||
job.SetenvBool("dirty", true)
|
||||
job.SetenvBool("raw", true)
|
||||
}
|
||||
streamJSON(job, w, false)
|
||||
return job.Run()
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ func (daemon *Daemon) ContainerInspect(job *engine.Job) engine.Status {
|
|||
if container := daemon.Get(name); container != nil {
|
||||
container.Lock()
|
||||
defer container.Unlock()
|
||||
if job.GetenvBool("dirty") {
|
||||
if job.GetenvBool("raw") {
|
||||
b, err := json.Marshal(&struct {
|
||||
*Container
|
||||
HostConfig *runconfig.HostConfig
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package graph
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"github.com/dotcloud/docker/engine"
|
||||
|
|
@ -135,8 +134,8 @@ func (s *TagStore) CmdLookup(job *engine.Job) engine.Status {
|
|||
}
|
||||
name := job.Args[0]
|
||||
if image, err := s.LookupImage(name); err == nil && image != nil {
|
||||
if job.GetenvBool("dirty") {
|
||||
b, err := json.Marshal(image)
|
||||
if job.GetenvBool("raw") {
|
||||
b, err := image.RawJson()
|
||||
if err != nil {
|
||||
return job.Error(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -149,6 +149,22 @@ func jsonPath(root string) string {
|
|||
return path.Join(root, "json")
|
||||
}
|
||||
|
||||
func (img *Image) RawJson() ([]byte, error) {
|
||||
root, err := img.root()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to get root for image %s: %s", img.ID, err)
|
||||
}
|
||||
fh, err := os.Open(jsonPath(root))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to open json for image %s: %s", img.ID, err)
|
||||
}
|
||||
buf, err := ioutil.ReadAll(fh)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read json for image %s: %s", img.ID, err)
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// TarLayer returns a tar archive of the image's filesystem layer.
|
||||
func (img *Image) TarLayer() (arch archive.Archive, err error) {
|
||||
if img.graph == nil {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
// save a repo and try to load it
|
||||
func TestSaveAndLoadRepo(t *testing.T) {
|
||||
// save a repo and try to load it using stdout
|
||||
func TestSaveAndLoadRepoStdout(t *testing.T) {
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
|
||||
out, _, err := runCommandWithOutput(runCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
|
||||
|
|
@ -25,6 +25,10 @@ func TestSaveAndLoadRepo(t *testing.T) {
|
|||
out, _, err = runCommandWithOutput(commitCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
|
||||
|
||||
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
|
||||
before, _, err := runCommandWithOutput(inspectCmd)
|
||||
errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
|
||||
|
||||
saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
|
||||
saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
|
||||
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
|
||||
|
|
@ -39,14 +43,70 @@ func TestSaveAndLoadRepo(t *testing.T) {
|
|||
errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
|
||||
|
||||
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
|
||||
out, _, err = runCommandWithOutput(inspectCmd)
|
||||
errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", out, err))
|
||||
after, _, err := runCommandWithOutput(inspectCmd)
|
||||
errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
|
||||
|
||||
if before != after {
|
||||
t.Fatalf("inspect is not the same after a save / load")
|
||||
}
|
||||
|
||||
deleteContainer(cleanedContainerID)
|
||||
deleteImages(repoName)
|
||||
|
||||
os.Remove("/tmp/foobar-save-load-test.tar")
|
||||
|
||||
logDone("save - save a repo")
|
||||
logDone("load - load a repo")
|
||||
logDone("save - save a repo using stdout")
|
||||
logDone("load - load a repo using stdout")
|
||||
}
|
||||
|
||||
// save a repo and try to load it using flags
|
||||
func TestSaveAndLoadRepoFlags(t *testing.T) {
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
|
||||
out, _, err := runCommandWithOutput(runCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
|
||||
|
||||
cleanedContainerID := stripTrailingCharacters(out)
|
||||
|
||||
repoName := "foobar-save-load-test"
|
||||
|
||||
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
||||
out, _, err = runCommandWithOutput(inspectCmd)
|
||||
errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
|
||||
|
||||
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
|
||||
out, _, err = runCommandWithOutput(commitCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
|
||||
|
||||
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
|
||||
before, _, err := runCommandWithOutput(inspectCmd)
|
||||
errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
|
||||
|
||||
saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
|
||||
saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
|
||||
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
|
||||
out, _, err = runCommandWithOutput(saveCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
|
||||
|
||||
deleteImages(repoName)
|
||||
|
||||
loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
|
||||
loadCmd := exec.Command("bash", "-c", loadCmdFinal)
|
||||
out, _, err = runCommandWithOutput(loadCmd)
|
||||
errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
|
||||
|
||||
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
|
||||
after, _, err := runCommandWithOutput(inspectCmd)
|
||||
errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
|
||||
|
||||
if before != after {
|
||||
t.Fatalf("inspect is not the same after a save / load")
|
||||
}
|
||||
|
||||
deleteContainer(cleanedContainerID)
|
||||
deleteImages(repoName)
|
||||
|
||||
os.Remove("/tmp/foobar-save-load-test.tar")
|
||||
|
||||
logDone("save - save a repo using -o")
|
||||
logDone("load - load a repo using -i")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -410,6 +410,7 @@ func (srv *Server) exportImage(eng *engine.Engine, name, tempdir string) error {
|
|||
return err
|
||||
}
|
||||
job := eng.Job("image_inspect", n)
|
||||
job.SetenvBool("raw", true)
|
||||
job.Stdout.Add(json)
|
||||
if err := job.Run(); err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Reference in New Issue