For software development and maintenance, contact me at contact@appsoftware.com or via appsoftware.com


Editing data in a Docker volume to fix a container that shuts down immediately after exit

Fri, 17 Mar 2023 by garethbrown

Today I was faced with a problem whereby I had edited configuration in a local development Postgres Docker container in such a way that caused it to fail on start, exiting immediately.

Context

The configuration change was in postgresql.conf to test a behaviour around connection starvation.

max_connections = 100
max_connections = 3

The container then failed to start since this value was incompatible with the reserved super user number of connections.

Normally you can used docker exec to get a shell into a container and then modify data on the attached volume, but if the container won't stay up, this is a problem. My containers were running under Docker Desktop with Docker compose.

Resolution

Keeping the Container Running

To resolve I researched ways of keeping a container running on failure. The method that worked for me was to append tail -f /dev/null to the end of a docker run command (credit).

Attaching the Volume

We can find the name of our volume using docker volume ls.

We then map the volume to the correct path inside the container as we would in our Docker Compose file (name_of_volume_postgres:/var/lib/postgresql/data).

docker run -d -v name_of_volume_postgres:/var/lib/postgresql/data postgres:13.7-bullseye tail -f /dev/null 

Editing the Data

We can now use docker exec -it <container ID> /bin/bash to get a shell into the running container. There is no text editor included with the stock Postgresql image, so I used sed to make the replacement.

The postgresql.conf file can be found at /var/lib/postgresql/data/postgresql.conf this can be checked using the SHOW config_file when Postgres is running.

# sed -i 's/max_connections = 3/max_connections = 5/g' postgresql.conf

This fixed the issue and now when I start the container under Docker compose, it runs without immmediately exiting.


The information provided on this Website is for general informational and educational purposes only. While we strive to provide accurate and up-to-date information, we make no warranties or representations, express or implied, as to the accuracy, completeness, reliability, or suitability of the content, including code samples and product recommendations, presented on this Website.

The use of any information, code samples, or product recommendations on this Website is entirely at your own risk, and we shall not be held liable for any loss or damage, direct or indirect, arising from or in connection with the use of this Website or the information provided herein.
UI block loader
One moment please ...