Can't run rake db:create in Dockerfile with docker-compose

When your web image is built (following the instructions of the Dockerfile), it doesn't have a connection to a db container.

The webserver and database images are independents and the containers are linked when you launch them (following the definitions of the docker-compose.yml file).

You cannot link to a container during the image build because it would break the principle that an image build must be reproducible. Similarly, you cannot mount a volume from the host machine during an image build neither.

The docker-compose run web rake db:create command you used is a correct way to initalize the database.

Alternatively, you could launch the containers normaly with docker-compose, and then use the docker exec command to execute rake db:create in the web container.