Configuring Docker-Compose for Container-Based Dev Environment
Muhammad Usman
Data Engineer | dbt, AWS, Airflow, Terraform, Python, SQL | Expert in Data Preparation for Business and ML Models
Configuring Docker-Compose for Container-Based Dev Environment
When we start working on a new project, one of the preliminary tasks is figuring out which tech stacks are needed for the project and how to set up and manage different versions of those tech stacks so that the project can continue to perform as intended.
Q. So, what if we could instantly access the resources we needed for each project, whether or not we went through the installation process or installed tech stacks on local or virtual machines?
Q. What if we could just work on our projects inside an environment that supported Java or Go, and everything would function as expected? Or, in the case of a tech stack like Python, where we may have to deal with different versions, what if we could do it quickly and not have to worry about things like installing or maintaining different versions of that tech stack?
Q. What if, when we have finished configuring our environment, we can actually share that setup with others, like we could share it on GitHub? As a result, our team can start working on the same project in the same development environment while having access to all identical versions.
Q. What if we could import the infrastructure remotely on any local or virtual machine and keep running our databases?
That’s exactly what we are going to do today with the help of?Docker.
Docker?is a set of platform-as-a-service (PaaS) products that use OS-level virtualization to deliver software in packages called containers.
Today we’ll run a?Postgresql?database in?PgAdmin4?and?JupyterLab?without installing the database and IDLE locally on our machine.
What is Docker Compose:
Docker-compose helps us run and manage multiple containers with a single command. Docker-compose files written in the?YAML?language. So let’s start writing the YAML file.
Create a docker-compose.yml file in a new folder and copy the below code. We’ll go through the code in a few seconds.
version: '2.12.2'
services:
pgadmin:
image: dpage/pgadmin4:latest
container_name: pgadmin
ports:
- "80:80"
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: your_password
volumes:
- pgadmin:/var/lib/pgadmin
postgres:
image: postgres:15.1
container_name: postgresql
ports:
- "5432:5432"
environment:
POSTGRES_USER: your_username
POSTGRES_PASSWORD: your_password
POSTGRES_DB: docker_postgres_db
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- postgres:/var/lib/postgresql/data/pgdata
jupyterlab:
image: jupyter/base-notebook
container_name: jupyterlab
ports:
- "8888:8888"
environment:
- JUPYTER_ENABLE_LAB=1
volumes:
- ./notebooks/:/notebooks
command: start-notebook.sh --NotebookApp.notebook_dir=/notebooks --NotebookApp.token='' --NotebookApp.password=''
volumes:
postgres:
pgadmin:
Open PowerShell or Terminal, and go to the docker-compose.yml location.
docker-compose up
Run the “docker-compose up” command from PowerShell, and it will start all the services defined in docker-compose.yml.?Let’s go through the code.
领英推荐
The PgAdmin4 container will operate with that setting going forward. I am confident that you will also be able to comprehend the PostgreSQL configuration. Moving on to the next service, the changes in the JupyterLab configuration are the “volumes” and “command”. Please let me explain these changes.
Now, open two new tabs on your choice search engine. In one of the tabs, conduct a search for “localhost:80” and in the other, search for “localhost:8888”.
You can see the GUI of PgAdmin4 at?port 80
and JupyterLab at?port 8888.
The final step is to establish a connection between the PostgreSQL database and the PgAdmin4 server.
As you can see, the database has been successfully connected.
We’re successfully running the containers without installing JupyterLab, PgAdmin4, and PostgreSQL locally. We can share the docker-compose file and containers on GitHub. Moreover, the containers interact with each other, and we can write in the PostgreSQL database via JupyterLab and query it via PgAdmin4.
I hope this article helps you realize the power of containers and how to deploy the containers successfully.
Thank you for reading.