What Is Docker Compose
What Is Docker Compose
Single host deployment - This means you can run everything on a single piece of
hardware
High productivity - Docker Compose reduces the time it takes to perform tasks
Security - All the containers are isolated from each other, reducing the threat landscape
Now, you might be thinking that Docker Compose is quite similar to Docker Swarm,
but that’s not the case. Here are some of the differences between Docker Compose
and Docker Swarm:
Basic Commands in Docker Compose
Pre-requisite:
Tested Infrastructure
Platform Number of Instance Read
Pre-requisite
Create an account with DockerHub
Open PWD Platform on your browser
Click on Add New Instance on the left side of the screen
to bring up Alpine OS instance on the right side
Setup environment:
$ mkdir Myapp
$ cd Myapp
Now lets create passowrd file for our DB:
$ openssl rand -base64 32 > db_password.txt
$ openssl rand -base64 32 > db_root_password.txt
Create a docker-compose.yml file:
version: '3.1'
services:
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
ports:
- "80:80"
- "443:443"
#Mysql DB
db:
image: mysql:5.7
container_name: Mysqldb
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_root_password
- db_password
secrets:
db_password:
file: db_password.txt
db_root_password:
file: db_root_password.txt
volumes:
db_data:
Create the compose container:
$ sudo docker-compose up
$ sudo docker-compose up -d
SSH into the instance and check the app
List out the compose services:
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
--------
Mysqldb docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp,
33060/tcp
webserver nginx -g daemon off; Up 0.0.0.0:443->443/tcp,
0.0.0.0:80->80/tcp