Overview
In our previous tutorial, we learned about docker and basic commands, In this tutorial, we will set up the docker-compose file to ease our task. So that we don't need to write in the command line every time.
we will set up Postgres and Pgadmin by using docker-compose but Before jumping into docker-compose. We will try to run Postgres and pgadmin images in our command line.
Run Postgres and pgadmin in the command line
To run Postgres and pgadmin we need to create a docker network.
docker network create postgres-network
Here postgres-network is the network name. You can give any name you want.
we can run our Postgres
docker run -p 5432:5432 -d -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password --name postgres-db --net postgres-network postgres
This is a large command. Let's break it into multiple lines. So that we can understand the command
docker run -d \
-p 5432:5432 \
-e POSTGRES_USER=user \
-e POSTGRES_PASSWORD=password \
--name postgres-db \
--net postgres-network \
postgres
Let's now get deep into this command.
docker run -d
- It will check the image in our local and run the image. If an image is not found it will pull and run. Also, run on detach mode by adding -d-p 5432:5432
- Here we are binding our docker port to our local port.-e POSTGRES_USER=user
- Setting environment variable by adding -e and adding POSTGRES_USER name is user-e POSTGRES_PASSWORD=password
- Another environment variable added is POSTGRES_PASSWORD is password--name postgres-db
- Giving container name postgres-db--net postgres-network
- Adding this container to our previously created network called postgres-networkpostgres
- last giving the image name we want to work with.
Run this command It will create a docker container and run it.
Now, We need the pgadmin. Let's run it.
docker run -d -p 8888:80 -e PGADMIN_DEFAULT_EMAIL=some@email.com -e PGADMIN_DEFAULT_PASSWORD=password --name pgadmin --net postgres-network dpage/pgadmin4
or
docker run -d \
-p 8888:80 \
-e PGADMIN_DEFAULT_EMAIL=some@email.com \
-e PGADMIN_DEFAULT_PASSWORD=password \
--name pgadmin \
--net postgres-network \
dpage/pgadmin4
Same as before adding port, environment, name, network and image.
Run this command it will create a docker container for Pgadmin.
Now. got to http://localhost:8888/ you see the pgadmin interface running.
But every time running all of these commands is very awful and not productive. Here the docker compose comes into the picture.
What is docker-compose?
Docker Compose is a tool that helps you define and run multi-container Docker applications. With Compose, you can create a YAML file to define the services and with a single command, you can spin everything up or tear it all down.
Simplified configuration: Docker Compose makes it easy to define and configure multi-container applications.
Efficient development: Docker Compose can help you develop and deploy applications more quickly and easily.
Consistent environments: Docker Compose ensures that your applications will run in the same way in development, testing, and production.
Reproducible deployments: Docker Compose makes it easy to reproduce deployments of your applications.
Run Postgres and pgadmin by docker-compose
version: "3"
services:
postgres-db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
pgadmin:
image: dpage/pgadmin4
restart: always
ports:
- "8888:80"
environment:
PGADMIN_DEFAULT_EMAIL: some@email.com
PGADMIN_DEFAULT_PASSWORD: password
Copy this file and save it as docker-compose.yaml
.
This file represents all the previous commands, we had executed in the terminal to run our postgres.
Version
- is the docker-compose version.services
- Under services, we need to define all the run commands.postgres-db
- Name of our container.
Other conventions are very clear along with the name. We don't need network command as docker-compose create an isolated network for all the running container.
We can run our Postgres server
docker-compose up -d
Also we can stop our container
docker-compose down
It is as simple as that.