Friday, 14 November 2025

Docker - Compose - lets play with it

In my previous demonstration, I've created docker compose using python flask and postgres database.

Lets play with it.

docker ps


 

docker compose ps







Lifecycle Management (Start/Stop/Run) command

CommandPurposeDetail
docker compose upBuild and Run (The Main Command)Builds any required images (e.g., your web app), creates a network, and starts all the services defined in your YAML file. It brings the entire application "up."
docker compose up -d(Common variant)Same as up, but runs the containers in detached mode (in the background) so you can get your terminal prompt back.
docker compose downStop and RemoveStops the running containers, removes the containers themselves, and removes the network that was created. (It usually preserves named volumes like postgres_data by default).
docker compose stopStop Running ContainersStops all running services gracefully. The containers still exist on the system and can be started again later.
docker compose startRestart Stopped ContainersStarts services that were previously stopped using docker compose stop.
docker compose restartRestart All ServicesStops and then starts all services defined in the configuration. Useful after changing configurations or fixing an issue.

Build and Inspection


CommandPurposeDetail
docker compose buildRebuild ImagesExecutes the build instructions in your YAML file (e.g., runs the Dockerfile for your web service). It must be run manually if you change your code or Dockerfile and want the changes reflected.
docker compose psView StatusLists the containers that are currently running (or stopped) for the current Compose project, showing their status, ports, and names.
docker compose logsView LogsDisplays the real-time or aggregated output logs from all services in your application stack. You can also target specific services (e.g., docker compose logs db).
docker compose configValidate ConfigurationValidates the compose.yml file and prints the resulting configuration after substituting environment variables and merging defaults. Useful for debugging configuration issues.

Execution and Shell Access


CommandPurposeDetail
docker compose execExecute a CommandRuns a command inside a running service container. This is primarily used to get an interactive shell or run debugging tools within a container.
Example: docker compose exec web shThis would open a shell (sh) inside your web application container.
Lets' verify few commands

1. Stop and Start

[root@devopsvm01 DockerCompose]# docker compose stop
[+] Stopping 2/2
 ✔ Container flask-app    Stopped                                                                                                                                                                                                      10.4s
 ✔ Container postgres-db  Stopped                                                                                                                                                                                                       0.4s
[root@devopsvm01 DockerCompose]#
[root@devopsvm01 DockerCompose]# docker compose ps
NAME      IMAGE     COMMAND   SERVICE   CREATED   STATUS    PORTS
[root@devopsvm01 DockerCompose]#
[root@devopsvm01 DockerCompose]# docker compose start
[+] Running 2/2
 ✔ Container postgres-db  Started                                                                                                                                                                                                       0.5s
 ✔ Container flask-app    Started                                                                                                                                                                                                       0.6s
[root@devopsvm01 DockerCompose]#
[root@devopsvm01 DockerCompose]# docker compose ps
NAME          IMAGE               COMMAND                  SERVICE   CREATED       STATUS         PORTS
flask-app     dockercompose-web   "python app.py"          web       3 hours ago   Up 3 seconds   0.0.0.0:5000->5000/tcp, [::]:5000->5000/tcp
postgres-db   postgres:15         "docker-entrypoint.s…"   db        3 hours ago   Up 3 seconds   0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp
[root@devopsvm01 DockerCompose]#

2. Verify the log.

[root@devopsvm01 DockerCompose]# docker compose logs -f --tail=10 web
flask-app  |  * Running on http://127.0.0.1:5000
flask-app  |  * Running on http://172.18.0.3:5000
flask-app  | Press CTRL+C to quit
flask-app  |  * Serving Flask app 'app'
flask-app  |  * Debug mode: off
flask-app  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
flask-app  |  * Running on all addresses (0.0.0.0)
flask-app  |  * Running on http://127.0.0.1:5000
flask-app  |  * Running on http://172.18.0.3:5000

[root@devopsvm01 DockerCompose]# docker compose logs -f --tail=10 db
postgres-db  | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres-db  |
postgres-db  | 2025-11-03 16:34:26.754 UTC [1] LOG:  starting PostgreSQL 15.15 (Debian 15.15-1.pgdg13+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
postgres-db  | 2025-11-03 16:34:26.756 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres-db  | 2025-11-03 16:34:26.756 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres-db  | 2025-11-03 16:34:26.759 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-db  | 2025-11-03 16:34:26.766 UTC [28] LOG:  database system was shut down at 2025-11-03 16:34:16 UTC
postgres-db  | 2025-11-03 16:34:26.779 UTC [1] LOG:  database system is ready to accept connections
postgres-db  | 2025-11-03 16:39:26.865 UTC [26] LOG:  checkpoint starting: time
postgres-db  | 2025-11-03 16:39:26.875 UTC [26] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.003 s, total=0.010 s; sync files=2, longest=0.002 s, average=0.002 s; distance=0 kB, estimate=0 kB
^C

3. docker compose config
is used to debug and verify  your service definitions before you deploy them.


4. docker compose exec
is used to run a command inside a running container of a Docker Compose service.

[root@devopsvm01 DockerCompose]# docker compose exec db bash
root@d0e35d5e0907:/#

postgres@d0e35d5e0907:~$ psql -U myuser -d mydatabase
psql (15.15 (Debian 15.15-1.pgdg13+1))
Type "help" for help.

mydatabase=# 

5. docker compose build
docker compose build is used to build Docker images for services defined in your docker-compose.yml that have a build: section.

[root@devopsvm01 app]# cat app.py |grep -i return
    return f"PostgreSQL version: {version[0]}"
[root@devopsvm01 app]#
[root@devopsvm01 app]# vi app.py
[root@devopsvm01 app]#
[root@devopsvm01 app]# cat app.py |grep -i return
return f"PostgreSQL version: {version[0]}<br>Welcome to my new Flask app!"
[root@devopsvm01 app]#

Before build, lets access the web page:











Rebuild the image for web:

docker compose build web

optional, you can rebuild all services if needed by omitting the service name:
docker compose build

[root@devopsvm01 app]# docker compose build web [+] Building 15.2s (13/13) FINISHED => [internal] load local bake definitions 0.0s => => reading from stdin 499B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 390B 0.0s => [internal] load metadata for docker.io/library/python:3.11-slim 2.3s => [auth] library/python:pull token for registry-1.docker.io 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/5] FROM docker.io/library/python:3.11-slim@sha256:e4676722fba839e2e5cdb844a52262b43e90e56dbd55b7ad953ee3615ad7534f 0.0s => [internal] load build context 0.0s => => transferring context: 689B 0.0s => CACHED [2/5] WORKDIR /app 0.0s => [3/5] COPY app.py /app/ 0.1s => [4/5] COPY requirements.txt /app/ 0.1s => [5/5] RUN pip install --no-cache-dir -r requirements.txt 9.7s => exporting to image 2.6s => => exporting layers 2.6s => => writing image sha256:f2a049317bd2aa2f80c5e64bbbbf0c35268c71af85c2c6f0a3d4c201ff1f88f5 0.0s => => naming to docker.io/library/dockercompose-web 0.0s => resolving provenance for metadata file 0.0s [+] Building 1/1 ✔ dockercompose-web Built 0.0s [root@devopsvm01 app]# Restart containers with new image

Compose will use the newly built image for web and db will remain unchanged.

docker compose up

[root@devopsvm01 DockerCompose]# docker compose up [+] Running 1/1 ✔ Container flask-app Recreated 0.1s Attaching to flask-app, postgres-db postgres-db | postgres-db | PostgreSQL Database directory appears to contain a database; Skipping initialization postgres-db | postgres-db | 2025-11-03 17:36:25.349 UTC [1] LOG: starting PostgreSQL 15.15 (Debian 15.15-1.pgdg13+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit postgres-db | 2025-11-03 17:36:25.353 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 postgres-db | 2025-11-03 17:36:25.356 UTC [1] LOG: listening on IPv6 address "::", port 5432 postgres-db | 2025-11-03 17:36:25.367 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" postgres-db | 2025-11-03 17:36:25.387 UTC [27] LOG: database system was shut down at 2025-11-03 17:33:15 UTC postgres-db | 2025-11-03 17:36:25.426 UTC [1] LOG: database system is ready to accept connections flask-app | * Serving Flask app 'app' flask-app | * Debug mode: off flask-app | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. flask-app | * Running on all addresses (0.0.0.0) flask-app | * Running on http://127.0.0.1:5000 flask-app | * Running on http://172.18.0.3:5000 flask-app | Press CTRL+C to quit
New page







No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...