Quickstart: Compose and Rails. Estimated reading time: 8 minutes. This Quickstart guide shows you how to use Docker Compose to set up and run a Rails/PostgreSQL app. Before starting, install Compose. Define the project. Start by setting up the files needed to build the app. The app will run inside a Docker container containing its dependencies. As you can see, we use the preconfigured Docker image comprising the Apache web server and PHP 7.1 with Xdebug. Note that we use the host.docker.internal value to refer to the remote host. In Docker for Windows and Docker for Mac, it automatically resolves to the internal address of the host, letting you easily connect to it from the container. For those running docker toolbox, shutting down the virtual box and then running the 'Docker quickstart terminal' resolved it for me. Maybe there's a better way to restart it, but that worked. – JohnnyFun Jan 27 '19 at 14:57. Docker Quickstart Terminal comes with Docker Toolbox. Docker toolbox is for older PCs, the latest ones need “Docker for windows” which does not have the Docker Quickstart Terminal. Also, the services show that docker is running, and so does “net start com.docker.service”.
- Docker Quickstart Terminal Mac
- Docker Quickstart Terminal Mac Os
- Docker Quickstart Terminal Checking If Machine Default Exists
- Docker Quickstart Terminal Mac Commands
- Docker Quickstart Terminal Mac Mojave
Estimated reading time: 8 minutes
This Quickstart guide shows you how to use Docker Compose to set up and runa Rails/PostgreSQL app. Before starting, install Compose.
Define the project
Start by setting up the files needed to build the app. The app will run inside aDocker container containing its dependencies. Defining dependencies is done usinga file called Dockerfile
. To begin with, the Dockerfile consists of:
That’ll put your application code inside an image that builds a containerwith Ruby, Bundler and all your dependencies inside it. For more information onhow to write Dockerfiles, see the Docker user guideand the Dockerfile reference.
Next, create a bootstrap Gemfile
which just loads Rails. It’ll be overwrittenin a moment by rails new
.
Create an empty Gemfile.lock
to build our Dockerfile
.
Next, provide an entrypoint script to fix a Rails-specific issue thatprevents the server from restarting when a certain server.pid
file pre-exists.This script will be executed every time the container gets started.entrypoint.sh
consists of:
Finally, docker-compose.yml
is where the magic happens. This file describesthe services that comprise your app (a database and a web app), how to get eachone’s Docker image (the database just runs on a pre-made PostgreSQL image, andthe web app is built from the current directory), and the configuration neededto link them together and expose the web app’s port.
Tip
You can use either a .yml
or .yaml
extension for this file.
Build the project
With those files in place, you can now generate the Rails skeleton appusing docker-compose run:
Docker Quickstart Terminal Mac
First, Compose builds the image for the web
service using the Dockerfile
.The --no-deps
tells Compose not to start linked services. Then it runsrails new
inside a new container, using that image. Once it’s done, youshould have generated a fresh app.
List the files.
If you are running Docker on Linux, the files rails new
created are owned byroot. This happens because the container runs as the root user. If this is thecase, change the ownership of the new files.
If you are running Docker on Mac or Windows, you should already have ownershipof all files, including those generated by rails new
.
Now that you’ve got a new Gemfile, you need to build the image again. (This, andchanges to the Gemfile
or the Dockerfile, should be the only times you’ll needto rebuild.)
Connect the database
The app is now bootable, but you’re not quite there yet. By default, Railsexpects a database to be running on localhost
- so you need to point it at thedb
container instead. You also need to change the database and username toalign with the defaults set by the postgres
image.
Replace the contents of config/database.yml
with the following:
You can now boot the app with docker-compose up:
If all’s well, you should see some PostgreSQL output.
Finally, you need to create the database. In another terminal, run:
Here is an example of the output from that command:
View the Rails welcome page!
That’s it. Your app should now be running on port 3000 on your Docker daemon.
On Docker Desktop for Mac and Docker Desktop for Windows, go to http://localhost:3000
on a webbrowser to see the Rails Welcome.
Docker Quickstart Terminal Mac Os
Stop the application
To stop the application, run docker-compose down inyour project directory. You can use the same terminal window in which youstarted the database, or another one where you have access to a command prompt.This is a clean way to stop the application.
Restart the application
To restart the application run docker-compose up
in the project directory.
Rebuild the application
If you make changes to the Gemfile or the Compose file to try out some differentconfigurations, you need to rebuild. Some changes require onlydocker-compose up --build
, but a full rebuild requires a re-run ofdocker-compose run web bundle install
to sync changes in the Gemfile.lock
tothe host, followed by docker-compose up --build
.
Here is an example of the first case, where a full rebuild is not necessary.Suppose you simply want to change the exposed port on the local host from 3000
in our first example to 3001
. Make the change to the Compose file to exposeport 3000
on the container through a new port, 3001
, on the host, and savethe changes:
Now, rebuild and restart the app with docker-compose up --build
.
Inside the container, your app is running on the same port as before 3000
, butthe Rails Welcome is now available on http://localhost:3001
on your localhost.
More Compose documentation
documentation, docs, docker, compose, orchestration, containersEstimated reading time: 8 minutes
This quick-start guide demonstrates how to use Docker Compose to set up and run a simple Django/PostgreSQL app. Before starting,install Compose.
Define the project components
For this project, you need to create a Dockerfile, a Python dependencies file,and a docker-compose.yml
file. (You can use either a .yml
or .yaml
extension for this file.)
Create an empty project directory.
You can name the directory something easy for you to remember. This directory is the context for your application image. The directory should only contain resources to build that image.
Create a new file called
Dockerfile
in your project directory.The Dockerfile defines an application’s image content via one or more build commands that configure that image. Once built, you can run the image in a container. For more information on
Dockerfile
, see the Docker user guide and the Dockerfile reference.Add the following content to the
Dockerfile
.This
Dockerfile
starts with a Python 3 parent image.The parent image is modified by adding a newcode
directory. The parent image is further modifiedby installing the Python requirements defined in therequirements.txt
file.Save and close the
Dockerfile
.Create a
requirements.txt
in your project directory.This file is used by the
RUN pip install -r requirements.txt
command in yourDockerfile
.Add the required software in the file.
Save and close the
requirements.txt
file.Create a file called
docker-compose.yml
in your project directory.The
docker-compose.yml
file describes the services that make your app. In this example those services are a web server and database. The compose file also describes which Docker images these services use, how they link together, any volumes they might need to be mounted inside the containers. Finally, thedocker-compose.yml
file describes which ports these services expose. See thedocker-compose.yml
reference for more information on how this file works.Add the following configuration to the file.
This file defines two services: The
db
service and theweb
service.Note:
This uses the build in development server to run your applicationon port 8000. Do not use this in a production environment. For moreinformation, see Django documentation.
Save and close the
docker-compose.yml
file.
Create a Django project
Docker Quickstart Terminal Checking If Machine Default Exists
In this step, you create a Django starter project by building the image from the build context defined in the previous procedure.
Change to the root of your project directory.
Create the Django project by running the docker-compose runcommand as follows.
This instructs Compose to run
django-admin startproject composeexample
in a container, using theweb
service’s image and configuration. Becausetheweb
image doesn’t exist yet, Compose builds it from the currentdirectory, as specified by thebuild: .
line indocker-compose.yml
.Once the
web
service image is built, Compose runs it and executes thedjango-admin startproject
command in the container. This commandinstructs Django to create a set of files and directories representing aDjango project.After the
docker-compose
command completes, list the contents of your project.If you are running Docker on Linux, the files
django-admin
created areowned by root. This happens because the container runs as the root user.Change the ownership of the new files.If you are running Docker on Mac or Windows, you should alreadyhave ownership of all files, including those generated by
django-admin
. List the files just to verify this.
Connect the database
In this section, you set up the database connection for Django.
Docker Quickstart Terminal Mac Commands
In your project directory, edit the
composeexample/settings.py
file.Replace the
DATABASES = ...
with the following:These settings are determined by thepostgres Docker imagespecified in
docker-compose.yml
.Save and close the file.
Run the docker-compose up command from the top level directory for your project.
At this point, your Django app should be running at port
8000
onyour Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, gotohttp://localhost:8000
on a web browser to see the Djangowelcome page.Note:
On certain platforms (Windows 10), you might need to edit
ALLOWED_HOSTS
insidesettings.py
and add your Docker host name or IP address to the list.For demo purposes, you can set the value to:This value is not safe for production usage. Refer to theDjango documentation for more information.
List running containers.
In another terminal window, list the running Docker processes with the
docker ps
ordocker container ls
command.Shut down services and clean up by using either of these methods:
Stop the application by typing
Ctrl-C
in the same shell in where youstarted it:Or, for a more elegant shutdown, switch to a different shell, and rundocker-compose down from the top level of yourDjango sample project directory.
Docker Quickstart Terminal Mac Mojave
Once you’ve shut down the app, you can safely remove the Django project directory (for example, rm -rf django
).