Photo by Kelvin Ang on Unsplash

How Can We Easily and Visually Explain Docker-Compose?

Alex Duncan
Dec 12, 2020 · 6 min read

Using Compose is basically a three-step process:

Common use cases:

Development environments

Automated testing environments

Single host deployments

docker-compose.yaml

Container structure visualization

AWS EC2 instance

sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -a -G docker ec2-user

sudo curl -L
"https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose

Apply executable permissions to the binary.

sudo chmod +x /usr/local/bin/docker-compose

Check if the “Docker Compose” is working.

docker-compose --version

`6379`.

import timeimport redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)

echo '
flask
redis
' > requirements.txt

Create a Dockerfile that builds a Docker image in your project folder.

* Build an image starting with the Python 3.7 image.

* Set the working directory to `/code`.

* Set environment variables used by the flask command.

* Install gcc and other dependencies

* Copy `requirements.txt` and install the Python dependencies.

* Add metadata to the image to describe that the container is listening on port 5000

* Copy the current directory `.` in the project to the workdir `.` in the image.

* Set the default command for the container to flask run.

echo '
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
' > Dockerfile

9.Step: Create a file called “docker-compose.yml” in your project folder.

echo '
version: "3"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
' > docker-compose.yml

10. Step: Finally you will have just four yaml file that is shown below.

Enter http://”ec2-host-name”5000/ in a browser to see the application is running.Whenever you refresh your browser, the counter will increase the result number.That means that your service in the container is up and running.

Useful commands for Docker-Compose:

docker-compose version

8. Remove a specific docker container

Wrap

Clarusway

We aim to set the new frontier in IT training with its…

Clarusway

We aim to set the new frontier in IT training with its comprehensive and innovative approach in teaching while transforming individuals into IT professionals from the ground up.

Alex Duncan

Written by

Human, AWS/DevOps Expert, Writer, Reader, Researcher, Traveler…

Clarusway

We aim to set the new frontier in IT training with its comprehensive and innovative approach in teaching while transforming individuals into IT professionals from the ground up.