Estimated reading time: 8 minutes

This is a bug report; This is a feature request; I searched existing issues before opening this one; Expected behavior. As in docker-for-mac and docker-for-windows, inside a container, the DNS name host.docker.internal resolves to an IP address allowing network access to the host (roughly the output of ip -4 route list match 0/0 cut -d' ' -f3 inside the same container). @GarouDan if you don't want to map a port but still want to access the postgres container from your host you'll need to deploy your container on the host network like this: docker run -net=host -name some-postgres -e POSTGRESPASSWORD=mysecretpassword -d -p 5432:5432 postgres – lvthillo May 26 '18 at 22:27.

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.)

Docker Postgres Mac
  1. 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.

  2. 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.

  3. Add the following content to the Dockerfile.

    This Dockerfile starts with a Python 3 parent image.The parent image is modified by adding a new code directory. The parent image is further modifiedby installing the Python requirements defined in the requirements.txt file.

  4. Save and close the Dockerfile.

  5. Create a requirements.txt in your project directory.

    This file is used by the RUN pip install -r requirements.txt command in your Dockerfile.

  6. Add the required software in the file.

  7. Save and close the requirements.txt file.

  8. 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, the docker-compose.yml file describes which ports these services expose. See the docker-compose.yml reference for more information on how this file works.

  9. Add the following configuration to the file.

    This file defines two services: The db service and the web service.


    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.

  10. Save and close the docker-compose.yml file.

其中postgres-export.tar是通过docker export导出的容器包,postgres-save.tar是通过docker save保存的镜像包,两者都是基于postgres:9.6镜像。从文件大小可以直观的发现,postgres-export.tar显然要比postgres-save.tar小100多M。 现在试试docker load容器包postgres-export.tar:. @GarouDan if you don't want to map a port but still want to access the postgres container from your host you'll need to deploy your container on the host network like this: docker run -net=host -name some-postgres -e POSTGRESPASSWORD=mysecretpassword -d -p 5432:5432 postgres – lvthillo May 26 '18 at 22:27.

Create a Django project

In this step, you create a Django starter project by building the image from the build context defined in the previous procedure.

  1. Change to the root of your project directory.

  2. Create the Django project by running the docker-compose runcommand as follows.

    This instructs Compose to run django-admin startproject composeexamplein a container, using the web service’s image and configuration. Becausethe web image doesn’t exist yet, Compose builds it from the currentdirectory, as specified by the build: . line in docker-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.

  3. 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 bydjango-admin. List the files just to verify this.

Connect the database

In this section, you set up the database connection for Django.

  1. In your project directory, edit the composeexample/ file.

  2. Replace the DATABASES = .. with the following:

    These settings are determined by thepostgres Docker imagespecified in docker-compose.yml.

  3. Save and close the file.

  4. 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, goto http://localhost:8000 on a web browser to see the Djangowelcome page.


    On certain platforms (Windows 10), you might need to edit ALLOWED_HOSTSinside 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.

  5. List running containers.

    In another terminal window, list the running Docker processes with the docker ps or docker container ls command.

  6. 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.

Once you’ve shut down the app, you can safely remove the Django project directory (for example, rm -rf django).

More Compose documentation

Docker Postgres Macdocumentation, docs, docker, compose, orchestration, containers

Here are my personal notes on how to set up Postgres locally using Docker. This is by no means the perfect or one-and-only method, but it works for my specific needs, and might help you as-well.

I have also included how to easily download and import data from a Heroku-hosted Postgres database, although the same can easily be adapted for other hosting solutions too.


  • Some familiarity with Terminal (I recommend iTerm for Mac, or cmder for Windows)
  • A Mac or access to a Linux (virtual) machine (Full Windows support is outside the scope of this guide)
  • About 15 minutes for the initial setup


  • Set up Docker
  • Launch a Postgres instance
  • (optional) Restore data from a dump file

In case you already have Docker set up on your machine, you can skip to the next step. If not, read on;

For macOS

You can download Docker Desktop for Mac and follow its installation instructions. Once installed and launched, you'll see a Docker status menu icon in the top-right of your display. The first time you launch Docker it might take a few minutes for it to initialize, so wait for it to complete before you proceed.

Note: Docker Desktop comes with an application called Kitematic, which allows you to more easily control docker containers using a GUI. You can use this application to start and stop containers and modify basic settings, but you'll still need to use Terminal for some of the commands mentioned, as these are not available through Kitematic.

For Ubuntu

This guide should work without issue on any recent version of Ubuntu. I have successfully used this on Ubuntu 16.04 as-well as 18.04 without issue, and it should work fine on other versions too although I have not tried this myself.

Start by installing the following required packages:

And now add Docker's official GPG key, this will allow us to add the Docker repository:

Now add Docker's repository, this will allow you to install Docker using apt-get:

Now that everything is prepared, you can install docker community edition with the following command:

When the installation is finished, add your user to the docker group so you don't have to use sudo when working with Docker:

Close and re-open your Terminal (or logout and back in if you're doing this on a remote machine) for the changes to take full effect.

(Optional) Start Docker at Boot

If you'd like to have Docker start at system boot, you can run this command:

(Optional) Allow TCP acces to Docker

If you're running Docker on a separate (virtual) machine, you might want to enable TCP access so you can use Docker from your local machine more conveniently.

Assuming you used systemctl (above) to start Docker at boot, run this command:

Docker Postgres Compose

Your favorite command line text editor of choice will open with an empty file. Paste or write the following in:

Update: It seems like they made a change to Docker at some point that killed support for adding -H -fd://, which was the previously recommended way to have Docker listen to the default unix socket as-well. I am not sure when exactly this change was made, and if it's a permanent one or not, but the method I describe above seems to work and is also referenced here.

Save the file ( ctrl+x, y, «enter» if you're using Nano, or ctrl+c, wq, «enter» if you're using VIM), and run the following commands:

You should now be able to remotely access your Docker. You can run any command as you normally would from your local machine, you only need to add -H to your command with the IP address of the (virtual) machine that Docker is running on. For example:

Docker Postgresql Mac Os X

The default port is2375and if you're using this can be omitted from the aforementioned command.

To not have to add the IP address every time you run a Docker command, you can add an environment variable to your .profile or .bashrc or equivalent file. Assuming you're adding it to your .bashrc, you can add it like so:

Be sure to adapt this to your specific needs. For example, if you're using something like Oh My ZSH you might want to add it to your .zshrc file instead.

Setting up and launching a Postgres Docker instance

Once you've taken the following steps once, you don't need to repeat them. You can simply start your container up (again) by running docker start postgres

Creating a new Postgres Docker container

Docker containers inherently don't have permanent storage, so we'll start by creating a Docker volume. This makes it easier to if, for whatever reason, you want to destroy and launch a new Postgres container without losing your database data.

Run the following command to create a Docker volume. Note that the example uses the name pgdata, but you can change this to whatever you like. Be sure to use the same name in all subsequent commands too if you do change it though.

Now we can create the container. I recommend using an easy to remember port such as 54320, which is Postgres' default port with a zero added at the end. This way it can be easily remembered and at the same time you avoid possible conflicts should you, for whatever reason, already have another Postgres instance running on the default port.

Note: Be sure to set the password to something secure enough. You should never expose your Docker instance or Postgres container to the world but it's important to ensure no unwanted person (even via your local network) can access all your data.

Docker will automatically download the Postgres image if you don't already have it on your machine. When the command finishes, a fresh Postgres container should be up and running in the background. To confirm ,try connecting to it using your favorite GUI client or by using something like the psql command-line interface.

The IP address to connect to will be that of the (virtual) machine Docker is running on, with the port you have specified ( 54320 if you followed my example), the username postgres and the password you have specified in the recentmost command.

Docker Postgres Image

Creating a Database

You can now create a database to use to either import your existing data into, or for setting up your new project with.

Docker postgres create database
Tip: You do not have to specify your password with this command as it is defined in the container's environment variable.

You should nw have a new and clean database, ready for use.

To restore a database dump into your local Docker container, first ensure your container is currently running. You can use docker container ls to check if it's on already. If it's not, simply start it by running docker start postgres.

Obtaining a dump of your Heroku-hosted databae

There are several ways to go about obtaining a database dump file from Heroku. If you have permission to accces the Heroku application in question, you can visit the Heroku datastores page and download the recent-most backup, or use Heroku's command-line interface to fetch the same file. This guide will assume the latter route, and walk you through how to set this up for convenient (re-)use.

Heroku's CLI

Using Heroku's CLI is probably the easiest method after initial setup, a fetching an updated dump file will only take you one command. If, however, you only plan to fetch a dump file once (or very rarely at most), it migth be easiest to simply manually download via Heroku's Dashboard.

Start by installing the Heroku CLI if you haven't already. for macOS you can use brew (or, alternatively, Heroku's installer): Create a macos catalina bootable usb.

For Ubuntu:


After installing, log in with your Heroku user credentials by triggering this command and following its steps:

When this is done, you can easily download the recent-most backup using the following command:

Docker Postgres Create Database

Note: this creates a filed called latest.dumpin the current working directory. Be sure to run this command in a convenient location, such as ~/

Docker Postgres Machine

Restoring the data into your Docker container

Assuming you have the Docker container running and a data dump file ready, you can run the following command to import all dat. Be sure to substitute ./latest.dump to where your dump file is located, if it's not in the current working directory and called latest.dump.

This command may take several minutes to complete, depending on how big your database is. Verbose mode is enabled in the above command so you can more easily track what's going on. Once it finishes, you should be all set and ready to go.

Docker Postgres Conf

I hope this guide was helpful to you. I had originally written this down for my own reference, but thought it might be useful for others as-well. Happy coding!

Coments are closed
Scroll to top