Get started with Docker-Compose
By Building and hosting a full stack application
Docker-compose is your tool of choice when you want to run more than one Docker container at a time, like in a full-stack application with a backend, frontend, and DB. In this blog post, we'll explore how to use Docker Compose to run a web application consisting of a frontend application built with React, a backend API built with Python Flask, and a PostgreSQL database.
The Docker Compose File
The first step is to create a Docker Compose file that defines the services for the application. Create a file called docker-compose.yml in the root directory of your project and add the following services:
In this configuration, we define three services: frontend
, backend
, and db
. The frontend
and backend
services are built using Dockerfiles in the ./frontend
and ./backend
directories, respectively. The db
service uses the latest version of the PostgreSQL image.
Run the Application
To run docker-compose application, navigate to the root directory of your project and run the following command:
This command will start all three services and build any necessary Docker images. The --build
flag ensures that Docker Compose builds new images if necessary.
You can also build the application using
Using the Application Once the application is running, you can access the frontend application by visiting http://localhost:3000
in your web browser. The backend API is available at http://localhost:5000/api
.
Connecting to the Database The PostgreSQL database can be accessed using a PostgreSQL client or any PostgreSQL-compatible ORM. To connect to the database, use the following connection string:
This string assumes you have defined the environment variables for the PostgreSQL username, password, and database name in your Docker Compose file.
That was a quick walkthrough to set up your app with docker-compose
Last updated