1. Docker Machine 最后更新时间是在0.16.0 (2018-11-08)版本 。官方建议在1.12以及之后版本使用Docker Desktop for Mac和Docker Desktop for Windows进行代替。 Docker Toolbox. Docker Toolbox是用于帮 Windows系统和Mac系统 安装Docker环境。.
  2. I got tired of this and used docker-machine with vmware driver. It's a bit tricky to set up to work as 'natural' as docker for mac (used wrapper for docker compose, some envvars-to-compose-file magic and shared folders), but IO is so much faster that it's worth it. It you folks have some spare time and a licence (or demo), try it.

Datadog APM integrates with Postgres to see the traces across your distributed system. Trace collection is enabled by default in the Datadog Agent v6+. To start collecting traces: Enable trace collection in Datadog. Instrument your application that makes requests to Postgres. Available for Agent versions 6.0. Feb 02, 2021 Docker Machine 最后更新时间是在0.16.0 (2018-11-08)版本 。官方建议在1.12以及之后版本使用Docker Desktop for Mac和Docker Desktop for Windows进行代替。 Docker Toolbox. Docker Toolbox是用于帮 Windows系统和Mac系统 安装Docker环境。. On Docker Desktop for Mac and Docker Desktop for Windows. 10 minutes ago Up 9 minutes 0.0.0.0:8000- 8000/tcp djangoweb1 678ce61c79cc postgres 'docker.

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:

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 Postgres Centos

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 3000in 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, 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.

Docker

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.

Prerequisites

  • 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

TL;DR

  • 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:

Docker Postgresql Mac Os X

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:

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

Docker postgresql max_connections
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.

Docker Postgres Machine

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:

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.

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.

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

Docker Postgres Compose

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.

Docker Postgres Create Database

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

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:

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 Image

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.

Docker Mac Postgres Slow

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.

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!