Memory allocation on containers

I am trying to create a script that assign memory automatically to a container. The concept is that i want to create a container and memory is assigned to it but only what is required, can anyone help ?