Deploying with Docker

Vagrant vs. Docker




version: '2' 
image: postgres
- "5432:5432"
build: .
command: bundle exec rake db:create db:migrate
command: bundle exec rails s -p 3000 -b ''
- .:/myapp
- "3000:3000"
- db
- db
- KEY=your-key-here
  • Under services, there are two sections called web and db respectively. This means that I am building two containers, one which will hold my app, and the other will hold my database.
  • Within the db section, the image: postgres tells Docker that PostgreSQL is a dependency of my app, and it will install it if it’s not already on the host.
  • The portscommand in both sections exposes the ports for the database and the application — Docker doesn’t do this autmatically. You can expose the internal ports externally as any port you want. For example, you could say ports:- “80:3000”, and what is port 3000 inside the container (Rails’ default port), would be exposed as port 80.
  • The commands lines are fairly obvious: they run those commands, creating and migrating my database, and starting the server. These need to be done in that order.
  • A volume is basically a directory, and tells Docker where to save my app.
  • The links and depends_on sections allow the web and db containers to communicate, which they can’t automatically do. The depends_on section will also determine the order in which the containers are started — the database will be started prior to the application.
  • The environments section contains your environmental variables. My real ones aren’t included here, obviously, but the formatting shown is what it should look like.

Making it all happen!

  • docker-compose build
  • docker-compose up

Senior Software Engineer |

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrienne Domingus

Adrienne Domingus

Senior Software Engineer |

More from Medium

Deploying VueJS With Java Backend on AWS EKS using HELM

Django App deployment on Azure — Azure Plugin, CI/CD, security configurations and data persistance

Setting up SSH Authentication in Remote Server

GitLab CI: How to Deploy a Docker image to AWS Elastic Beanstalk from a Private Docker Registry