Can't create user in dockerized mongoDB

Hi I’m new to mongo and I’m trying to create new user by following the documentation. So I am using this command:

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)

but after running it I get this error `MongoServerError[Unauthorized]: Command createUser requires authentication.

I am also running mongo in docker container. This is my docker compose file:

version: '3.1'

services:
  
  mongo:
    image: mongo
    volumes:
      - mongodb:/var/lib/mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    ports:
      - 27017:27017

volumes:
  mongodb:

Does anyone know what is the issue with creating user ?