 	.tap ->
 		utils.mixpanelTrack('Application stop', app.imageId)
 
+isValidPort = (port) ->
+	maybePort = parseInt(port, 10)
+	return parseFloat(port) is maybePort and maybePort > 0 and maybePort < 65535
+
 exports.start = start = (app) ->
 	docker.getImage(app.imageId).inspectAsync()
 	.catch (error) ->

		.then (container) ->
			console.log('Starting container:', app.imageId)
			ports = {}
			if port?
				ports[port + '/tcp'] = [ HostPort: String(port) ]
			container.startAsync(
				Privileged: true
				PortBindings: ports

		# Parse the env vars before trying to access them, that's because they have to be stringified for knex..
		env = JSON.parse(app.env)
		if env.PORT?
			portList = env.PORT
			.split(',')
			.map((port) -> port.trim())
			.filter(isValidPort)

			portList.forEach (port) ->
				ports[port + '/tcp'] = {}

		docker.createContainerAsync(
			Image: app.imageId
			Cmd: ['/bin/bash', '-c', '/start']
