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
| Command | Purpose | Detail |
docker compose up | Build 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 down | Stop and Remove | Stops 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 stop | Stop Running Containers | Stops all running services gracefully. The containers still exist on the system and can be started again later. |
docker compose start | Restart Stopped Containers | Starts services that were previously stopped using docker compose stop. |
docker compose restart | Restart All Services | Stops and then starts all services defined in the configuration. Useful after changing configurations or fixing an issue. |
Build and Inspection
| Command | Purpose | Detail |
docker compose build | Rebuild Images | Executes 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 ps | View Status | Lists the containers that are currently running (or stopped) for the current Compose project, showing their status, ports, and names. |
docker compose logs | View Logs | Displays 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 config | Validate Configuration | Validates 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
| Command | Purpose | Detail |
docker compose exec | Execute a Command | Runs 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 sh | This 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:
No comments:
Post a Comment