Skip to content

Commit 98acf61

Browse files
committed
correctly run php container from exec command if container is stopped
1 parent 8f271d2 commit 98acf61

File tree

4 files changed

+68
-24
lines changed

4 files changed

+68
-24
lines changed

build-packages/magento-scripts/lib/tasks/docker/containers/container-api.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export interface ContainerRunOptions {
6969
* Run container in background and print container ID
7070
*/
7171
detach?: boolean
72+
73+
tty?: boolean
7274
/**
7375
* Publish or expose port [docs](https://docs.docker.com/engine/reference/commandline/run/#publish-or-expose-port--p---expose)
7476
*/
@@ -126,3 +128,5 @@ export interface ContainerRunOptions {
126128
}
127129

128130
export function run(containerOptions: ContainerRunOptions, execOptions?: ExecAsyncSpawnOptions<false>): Promise<false>
131+
132+
export function runCommand(options: ContainerRunOptions): string[]

build-packages/magento-scripts/lib/tasks/docker/containers/container-api.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const { execAsyncSpawn } = require('../../../util/exec-async-command');
33

44
/**
55
* @param {import('./container-api').ContainerRunOptions} options
6-
* @param {import('../../../util/exec-async-command').ExecAsyncSpawnOptions<false>} execOptions
6+
* @returns {string[]}
77
*/
8-
const run = (options, execOptions = {}) => {
8+
const runCommand = (options) => {
99
const {
1010
addHost,
1111
ports = [],
@@ -24,32 +24,35 @@ const run = (options, execOptions = {}) => {
2424
expose = [],
2525
detach = true,
2626
rm = false,
27+
tty = false,
2728
user
2829
} = options;
2930

3031
const detachArg = detach && '-d';
3132
const rmArg = rm && '--rm';
33+
const ttyArg = tty && '-it';
3234
const exposeArg = expose && expose.map((e) => `--expose=${ e }`);
33-
const restartArg = !rm && restart && `--restart ${ restart }`;
34-
const networkArg = network && `--network ${ network }`;
35-
const portsArgs = ports.map((port) => `-p ${ port }`).join(' ');
36-
const mountsArgs = mounts.map((mount) => `--mount ${ mount }`).join(' ');
37-
const mountVolumesArgs = mountVolumes.map((mount) => `-v ${mount}`).join(' ');
38-
const envArgs = !env ? '' : Object.entries(env).map(([key, value]) => `--env ${ key }='${ value }'`).join(' ');
39-
const nameArg = name && `--name ${name}`;
40-
const entrypointArg = entrypoint && `--entrypoint "${entrypoint}"`;
41-
const healthCheckArg = healthCheck && Object.entries(healthCheck).map(([key, value]) => `--health-${key} '${value}'`).join(' ');
42-
const securityArg = securityOptions.length > 0 && securityOptions.map((opt) => `--security-opt ${opt}`).join(' ');
43-
const tmpfsArg = tmpfs.length > 0 && tmpfs.map((t) => `--tmpfs ${t}`).join(' ');
35+
const restartArg = !rm && restart && `--restart=${ restart }`;
36+
const networkArg = network && `--network=${ network }`;
37+
const portsArgs = ports && ports.length > 0 && ports.map((port) => `-p=${ port }`).join(' ');
38+
const mountsArgs = mounts && mounts.map((mount) => `--mount=${ mount }`).join(' ');
39+
const mountVolumesArgs = mountVolumes && mountVolumes.map((mount) => `-v=${mount}`).join(' ');
40+
const envArgs = !env ? '' : Object.entries(env).map(([key, value]) => `--env=${ key }='${ value }'`).join(' ');
41+
const nameArg = name && `--name=${name}`;
42+
const entrypointArg = entrypoint && `--entrypoint="${entrypoint}"`;
43+
const healthCheckArg = healthCheck && Object.entries(healthCheck).map(([key, value]) => `--health-${key}='${value}'`).join(' ');
44+
const securityArg = securityOptions.length > 0 && securityOptions.map((opt) => `--security-opt=${opt}`).join(' ');
45+
const tmpfsArg = tmpfs.length > 0 && tmpfs.map((t) => `--tmpfs=${t}`).join(' ');
4446
const userArg = user && `--user=${user}`;
4547
const addHostArg = addHost && `--add-host=${addHost}`;
4648

4749
const dockerCommand = [
4850
'docker',
4951
'run',
52+
nameArg,
53+
ttyArg,
5054
detachArg,
5155
rmArg,
52-
nameArg,
5356
networkArg,
5457
restartArg,
5558
portsArgs,
@@ -65,11 +68,17 @@ const run = (options, execOptions = {}) => {
6568
addHostArg,
6669
image,
6770
command
68-
].filter(Boolean).join(' ');
71+
].filter(Boolean).filter((arg) => typeof arg === 'string');
6972

70-
return execAsyncSpawn(dockerCommand, execOptions);
73+
return dockerCommand;
7174
};
7275

76+
/**
77+
* @param {import('./container-api').ContainerRunOptions} options
78+
* @param {import('../../../util/exec-async-command').ExecAsyncSpawnOptions<false>} execOptions
79+
*/
80+
const run = (options, execOptions = {}) => execAsyncSpawn(runCommand(options).join(' '), execOptions);
81+
7382
/**
7483
* @param {string} command
7584
* @param {string} container container id or name
@@ -148,6 +157,7 @@ const ls = async (options = {}, execOptions = {}) => {
148157

149158
module.exports = {
150159
run,
160+
runCommand,
151161
exec,
152162
ls
153163
};

build-packages/magento-scripts/lib/tasks/execute.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ const getMagentoVersionConfig = require('../config/get-magento-version-config');
55
const checkConfigurationFile = require('../config/check-configuration-file');
66
const getProjectConfiguration = require('../config/get-project-configuration');
77
const { getCachedPorts } = require('../config/get-port-config');
8-
const executeInContainer = require('../util/execute-in-container');
8+
const { executeInContainer, runInContainer } = require('../util/execute-in-container');
99
const { containerApi } = require('./docker/containers');
10-
const { runPHPContainerCommand } = require('./php/php-container');
1110
const KnownError = require('../errors/known-error');
1211

1312
/**
@@ -76,12 +75,12 @@ const executeTask = async (argv) => {
7675

7776
if (container.name.endsWith('php')) {
7877
logger.logN(`Starting container ${logger.style.misc(container._)} with command: ${logger.style.command(argv.commands.join(' '))}`);
79-
const result = await runPHPContainerCommand(
80-
ctx,
81-
argv.commands.join(' '),
78+
const result = await runInContainer(
8279
{
83-
logOutput: true
84-
}
80+
...container,
81+
name: `${container.name}_exec-${Date.now()}`
82+
},
83+
argv.commands
8584
);
8685

8786
return result;

build-packages/magento-scripts/lib/util/execute-in-container.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const os = require('os');
22
const { spawn } = require('child_process');
3+
const { runCommand } = require('../tasks/docker/containers/container-api');
34

45
/**
56
* @param {{ containerName: string, commands: string[] }} param0
@@ -29,4 +30,34 @@ const executeInContainer = ({ containerName, commands }) => {
2930
});
3031
};
3132

32-
module.exports = executeInContainer;
33+
/**
34+
* @param {import('../tasks/docker/containers/container-api').ContainerRunOptions} options
35+
* @param {string[]} commands
36+
*/
37+
const runInContainer = (options, commands) => {
38+
if (!process.stdin.isTTY) {
39+
process.stderr.write('This app works only in TTY mode');
40+
process.exit(1);
41+
}
42+
43+
const runArgs = runCommand({
44+
...options,
45+
tty: true,
46+
detach: false,
47+
rm: true,
48+
command: commands.map((command) => command.split(' ')).flat().join(' ')
49+
});
50+
51+
const args = runArgs.slice(1).map((command) => command.split(' ')).flat();
52+
53+
spawn('docker', args, { stdio: [0, 1, 2] });
54+
55+
return new Promise((_resolve) => {
56+
// never resolve
57+
});
58+
};
59+
60+
module.exports = {
61+
executeInContainer,
62+
runInContainer
63+
};

0 commit comments

Comments
 (0)