Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
037a744b
Commit
037a744b
authored
Dec 27, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/docker: customize run command [GH-648]
parent
be786108
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
48 additions
and
10 deletions
+48
-10
CHANGELOG.md
CHANGELOG.md
+2
-0
builder/docker/builder.go
builder/docker/builder.go
+1
-1
builder/docker/config.go
builder/docker/config.go
+12
-0
builder/docker/driver.go
builder/docker/driver.go
+8
-1
builder/docker/driver_docker.go
builder/docker/driver_docker.go
+17
-6
builder/docker/step_run.go
builder/docker/step_run.go
+3
-2
website/source/docs/builders/docker.html.markdown
website/source/docs/builders/docker.html.markdown
+5
-0
No files found.
CHANGELOG.md
View file @
037a744b
...
...
@@ -45,6 +45,8 @@ IMPROVEMENTS:
"Packer Builder" so that they are easily recognizable. [GH-642]
*
builder/amazon/all: Copying AMIs to multiple regions now happens
in parallel. [GH-495]
*
builder/docker: A "run
\_
command" can be specified, configuring how
the container is started. [GH-648]
*
builder/openstack: In debug mode, the generated SSH keypair is saved
so you can SSH into the machine. [GH-746]
*
builder/qemu: Floppy files are supported. [GH-686]
...
...
builder/docker/builder.go
View file @
037a744b
...
...
@@ -25,7 +25,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
}
func
(
b
*
Builder
)
Run
(
ui
packer
.
Ui
,
hook
packer
.
Hook
,
cache
packer
.
Cache
)
(
packer
.
Artifact
,
error
)
{
driver
:=
&
DockerDriver
{
Ui
:
ui
}
driver
:=
&
DockerDriver
{
Tpl
:
b
.
config
.
tpl
,
Ui
:
ui
}
if
err
:=
driver
.
Verify
();
err
!=
nil
{
return
nil
,
err
}
...
...
builder/docker/config.go
View file @
037a744b
...
...
@@ -12,6 +12,7 @@ type Config struct {
ExportPath
string
`mapstructure:"export_path"`
Image
string
Pull
bool
RunCommand
[]
string
`mapstructure:"run_command"`
tpl
*
packer
.
ConfigTemplate
}
...
...
@@ -28,6 +29,17 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
return
nil
,
nil
,
err
}
// Defaults
if
len
(
c
.
RunCommand
)
==
0
{
c
.
RunCommand
=
[]
string
{
"run"
,
"-d"
,
"-i"
,
"-t"
,
"-v"
,
"{{.Volumes}}"
,
"{{.Image}}"
,
"/bin/bash"
,
}
}
// Default Pull if it wasn't set
hasPull
:=
false
for
_
,
k
:=
range
md
.
Keys
{
...
...
builder/docker/driver.go
View file @
037a744b
...
...
@@ -28,5 +28,12 @@ type Driver interface {
// ContainerConfig is the configuration used to start a container.
type
ContainerConfig
struct
{
Image
string
RunCommand
[]
string
Volumes
map
[
string
]
string
}
// This is the template that is used for the RunCommand in the ContainerConfig.
type
startContainerTemplate
struct
{
Image
string
Volumes
string
}
builder/docker/driver_docker.go
View file @
037a744b
...
...
@@ -12,6 +12,7 @@ import (
type
DockerDriver
struct
{
Ui
packer
.
Ui
Tpl
*
packer
.
ConfigTemplate
}
func
(
d
*
DockerDriver
)
Export
(
id
string
,
dst
io
.
Writer
)
error
{
...
...
@@ -40,19 +41,29 @@ func (d *DockerDriver) Pull(image string) error {
}
func
(
d
*
DockerDriver
)
StartContainer
(
config
*
ContainerConfig
)
(
string
,
error
)
{
//
Args that we're going to pass to Docker
args
:=
[]
string
{
"run"
,
"-d"
,
"-i"
,
"-t"
}
//
Build up the template data
var
tplData
startContainerTemplate
tplData
.
Image
=
config
.
Image
if
len
(
config
.
Volumes
)
>
0
{
volumes
:=
make
([]
string
,
0
,
len
(
config
.
Volumes
))
for
host
,
guest
:=
range
config
.
Volumes
{
volumes
=
append
(
volumes
,
fmt
.
Sprintf
(
"%s:%s"
,
host
,
guest
))
}
args
=
append
(
args
,
"-v"
,
strings
.
Join
(
volumes
,
","
)
)
tplData
.
Volumes
=
strings
.
Join
(
volumes
,
","
)
}
args
=
append
(
args
,
config
.
Image
,
"/bin/bash"
)
// Args that we're going to pass to Docker
args
:=
config
.
RunCommand
for
i
,
v
:=
range
args
{
var
err
error
args
[
i
],
err
=
d
.
Tpl
.
Process
(
v
,
&
tplData
)
if
err
!=
nil
{
return
""
,
err
}
}
d
.
Ui
.
Message
(
fmt
.
Sprintf
(
"Run command: docker %s"
,
strings
.
Join
(
args
,
" "
)))
// Start the container
var
stdout
,
stderr
bytes
.
Buffer
...
...
builder/docker/step_run.go
View file @
037a744b
...
...
@@ -18,12 +18,13 @@ func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
runConfig
:=
ContainerConfig
{
Image
:
config
.
Image
,
RunCommand
:
config
.
RunCommand
,
Volumes
:
map
[
string
]
string
{
tempDir
:
"/packer-files"
,
},
}
ui
.
Say
(
"Starting docker container
with /bin/bash
"
)
ui
.
Say
(
"Starting docker container
...
"
)
containerId
,
err
:=
driver
.
StartContainer
(
&
runConfig
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error running container: %s"
,
err
)
...
...
website/source/docs/builders/docker.html.markdown
View file @
037a744b
...
...
@@ -59,6 +59,11 @@ Optional:
`docker pull`
prior to use. Otherwise, it is assumed the image already
exists and can be used. This defaults to true if not set.
*
`run_command`
(array of strings) - An array of arguments to pass to
`docker`
in order to run the container. By default this is set to
`["run", "-d", "-i", "-t", "-v", "{{.Volumes}}", "{{.Image}}", "/bin/bash"]`
.
As you can see, you have a couple template variables to customize, as well.
## Using the generated artifact
Once the tar artifact has been generated, you will likely want to import, tag,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment