Description
I'd like to easily change the shm-size in a running container by using docker container update. I'm running PostgreSQL inside a container and receiving an error related to the small amount of shared memory available.
Steps to reproduce the issue:
--shm-size Describe the results you received:
I've found two ways of changing the shared memory of a running container:
Remount the shm file system of the container: mount -o remount,rw,nosuid,nodev,noexec,relatime,size=256M -t tmpfs /var/lib/docker/containers/<container-id>/mounts/shm
Change ShmSize inside /var/lib/docker/containers/<container-id>/hostconfig.json and restart container. EDIT: as noted by @dmitrygashnikov: you must stop docker service (service docker stop), then change the hostconfig.json file for this container and start docker again (service docker start).
The second option is the only one which makes the change persistent, but we need to stop all running containers to make the change.
Here is a little script to change /dev/shm (by default to 1GB), using the docker daemon restart method:
#!/bin/bash
set -e
CONTAINER_NAME="$1"
if [ -z "$CONTAINER_NAME" ]; then
echo "ERROR - Usage: $0 <container-name> [shm-size]"
exit 1
fi
if [ ! -z "$2" ]; then
SHM_SIZE="$2"
else
SHM_SIZE="1073741824"
fi
echotime() {
echo [$(date "+%Y-%m-%d %H:%M:%S")] $@
}
CONTAINER_ID=$(docker inspect -f '{{ .ID }}' $CONTAINER_NAME)
HOST_CONFIG=/var/lib/docker/containers/$CONTAINER_ID/hostconfig.json
echotime "Stopping docker service"
service docker stop
echotime "Changing container's hostconfig.json"
sed -i 's/"ShmSize":[0-9]\+,/"ShmSize":'$SHM_SIZE',/' $HOST_CONFIG
echotime "Starting docker service"
service docker start
Describe the results you expected:
It would be awesome to change this setting from the command line, using docker container update --shm-size=256M.
Output of docker version:
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:17:14 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:15:24 2018
OS/Arch: linux/amd64
Experimental: false
It's not really easily done on a running container, so it could only take effect on container restart.
Why not create a new container?
@cpuguy83 because of some reasons:
docker run command everytime I need to fix this problem.If there are use cases for changing other container configurations inside docker container update, why not add shm?
If you need persistent state, please consider using a volume for this. This would allow you to attach the same volume to a different container. It is also significantly more performant if that is at all needed.
All other options can be updated while a container is running. Mounts cannot.
@cpuguy83 , I have similar issue. I have container that I created without use of volumes unfortunately. And each time I start it I have to perform mount -o remount,size=256m /dev/shm which is annoying.
Changing start configuration of container makes sense even if effect will be during next start. Ideally docker container inspect should allow you to see both running and next start configuration.
Right now work-around is:
hostconfig.json as you wantShmSize.Note that though generic mounts changes might not be achievable with running container there still should be a way to change ShmSize since that's a managed option despite resulting in a mount. Changing that can result in something like:
# outside of container
mount -o remount,size=500m '/var/lib/docker/containers/.../mounts/shm'
These suggestions do not work for version 19.03.
After updating hostconfig.json with new value, I start the container but config returns to old value. And there is not shm in mounts dir.
These suggestions do not work for version 19.03.
After updating hostconfig.json with new value, I start the container but config returns to old value. And there is not shm in mounts dir.
Make sure, that docker service is stoped, before modify hostconfig.json.
When service reloading or something it`s rewriting all changes using running config.
Most helpful comment
@cpuguy83 , I have similar issue. I have container that I created without use of volumes unfortunately. And each time I start it I have to perform
mount -o remount,size=256m /dev/shmwhich is annoying.Changing start configuration of container makes sense even if effect will be during next start. Ideally
docker container inspectshould allow you to see both running and next start configuration.Right now work-around is:
hostconfig.jsonas you wantShmSize.Note that though generic mounts changes might not be achievable with running container there still should be a way to change
ShmSizesince that's a managed option despite resulting in a mount. Changing that can result in something like: