Skip to content

Instantly share code, notes, and snippets.

@hex337

hex337/blog.md Secret

Created August 17, 2018 03:54
Show Gist options
  • Save hex337/a6706fbd6a2c0a17c4cc0afb2fa1b75f to your computer and use it in GitHub Desktop.
Save hex337/a6706fbd6a2c0a17c4cc0afb2fa1b75f to your computer and use it in GitHub Desktop.

As a follow up to my original post, I wanted to create a cleaner system for getting phoenix projects up and running.

We'll set up a container that's running Phoenix 1.3.4 with Elixir 1.6. It'll be backed by a postgres database. We'll do all of this without installing anything on our local machine.

Create a new directory for your project, we'll call this phoenix-api for now.

https://gist.github.com/bddacff5cc1bd66020820323b4e7d2c2

Dockerfile

We'll go ahead and set up a Dockerfile to build a container, which we can then use to run the commands we'll need to initalize the Phoenix project.

https://gist.github.com/056f7556d185131b7813837cade263fb

With this done, we can run docker build -t phoenix-api:latest . and get a container for this Dockerfile with the phoenix-api:latest tag.

Compose That API

Next up, we'll set up our bare bones compose file.

We have two containers that we need, our api server and our database.

Lets create our docker-compose.yml file:

https://gist.github.com/10072a983b07245f0d0b10c2d4644ba9

We can now run a compose build with

docker-compose build

Once that builds, we need to get a bash prompt on the container with

docker-compose run --rm api bash

The --rm makes sure that we dispose of our container when we're done, since we don't need it to stick around. We just want to create a new project and then drop it.

With a bash prompt, we can install the phx_new mix task.

https://gist.github.com/b5970446689ddbc377c8b2f048c7fa3d

This will then ask you if you are sure since the phoenix-api folder already exists. Say Yes to that and for installing dependencies. I'm sure some people will complain that this circumvents the whole umbrella project thing, but I find that this is what I want most of the time for my own projects. Using the relative directory and having it bound to your local dir prevents the nesting from happening.

Once all that finishes, let's get our dependencies installed and then exit out of the container.

https://gist.github.com/2284b90010e4b118c658e16d2235b926

Setting up the Database

We'll have to tell Phoenix where our dockerized database lives, so boot up your favorite editor for config/dev.ex.

Find the part that looks like this and change the hostname field to use the name of the db container.

https://gist.github.com/5e8b063ba79a498d454aa2a5cae49dd4

To now create the table, lets run docker-compose run --rm api mix ecto.create. Since the api container depends on the db container, that will start up before the api container spins up.

Setting up the Entryfile

Next up, we'll want to modify our Dockerfile to add an entry file that will boot up our Phoenix service when the container starts.

Open up the Dockerfile and edit the bottom part to look like this:

https://gist.github.com/a47968ea1adba1ee750f9711d4787d2a

The docker-entrypoint.sh is what we care about. But... what does that look like?

Probably something like this (NOTE: you have to have your database container name in the -h flag for this to work properly):

https://gist.github.com/9ed48e773262d8f6e69c5ad2b7d19e06

We need to allow our entrypoint to be executable:

https://gist.github.com/a3dd0c670430d5b8eb99fb97680b192a

Putting it all Together

Build and run the containers:

https://gist.github.com/0cee6771b95101dd3b1fa40787889a1f

We can see when the server is ready by following the logs:

https://gist.github.com/8cc5b31c24712a2f87b6170a1c06f69b

Once everything is running, you can see an error page at http://localhost:4000. Now you just have to add some API routes.

Congrats on getting a Phoenix project started without installing any Elixir on your computer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment