Workflow

Part 1, Lesson 12



Reference guide…

Aliases

To save some precious keystrokes, let’s create aliases for both the docker-compose and docker-machine commands - dc and dm, respectively.

Simply add the following lines to your .bashrc file:

alias dc='docker-compose'
alias dm='docker-machine'

Save the file, then execute it:

$ source ~/.bashrc

Test them out!

On Windows? You will first need to create a PowerShell Profile (if you don’t already have one), and then you can add the aliases to it using Set-Alias - i.e., Set-Alias dc docker-compose.

“Saved” State

Is the VM stuck in a “Saved” state?

$ docker-machine ls

NAME               ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
testdriven-prod    *        amazonec2    Running   tcp://34.207.173.181:2376           v17.09.0-ce
testdriven-dev     -        virtualbox   Saved                                         Unknown

First, try:

$ docker-machine start testdriven-dev

If that doesn’t work, to break out of this, you’ll need to power off the VM:

  1. Start virtualbox - virtualbox
  2. Select the VM and click “start”
  3. Exit the VM and select “Power off the machine”
  4. Exit virtualbox

The VM should now have a “Stopped” state:

$ docker-machine ls

NAME               ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
testdriven-prod    *        amazonec2    Running   tcp://34.207.173.181:2376           v17.09.0-ce
testdriven-dev     -        virtualbox   Stopped  

Now you can start the machine:

$ docker-machine start dev

It should be “Running”:

$ docker-machine ls

NAME               ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
testdriven-prod    *        amazonec2    Running   tcp://34.207.173.181:2376           v17.09.0-ce
testdriven-dev     -        virtualbox   Running   tcp://192.168.99.100:2376           v17.09.0-ce  

Can’t Download Python Packages?

Are you running into this error when trying to pip install inside a Docker Machine?

Retrying (Retry(total=4, connect=None, read=None, redirect=None))
after connection broken by 'NewConnectionError(
  '<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f0f88deec18>:
Failed to establish a new connection: [Errno -2] Name or service not known',)':
/simple/flask/

Restart the Machine and then start over:

$ docker-machine restart testdriven-dev
$ docker-machine env testdriven-dev
$ eval $(docker-machine env testdriven-dev)
$ docker-compose -f docker-compose-dev.yml up -d --build

Common Commands

Build the images:

$ docker-compose -f docker-compose-dev.yml build

Run the containers:

$ docker-compose -f docker-compose-dev.yml up -d

Create the database:

$ docker-compose -f docker-compose-dev.yml \
  run users python manage.py recreate_db

Seed the database:

$ docker-compose -f docker-compose-dev.yml \
  run users python manage.py seed_db

Run the tests:

$ docker-compose -f docker-compose-dev.yml \
  run users python manage.py test

Other commands

To stop the containers:

$ docker-compose -f docker-compose-dev.yml stop

To bring down the containers:

$ docker-compose -f docker-compose-dev.yml down

Want to force a build?

$ docker-compose -f docker-compose-dev.yml build --no-cache

Remove images:

$ docker rmi $(docker images -q)

Postgres

Want to access the database via psql?

$ docker exec -ti users-db psql -U postgres -W

Then, you can connect to the database and run SQL queries. For example:

# \c users_dev
# select * from users;