First of all install docker on your Ubuntu Machine.
apt install docker.io
To check the status of docker, whether it is installed properly and running.
systemctl status docker.service
Then pull the metasploitable2 docker image.
docker pull tleemcjr/metasploitable2
Check whether the docker image has been successfully downloaded.
docker images
Now run the docker container Image file.
docker run -it tleemcjr/metasploitable2
Once everything has been built successfully, you should see a terminal prompt like below and check the IP.
Now you can Access the Metasploitable2 Vulnerable machine via browser.