Deploy an App on Docker Containers: DeVops tickets/Real Job Scenario

GABRIEL OKOM
4 min readOct 10, 2023

--

Deploying a Containerized Stack with Docker Compose: A Step-by-Step Guide

Introduction

In this guide, we’ll walk you through deploying a containerized stack on App Server 3 using Docker Compose. We’ll set up two services: a web server and a database.

Prerequisites

Before we begin, ensure you have the following prerequisites in place:

  1. You should have SSH access
  2. Docker and Docker Compose: Docker and Docker Compose should be installed on App Server 3. If not, you can follow the official installation instructions for Docker and Docker Compose.

Objectives

  1. Create a Docker Compose file for two services: a web server and a database.
  2. Map ports and volumes correctly.
  3. Set up a secure MySQL database with a custom user and password.
  4. Deploy the containers using Docker Compose.
  5. Access the web application.

Job Scenario:

The Nautilus Application development team recently finished development of one of the apps that they want to deploy on a containerized platform. The Nautilus Application development and DevOps teams met to discuss some of the basic pre-requisites and requirements to complete the deployment. The team wants to test the deployment on one of the app servers before going live and set up a complete containerized stack using a docker compose fie. Below are the details of the task:

On App Server 3 in Stratos Datacenter create a docker compose file /opt/finance/docker-compose.yml (should be named exactly).

The compose should deploy two services (web and DB), and each service should deploy a container as per details below:

For web service:

a. Container name must be php_host.

b. Use image php with any apache tag. Check here for more details.

c. Map php_host container’s port 80 with host port 6000

d. Map php_host container’s /var/www/html volume with host volume /var/www/html.

For DB service:

a. Container name must be mysql_host.

b. Use image mariadb with any tag (preferably latest). Check here for more details.

c. Map mysql_host container’s port 3306 with host port 3306

d. Map mysql_host container’s /var/lib/mysql volume with host volume /var/lib/mysql.

e. Set MYSQL_DATABASE=database_host and use any custom user ( except root ) with some complex password for DB connections.

After running docker-compose up you can access the app with curl command curl <server-ip or hostname>:6000/

Note: this is a fictional app server.

Step-by-Step Guide

1. SSH into App Server 3

Use an SSH client to connect to App Server 3. You should have the necessary credentials for access.

ssh banner@172.16.238.12

2. Create a Directory for Docker Compose

If the directory doesn’t exist, create it to store the Docker Compose file:

mkdir -p /opt/finance

3. Create the Docker Compose File

Use a text editor (e.g., vi or nano) to create the Docker Compose file. In this example, we'll use vi:

vi /opt/finance/docker-compose.yml

Paste or configure the following YAML content into the file:

version: '3'
services:
php_host:
container_name: php_host
image: php:apache
ports:
- "6000:80"
volumes:
- /var/www/html:/var/www/html

mysql_host:
container_name: mysql_host
image: mariadb:latest
ports:
- "3306:3306"
volumes:
- /var/lib/mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: database_host
MYSQL_USER: mysql-user
MYSQL_PASSWORD: db1234

Save and exit the text editor.

4. Start the Containers

Launch the containers in the background using Docker Compose:

docker-compose -f /opt/finance/docker-compose.yml up -d

5. Access the Web Application

Once the containers are up and running, you can access the web service using curl.

curl http://172.16.238.12:6000/

or

curl http://localhost:6000/

You should now be able to access the web application.

Conclusively;

Use the docker ps command to list the running containers. Look for the container running MariaDB (the DB service). You can identify it by its container name, or by other details you may have set.

docker ps

This command will list all running containers, and you should see the MariaDB container listed.

To get more detailed information about the MariaDB container, use the docker inspect command with the name of the container or the ID. In this case I will be using the name of the container.

docker inspect mysql_host

In the output of the docker inspect command, check the State section. Specifically, look at the Status field to confirm if the container is running.

  • If the Status field shows "Status": "running", it means the container is currently running.
  • If the Status field shows "Status": "exited", it means the container is not running. You may need to start it if it's not running.

Example:

A mysql_host with a running status

Security Tips

To enhance security while working with containerized applications, consider the following tips:

  1. Keep Images Updated: Regularly update your container images to apply security patches and fixes.
  2. Use Strong Passwords: When setting up databases, use strong and unique passwords to protect sensitive data.
  3. Network Isolation: Configure your containers and networks to limit exposure to the external world. Use Docker network modes effectively.
  4. Regular Backups: Ensure that critical data, such as database contents, is regularly backed up.
  5. Access Control: Apply access controls and permissions to restrict who can create, modify, or access containers and service.

Ref:

Kodekloud

#kodekloud #devops #devopstickets #devopsengineer #secops #devsecops #kubernetes #docker #containers mysql #php #apache #realjobscenario #devopsjobs

--

--

GABRIEL OKOM
GABRIEL OKOM

Written by GABRIEL OKOM

MSc Cyber Security and Computer Forensics | Certified DevOps Engineer

Responses (1)