Docker Server Setup
Start Database Servers w/ single commands
Docker Server Setup
Who’s this guide for?
- Have you ever wanted to test an app with a ‘throw-away’ database?
- Inherited a suspicious codebase? Prefer not sharing access to your existing database?
- Work with security sensitive clients? Don’t risk cross-contamination! Use containers & control data persistance!
- Can’t upgrade your dev environment to the latest database version because your legacy apps depend on a 12 year old version of mysql?
Never let those reasons get in your way again!
Quick Links To 1-liners
This article features 1-line commands to start some of the most popular databases, including:
Note: The commands work in production. However they are mainly designed to accelerate your development workflow.
Advanced Docker Users: If you are familiar with
docker-compose
you may want to convert the shell commands below for use in yourdocker-compose.yml
files.
Postgres Server
Start a container, naming it
pg-server
Adjust the command line options as needed. (The postgres daemon arguments start following the docker image name
postgres:12-alpine
. Seepostgres -c 'listen_addresses=*'...
)
Access the
psql
prompt as postgres user
Access the container’s shell as root
Note: The above command uses the official Alpine Linux base images. It is not your typical debian environment.
To use the debian/ubuntu base image, change
postgres:12-alpine
topostgres:12
.
MongoDB Server
Now that your server is setup, verify your data is at $HOME/.mongodb
with:
Let’s connect to the server with mongo
CLI tool. (If you don’t have it installed see below.)
And you should see something like this:
Setup mongo CLI tools
Using brew & OSX
Mysql Server
WARNING: CHANGE THE PASSWORD IN
MYSQL_ROOT_PASSWORD
BELOW.
ElasticSearch Server
Security Notes
NOTE: the
-p 127.0.0.1:27017:27017
-style port option prevents access to your instance except from the docker server’s localhost network. To ‘publish’ the exposed ports, remove the local IP address prefix to allow external access:-p 27017:27017
. Make sure you have taken necessary security precautions.
Recommended: Always use a port scanning tool (like nmap/masscan) to verify your network configuration (from separate system on another network.)
Now that you have the commands to start your database servers, the next step is to package up your application as a docker image. Part 2 continued below:
Packaging a NodeJS Web App
- Add a blank file named
Dockerfile
in your project root. - (Optional, Recommended) Add a
.dockerignore
using .gitignore rules to exclude large non-essential paths. By default all project files are included.
Create a Dockerfile
in your apps root
It’s easier to show how to start using the Dockerfile and demonstrate the results via console (see commands below).
In terminal, cd
to your project folder and run the following build
command everytime you deploy changes - or want to change/upgrade OS or Env config)
Key Docker Commands Reference
Build Docker Image
Create/Run Web App w/ Links to DB Servers
Run Interactively (non-daemon, in terminal)
Delete Container Instance or Image
Important: Any data not stored on a mounted volume path will be lost!!