If you are new to database management with Docker, this tutorial will guide you through setting up both PgAdmin and Adminer using Docker containers. By containerising these tools, you can quickly launch lightweight and portable database management environments without installing them directly on your operating system. This approach also makes it easier to manage configurations, updates, and multiple projects across different devices.
Prerequisite:This tutorial is part of the Docker Series.
📚 View the Complete Docker Series
- No Manual Installation,
- Keeps My System Clean,
- Easy Portability,
- Faster Setup for Development
- Easier Updates,
- Better for Learning and Testing and
- Works Well with Database Containers
YAML file helps to define and manage multiple Docker containers in one place, and in my case, it contains two services: one is PostgreSQL, and the other is PgAdmin, as shown below:name: PostgreSQLservices:postgres:image: postgres:16container_name: postgres_dbrestart: alwaysenvironment:POSTGRES_USER: adminPOSTGRES_PASSWORD: secretPOSTGRES_DB: mydbports:- "5432:5432"pgadmin:image: dpage/pgadmin4:latestcontainer_name: pgadminrestart: alwaysenvironment:PGADMIN_DEFAULT_EMAIL: admin@example.comPGADMIN_DEFAULT_PASSWORD: admin123ports:- "8080:80"
# Use an official Python runtime as a parent imageFROM python:3.11-slim# Set the working directory in the containerWORKDIR /app# Install system dependencies required for building psycopg2 (if using non-binary)RUN apt-get update && apt-get install -y \libpq-dev \gcc \&& rm -rf /var/lib/apt/lists/*# Copy the requirements file into the containerCOPY requirements.txt .# Install the Python dependenciesRUN pip install --no-cache-dir -r requirements.txt# Copy the rest of your application codeCOPY . .# Command to run your application (adjust 'app.py' to your entry file)CMD ["python", "app.py"]
- FROM python:3.11-slim, it is optimised to be very small, containing only the minimal packages needed to run Python. This saves space and improves security.
- WORKDIR /app creates a folder named
/appinside the container and sets it as the "home base.", RUN apt-get update && apt-get install -y ...: This updates the list of available software and installs,- && rm -rf /var/lib/apt/lists/*, cleans up temporary files created during the installation, keeping the final Docker image size as small as possible.
- RUN pip install --no-cache-dir -r requirements.txt: This installs all the libraries your app needs. not to store the downloaded installer files, which you don't need once the packages are installed.
psycopg2-binarysqlalchemy
# Access the project directorycd docker_postgre_pgadmin# Pull and download the required container imagesdocker compose pull# Create and start the containersdocker compose up
http://localhost:8080/
docker compose down🎁 Get Your FREE Flask Cheat Sheet
Get more Flask, Python automation, Docker, and HTMX tutorials delivered to your inbox.
✓ Practical coding tutorials
✓ Automation tips for SMEs
✓ New project ideas and templates
Download my FREE Flask Cheat Sheet (PDF)
name: MySQL
services:
db:
image: mysql:8.0
container_name: mysql_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: mypass
ports:
- "3307:3306" # Host port changed to 3307
volumes:
- db_data:/var/lib/mysql
adminer:
image: adminer
container_name: adminer_ui
restart: always
ports:
- "9000:8080" # Adminer on port 9000
environment:
- ADMINER_DEFAULT_SERVER=db
depends_on:
- db
volumes:
db_data:- Host Port (9000): This is the port you use in your web browser to access the Adminer interface. You should navigate to http://localhost:9000.
- Container Port (8080): This is the internal port that the Adminer container is listening on inside its own environment.
# Use an official Python runtime as a parent image
FROM python:3.11-slim
# Set the working directory in the container
WORKDIR /app
# Install system dependencies for MySQL client and building C extensions
RUN apt-get update && apt-get install -y \
default-libmysqlclient-dev \
build-essential \
pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Copy the requirements file into the container
COPY requirements.txt .
# Install the Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of your application code
COPY . .
# Command to run your application
CMD ["python", "app.py"]The Dockerfile is similar to PostgreSQL, except that the MySQL installation is different: - default-libmysqlclient-dev (or libmariadb-dev): install mysqlclient. Without this, the installation would fail because it cannot find the necessary "hooks" to communicate with the MySQL server.
- build-essential: A package that installs a bundle of necessary tools for compiling software.
- pkg-config: A helper tool that makes it easier for the installer to find where the MySQL libraries are located on the system.
- && rm -rf /var/lib/apt/lists/*: By deleting this folder, you significantly reduce the size of your final Docker image, making it faster to upload and download.
The requirements.txt
mysqlclient
sqlalchemy# Access the project directorycd docker_postgre_pgadmin# Pull and download the required container imagesdocker compose pull# Create and start the containersdocker compose up
http://localhost:9000/- Amazon Web Services (AWS),
- Google Cloud Platform (GCP),
- Microsoft Azure
About the Author
Kelvin Loh is a Python developer focused on Flask, desktop applications, and business automation solutions. He shares practical tutorials and real-world coding projects to help developers and small businesses build useful applications.
.png)








Comments