Skip to content

Commit 3b3cd0b

Browse files
committed
Add docker build --pull command.
1 parent 9a0bb32 commit 3b3cd0b

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

compose/cli/main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ def build(self, project, options):
119119
120120
Options:
121121
--no-cache Do not use cache when building the image.
122+
--pull Always attempt to pull a newer version of the image
122123
"""
123124
no_cache = bool(options.get('--no-cache', False))
124-
project.build(service_names=options['SERVICE'], no_cache=no_cache)
125+
pull = bool(options.get('--pull', False))
126+
project.build(service_names=options['SERVICE'], no_cache=no_cache, pull=pull)
125127

126128
def help(self, project, options):
127129
"""

compose/project.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ def restart(self, service_names=None, **options):
198198
for service in self.get_services(service_names):
199199
service.restart(**options)
200200

201-
def build(self, service_names=None, no_cache=False):
201+
def build(self, service_names=None, no_cache=False, pull=False):
202202
for service in self.get_services(service_names):
203203
if service.can_be_built():
204-
service.build(no_cache)
204+
service.build(no_cache, pull)
205205
else:
206206
log.info('%s uses an image, skipping' % service.name)
207207

compose/service.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def _get_container_host_config(self, override_options, one_off=False):
633633
security_opt=security_opt
634634
)
635635

636-
def build(self, no_cache=False):
636+
def build(self, no_cache=False, pull=False):
637637
log.info('Building %s...' % self.name)
638638

639639
path = six.binary_type(self.options['build'])
@@ -644,6 +644,7 @@ def build(self, no_cache=False):
644644
stream=True,
645645
rm=True,
646646
nocache=no_cache,
647+
pull=pull,
647648
dockerfile=self.options.get('dockerfile', None),
648649
)
649650

tests/integration/cli_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ def test_build_no_cache(self, mock_stdout):
9999
output = mock_stdout.getvalue()
100100
self.assertNotIn(cache_indicator, output)
101101

102+
@patch('sys.stdout', new_callable=StringIO)
103+
def test_build_pull(self, mock_stdout):
104+
self.command.base_dir = 'tests/fixtures/simple-dockerfile'
105+
self.command.dispatch(['build', 'simple'], None)
106+
107+
mock_stdout.truncate(0)
108+
cache_indicator = 'latest: Pulling from busybox'
109+
self.command.dispatch(['build', 'simple'], None)
110+
output = mock_stdout.getvalue()
111+
self.assertNotIn(cache_indicator, output)
112+
113+
mock_stdout.truncate(0)
114+
self.command.dispatch(['build', '--pull', 'simple'], None)
115+
output = mock_stdout.getvalue()
116+
self.assertIn(cache_indicator, output)
117+
102118
def test_up(self):
103119
self.command.dispatch(['up', '-d'], None)
104120
service = self.project.get_service('simple')

0 commit comments

Comments
 (0)