Skip to content

Commit 7f1052f

Browse files
Merge pull request #1187 from kirtangajjar/global-docker-compose
Move global containers to docker-compose
2 parents f08038d + dd2b556 commit 7f1052f

File tree

4 files changed

+118
-37
lines changed

4 files changed

+118
-37
lines changed

php/class-ee-docker.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ public static function docker_compose_up( $dir, $services = [] ) {
171171
return false;
172172
}
173173

174+
/**
175+
* Function to check if a network exists
176+
*
177+
* @param string $network Name/ID of network to check
178+
*
179+
* @return bool Network exists or not
180+
*/
181+
public static function docker_network_exists( string $network ) {
182+
return EE::exec( "docker network inspect $network" );
183+
}
184+
174185
/**
175186
* Function to destroy the containers.
176187
*

php/class-ee-site.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,6 @@ protected function delete_site( $level, $site_name, $site_root ) {
149149
EE::warning( 'Error in removing docker containers.' );
150150
}
151151
}
152-
153-
EE::docker()::disconnect_site_network_from( $site_name, $proxy_type );
154-
}
155-
156-
if ( $level >= 2 ) {
157-
if ( EE::docker()::rm_network( $site_name ) ) {
158-
EE::log( "[$site_name] Docker container removed from network $proxy_type." );
159-
} else {
160-
if ( $level > 2 ) {
161-
EE::warning( "Error in removing Docker container from network $proxy_type" );
162-
}
163-
}
164152
}
165153

166154
if ( $this->fs->exists( $site_root ) ) {

php/site-utils.php

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,20 @@ function init_checks() {
8686
if ( ! ( $port_80_status && $port_443_status ) ) {
8787
EE::error( 'Cannot create/start proxy container. Please make sure port 80 and 443 are free.' );
8888
} else {
89-
$EE_CONF_ROOT = EE_CONF_ROOT;
90-
$img_versions = EE\Utils\get_image_versions();
91-
$ee_proxy_command = "docker run --name $proxy_type -e LOCAL_USER_ID=`id -u` -e LOCAL_GROUP_ID=`id -g` --restart=always -d -p 80:80 -p 443:443 -v $EE_CONF_ROOT/nginx/certs:/etc/nginx/certs -v $EE_CONF_ROOT/nginx/dhparam:/etc/nginx/dhparam -v $EE_CONF_ROOT/nginx/conf.d:/etc/nginx/conf.d -v $EE_CONF_ROOT/nginx/htpasswd:/etc/nginx/htpasswd -v $EE_CONF_ROOT/nginx/vhost.d:/etc/nginx/vhost.d -v /var/run/docker.sock:/tmp/docker.sock:ro -v $EE_CONF_ROOT:/app/ee4 -v /usr/share/nginx/html easyengine/nginx-proxy:" . $img_versions['easyengine/nginx-proxy'];
9289

90+
$fs = new Filesystem();
9391

94-
if ( EE::docker()::boot_container( $proxy_type, $ee_proxy_command ) ) {
95-
$fs = new Filesystem();
92+
if ( ! $fs->exists( EE_CONF_ROOT . '/docker-compose.yml' ) ) {
93+
generate_global_docker_compose_yml( $fs );
94+
}
95+
96+
$EE_CONF_ROOT = EE_CONF_ROOT;
97+
if ( ! EE::docker()::docker_network_exists( 'ee-global-network' ) ) {
98+
if ( ! EE::docker()::create_network( 'ee-global-network' ) ) {
99+
EE::error( 'Unable to create network ee-global-network' );
100+
}
101+
}
102+
if ( EE::docker()::docker_compose_up( EE_CONF_ROOT, [ 'nginx-proxy' ] ) ) {
96103
$fs->dumpFile( "$EE_CONF_ROOT/nginx/conf.d/custom.conf", file_get_contents( EE_ROOT . '/templates/custom.conf.mustache' ) );
97104
EE::success( "$proxy_type container is up." );
98105
} else {
@@ -102,6 +109,47 @@ function init_checks() {
102109
}
103110
}
104111

112+
/**
113+
* Generates global docker-compose.yml at EE_CONF_ROOT
114+
*
115+
* @param Filesystem $fs Filesystem object to write file
116+
*/
117+
function generate_global_docker_compose_yml( Filesystem $fs ) {
118+
$img_versions = EE\Utils\get_image_versions();
119+
120+
$data = [
121+
'services' => [
122+
'name' => 'nginx-proxy',
123+
'container_name' => 'ee-nginx-proxy',
124+
'image' => 'easyengine/nginx-proxy:' . $img_versions['easyengine/nginx-proxy'],
125+
'restart' => 'always',
126+
'ports' => [
127+
'80:80',
128+
'443:443',
129+
],
130+
'environment' => [
131+
'LOCAL_USER_ID=' . posix_geteuid(),
132+
'LOCAL_GROUP_ID=' . posix_getegid(),
133+
],
134+
'volumes' => [
135+
EE_CONF_ROOT . '/nginx/certs:/etc/nginx/certs',
136+
EE_CONF_ROOT . '/nginx/dhparam:/etc/nginx/dhparam',
137+
EE_CONF_ROOT . '/nginx/conf.d:/etc/nginx/conf.d',
138+
EE_CONF_ROOT . '/nginx/htpasswd:/etc/nginx/htpasswd',
139+
EE_CONF_ROOT . '/nginx/vhost.d:/etc/nginx/vhost.d',
140+
'/usr/share/nginx/html',
141+
'/var/run/docker.sock:/tmp/docker.sock:ro',
142+
],
143+
'networks' => [
144+
'global-network',
145+
],
146+
],
147+
];
148+
149+
$contents = EE\Utils\mustache_render( EE_ROOT . '/templates/global_docker_compose.yml.mustache', $data );
150+
$fs->dumpFile( EE_CONF_ROOT . '/docker-compose.yml', $contents );
151+
}
152+
105153
/**
106154
* Creates site root directory if does not exist.
107155
* Throws error if it does exist.
@@ -123,26 +171,6 @@ function create_site_root( $site_root, $site_name ) {
123171
$fs->chown( $site_root, $terminal_username );
124172
}
125173

126-
/**
127-
* Function to setup site network.
128-
*
129-
* @param string $site_name Name of the site.
130-
*
131-
* @throws \Exception when network start fails.
132-
*/
133-
function setup_site_network( $site_name ) {
134-
135-
$proxy_type = EE_PROXY_TYPE;
136-
if ( EE::docker()::create_network( $site_name ) ) {
137-
EE::success( 'Network started.' );
138-
} else {
139-
throw new \Exception( 'There was some error in starting the network.' );
140-
}
141-
142-
EE::docker()::connect_site_network_to( $site_name, $proxy_type );
143-
144-
}
145-
146174
/**
147175
* Reloads configuration of ee-nginx-proxy container
148176
*
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
version: '3'
2+
3+
services:
4+
5+
{{#services}}
6+
{{name}}:
7+
container_name: {{container_name}}
8+
image: {{image}}
9+
{{#ports.0}}
10+
ports:
11+
{{#ports}}
12+
- "{{.}}"
13+
{{/ports}}
14+
{{/ports.0}}
15+
{{#depends_on}}
16+
depends_on:
17+
- {{.}}
18+
{{/depends_on}}
19+
{{#restart}}
20+
restart: {{.}}
21+
{{/restart}}
22+
{{#command}}
23+
command: {{.}}
24+
{{/command}}
25+
{{#labels.0}}
26+
labels:
27+
{{#labels}}
28+
- "{{.}}"
29+
{{/labels}}
30+
{{/labels.0}}
31+
{{#volumes.0}}
32+
volumes:
33+
{{#volumes}}
34+
- "{{.}}"
35+
{{/volumes}}
36+
{{/volumes.0}}
37+
{{#environment.0}}
38+
environment:
39+
{{#environment}}
40+
- {{.}}
41+
{{/environment}}
42+
{{/environment.0}}
43+
{{#networks.0}}
44+
networks:
45+
{{#networks}}
46+
- {{.}}
47+
{{/networks}}
48+
{{/networks.0}}
49+
{{/services}}
50+
51+
networks:
52+
global-network:
53+
external:
54+
name: ee-global-network

0 commit comments

Comments
 (0)