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 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:
backendservices are built using Dockerfiles in the
./backenddirectories, respectively. The
dbservice uses the latest version of the PostgreSQL image.
To run docker-compose application, navigate to the root directory of your project and run the following command:
docker-compose up --build
This command will start all three services and build any necessary Docker images. The
--buildflag 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:3000in your web browser. The backend API is available at
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