$$\color{red} { Docker \ Compose }$$
🔗Home
Build in Dockerfile; Dockerfile; args; cache_from'; Labels; Shm_size
Config; Command; Target; Network; container Name; Depends On;
Deploy; Endpoint Mode
Create mysql cotainer; Create postgre cotainer; Create mongo cotainer; Create redis cotainer; Create php cotainer; Create phpmyadmin cotainer; Create adminer cotainer;
Create php_mysql cotainer; Create php_mysql_adminer cotainer; Create php_mysql_phpmyadmin cotainer; Create postgre & adminer cotainer; Create mongo & mongo-express cotainer; Create mysql & phpmyadmin cotainer; Create Mysql & adminer cotainer;
Configuration options that are applied at build time.
build can be specified either as a string containing a path to the build context:
version: "3.9" services: webapp: build: ./dir
Or, as an object with the path specified under context and optionally Dockerfile and args:
version: "3.9"
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image:
build: ./dir
image: webapp:tagcontext:
Either a path to a directory containing a Dockerfile, or a url to a git repository.
build:
context: ./dirAlternate Dockerfile.
Compose uses an alternate file to build with. A build path must also be specified.
build:
context: .
dockerfile: Dockerfile-alternateAdd build arguments, which are environment variables accessible only during the build process.
First, specify the arguments in your Dockerfile:
ARG buildno
ARG gitcommithash
RUN echo "Build number: $buildno"
RUN echo "Based on commit: $gitcommithash"Then specify the arguments under the build key. You can pass a mapping or a list:
build:
context: .
args:
buildno: 1
gitcommithash: cdc3b19
build:
context: .
args:
- buildno=1
- gitcommithash=cdc3b19args:
- buildno
- gitcommithashAdded in version 3.2 file formatA list of images that the engine uses for cache resolution.
build:
context: .
cache_from:
- alpine:latest
- corp/web_app:3.14Added in version 3.3 file formatAdd metadata to the resulting image using Docker labels. You can use either an array or a dictionary.
It’s recommended that you use reverse-DNS notation to prevent your labels from conflicting with those used by other software.
build:
context: .
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
build:
context: .
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"Added in version 3.4 file formatSet the network containers connect to for the RUN instructions during build.
build:
context: .
network: host
build:
context: .
network: custom_network_1
Use none to disable networking during build:
build:
context: .
network: noneAdded in version 3.5 file formatSet the size of the /dev/shm partition for this build’s containers. Specify as an integer value representing the number of bytes or as a string expressing a byte value.
build:
context: .
shm_size: '2gb'
build:
context: .
shm_size: 10000000Added in version 3.4 file formatBuild the specified stage as defined inside the Dockerfile. See the multi-stage build docs for details.
build:
context: .
target: prod
cap_add, cap_dropAdd or drop container capabilities. See man 7 capabilities for a full list.
cap_add:
- ALL
cap_drop:
- NET_ADMIN
- SYS_ADMINSpecify an optional parent cgroup for the container.
cgroup_parent: m-executor-abcd Override the default command.
command: bundle exec thin -p 3000
The command can also be a list, in a manner similar to dockerfile:
command: ["bundle", "exec", "thin", "-p", "3000"]Added in version 3.3 file format.
config definitions are only supported in version 3.3 and higher of the compose file format.
version: "3.9"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- my_config
- my_other_config
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true version: "3.9"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- source: my_config
target: /redis_config
uid: '103'
gid: '103'
mode: 0440
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: trueSpecify a custom container name, rather than a generated default name.
container_name: my-web-containerversion: "3.9"
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgresAdded in version 3 file format.Specify configuration related to the deployment and running of services. The following sub-options only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run, except for resources.
version: "3.9"
services:
redis:
image: redis:alpine
deploy:
replicas: 6
placement:
max_replicas_per_node: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure version: "3.9"
services:
wordpress:
image: wordpress
ports:
- "8080:80"
networks:
- overlay
deploy:
mode: replicated
replicas: 2
endpoint_mode: vip
mysql:
image: mysql
volumes:
- db-data:/var/lib/mysql/data
networks:
- overlay
deploy:
mode: replicated
replicas: 2
endpoint_mode: dnsrr
volumes:
db-data:
networks:
overlay:version: "3.9"
services:
web:
image: web
deploy:
labels:
com.example.description: "This label will appear on the web service"
version: "3.9"
services:
web:
image: web
labels:
com.example.description: "This label will appear on all containers for the web service"version: '2' services: db: image: mysql:5.7 ports: - "6603:3306" environment: - MYSQL_ALLOW_EMPTY_PASSWORD=true - MYSQL_DATABASE=laravelProject - LANG=C.UTF-8 volumes: - db:/var/lib/mysql command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION --character-set-server=utf8 --collation-server=utf8_unicode_ci
web: image: arbiedev/php-nginx:7.1.8 ports: - "8080:80" volumes: - ./www:/var/www - ./nginx.conf:/etc/nginx/sites-enabled/default
volumes: db:
web:
build: path/to/your/Dockerfile/directory
image: your-image-tag
ports:
- "8080:80"
volumes:
- ./www:/var/www
- ./nginx.conf:/etc/nginx/sites-enabled/defaultversion: "3.9" services:
redis: image: redis:alpine ports: - "6379" networks: - frontend deploy: replicas: 2 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure
db: image: postgres:9.4 volumes: - db-data:/var/lib/postgresql/data networks: - backend deploy: placement: max_replicas_per_node: 1 constraints: - "node.role==manager"
vote: image: dockersamples/examplevotingapp_vote:before ports: - "5000:80" networks: - frontend depends_on: - redis deploy: replicas: 2 update_config: parallelism: 2 restart_policy: condition: on-failure
result: image: dockersamples/examplevotingapp_result:before ports: - "5001:80" networks: - backend depends_on: - db deploy: replicas: 1 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure
worker: image: dockersamples/examplevotingapp_worker networks: - frontend - backend deploy: mode: replicated replicas: 1 labels: [APP=VOTING] restart_policy: condition: on-failure delay: 10s max_attempts: 3 window: 120s placement: constraints: - "node.role==manager"
visualizer: image: dockersamples/visualizer:stable ports: - "8080:8080" stop_grace_period: 1m30s volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: - "node.role==manager"
networks: frontend: backend:
volumes: db-data:
docker-compose.yml:
<code>
services:
db:
image: mysql:latest
container_name: mydb
restart: always
environment:
MYSQL_ROOT_PASSWORD: ajay123
</dode>
docker=compose up -d
docker-compose down
docker-compose restartdocker-compose.yml:
<code>
postgre:
image: postgres
restart: always
environment:
POSTGRES_USER: ajay
POSTGRES_PASSWORD: ajay123
</code>
docker=compose up -d
docker-compose down
docker-compose restartdocker-compose.yml:
<code>
#Use root/example as user/password credentials
version: '3.1'
services:
mongo:
image: mongo:4
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
</code>
docker=compose up -d
docker-compose down
docker-compose restartdocker-compose.yml:
docker-compose.yml:
Dockerfile:
<code>
FROM php:8.0-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y
</code>docker-compose.yml
<code>
version: "3.5"
services:
web:
build:
context: ./
dockerfile: Dockerfile
container_name: phpserver
depends_on:
- db
volumes:
- ./php/src:/var/www/html
ports:
- 8000:80
</code>index.php:
$servername = "db";
$username = "root";
$password = "example";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->query('create database ajay');
echo "Connected successfully";
?>
docker=compose up -d
docker-compose down
docker-compose restartdocker-compose.yml:
<code>
#Use postgres/example user/password credentials
version: '3.1'
services:
adminer:
image: adminer
restart: always
ports:
- 8080:8080
</code>
docker=compose up -d
docker-compose down
docker-compose restartdocker-compose.yml:
<code>
version: "3"
services:
phpmyadmin:
image: phpmyadmin
restart: always
environment:
- PMA_HOST:db
ports:
- 8080:80
</code>
docker=compose up -d
docker-compose down
docker-compose restartDirectory Structure:
project/Dockerfile project/docker-compose.yml project/php/src/index.php
docker-compose.yml:
<code>
services:
db:
image: mysql:latest
container_name: mydb
restart: always
environment:
MYSQL_ROOT_PASSWORD: ajay123
adminer:
image: adminer
restart: always
ports:
- 8080:8080
</dode>
docker-compose up -d
docker-compose down
docker exex -it mydb bash
abcd# mysql -u root -p
password: ajay123
mysql> .......
docker=compose up -d
docker-compose down
docker-compose restartDirectory Structure:
project/Dockerfile project/docker-compose.yml project/php/src/index.php
docker-compose.yml:
<code>
version: "3"
services:
db:
image: mysql:latest
container_name: mydb
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
phpmyadmin:
image: phpmyadmin
restart: always
environment:
- PMA_HOST:db
ports:
- 8080:80
</code>
docker=compose up -d
docker-compose down
docker-compose restartDirectory Structure:
project/Dockerfile project/docker-compose.yml project/php/src/index.php
<code>
version: "3.5"
services:
db:
image: mysql:latest
container_name: mydb
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
web:
build:
context: ./
dockerfile: Dockerfile
container_name: phpserver
depends_on:
- db
volumes:
- ./php/src:/var/www/html
ports:
- 8000:80
</code>//Dir php/src/index.php:
<code>
<?php
echo "php and mysql";
$servername = "db";
$username = "root";
$password = "example";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
</code>
docker=compose up -d
docker-compose down
docker-compose restartDirectory Structure:
project/Dockerfile project/docker-compose.yml project/php/src/index.php
Dockerfile:
<code>
FROM php:8.0-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y
</code>docker-compose.yml
<code>
version: "3.5"
services:
db:
image: mysql:latest
container_name: mydb
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
web:
build:
context: ./
dockerfile: Dockerfile
container_name: phpserver
depends_on:
- db
volumes:
- ./php/src:/var/www/html
ports:
- 8000:80
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: db
ports:
- 8080:80
</code>
docker=compose up -d
docker-compose down
docker-compose restartindex.php:
$servername = "db";
$username = "root";
$password = "example";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->query('create database ajay');
echo "Connected successfully";
?>
docker-compose.yml:
<code>
#Use postgres/example user/password credentials
version: '3.1'
services:
postgre:
image: postgres
restart: always
environment:
POSTGRES_USER: ajay
POSTGRES_PASSWORD: ajay123
adminer:
image: adminer
restart: always
ports:
- 8080:8080
</code>
docker=compose up -d
docker-compose down
docker-compose restart <code>
#Use root/example as user/password credentials
version: '3.1'
services:
mongo:
image: mongo:4
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
# depends_on:
# - mongo
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
</code>
docker=compose up -d
docker-compose down
docker-compose restart🔚
- Manually installing PHP, MySQL and Apache
- Preconfigured Packages such as XAMPP
- Virtual Machines and Vagrant
- Docker
nginx.conf / httpd.conf, php.ini, /bin/php72, /bin/php, open_basedir, docker, docker-compose
systemctl start docker.service and enable it with systemctl enable docker
docker-compose.yml for NGINX
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"version: '3'
services, nginx:latest
version: '3'
services:
web:
image: nginx:1.18.0
ports:
- "80:80"Running the Service
httpdocs, htdocs or public, volumes
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/nginx.conf
- ./app:/app/etc/nginx/conf.d
server {
listen 80 default_server;
root /app/public;
}docker-compose.yml
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/nginx.conf
- ./app:/app
php:
image: php:fpm
volumes:
- ./app:/appfpm, php:fpm-latest, php:7.4-fpm, php:7.3-fpm, php:8.0-fpm, php:8.0-fpm, php:fpm
nginx.conf
server {
listen 80 default_server;
root /app/public;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
} fastcgi_pass php:9000
Create a phpinfo file at app/public/index.php:
<?php
phpinfo();php:fpm
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/nginx.conf
- ./app:/app
php:
build:
context: .
dockerfile: PHP.Dockerfile
volumes:
- ./app:/appPHP.Dockerfile
FROM php:fpm
RUN docker-php-ext-install pdo pdo_mysql
FROM php:fpm
RUN docker-php-ext-install mysqlixdbug
FROM php:fpm
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install xdebug && docker-php-ext-enable xdebugMYSQL:
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/nginx.conf
- ./app:/app
php:
build:
context: .
dockerfile: PHP.Dockerfile
volumes:
- ./app:/app
mysql:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: 'secret'
MYSQL_USER: 'tutorial'
MYSQL_PASSWORD: 'secret'
MYSQL_DATABASE: 'tutorial'
volumes:
- mysqldata:/var/lib/mysql
ports:
- 3306:3306
volumes:
mysqldata: {}mariadb:latest, environment, MYSQL_ROOT_PASSWORD, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE app/public
<?php
$pdo = new PDO('mysql:dbname=tutorial;host=mysql', 'tutorial', 'secret', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$query = $pdo->query('SHOW VARIABLES like "version"');
$row = $query->fetch();
echo 'MySQL version:' . $row['Value'];🔚