Docker linked container problems

I need a postgres container linked to a rails container. The rails container needs to install SpreeCommerce, but not before pointing to the postgres container for the database connection.

Using docker-compose to orchestrate these things leads to a common problem. All of the containers launch concurrently, and the rails container attempts the Spree Commerce installation before the postgres server is actually listening for connections.

Let it be known I am holding back from venting about the dichotomy between Docker’s amazing ability to simplify the development/deployment process, and Docker’s grand reluctance to implement even a basic ready-state for containers (instead insisting on multi-step implementation of ready-state scripts). Docker has their reasons, but google the issue and you’ll see this is a sorely needed functionality. Whoops, I just vented.

This post aims to create more of a ‘lay tutorial’ for linking containers. I explore the method of using a shell script (on container1) that waits and listens for a postgres server (on container2) to become ready, after which container1 can install Spree Commerce (as spree initially populates its database).

Docker recommends using a docker-compose entrypoint, and a CMD (in the dockerfile) to be completed after wait-for-it.sh says the database is ready.

Using the example from Docker’s recommendation. The docker-compose file looks like this:

docker-compose.yml

version: “2”
services:
web:
build: .
ports:
– “80:8000”
depends_on:
– “db”
entrypoint: ./wait-for-it.sh db:5432
db:
image: postgres

Breakdown:
docker-compose.yml has two services (actions), and thus builds two images. Whenever you want to use those images, they create separate entities called containers.

web:
build: .
#builds the web image using the dockerfile located in the directory you run the “docker-compose build .” command from. This directory is alternatively called the ‘current context’. Note that those dockerfile instructions are not shown in this post.

ports:
– “8000:80”
#expose ports to the host as HOST:CONTAINER. That is, container port 8000 is routed to host port 80. For example, if you are running a webserver in your container that is expecting traffic from port 8000, you can communicate with it from the computer running docker via your-docker-host-ip:80. Note that if you try to expose ports via your dockerfile (not covered here), those ports are only available to other containers within your virtual docker network. For example, using EXPOSE 8000 in your dockerfile would allow outgoing traffic from other containers (whatever port they choose) to be sent to port 80 on the container you EXPOSED.

depends_on:
– “db”
#some newcomers may be tempted to think that the web image is somehow being built from db. That is not true here. The depends_on key instead means the Web container will not run without the companionship of the DB container (example: if you were to use docker-compose to build these two images, create their respective containers, manually delete the db container, you would not be able to launch the web container).

entrypoint: ./wait-for-it.sh db:5432
#First off, wait-for-it.sh is a github project by vishnubob–big thanks to him (and his employer Ginkgo Bioworks). In a nutshell, this instruction looks for Work-dir-set-by-dockerfile/wait-for-it.sh located in the Web image. That is, in the dockerfile you can set a work directory (such as /usr/home/scripts/) and that will become the relative path used whenever you don’t explicitly specify an absolute path. The arguments passed to the script (db:5432) tell the script to repeatedly check the linked container db for a port 5432 response.

db:
image: postgres
#This is analogous to the “build: .” key used above in Web. Instead of looking to the dockerfile for image build instructions, this points docker to use an image that already exists (docker looks locally for the existence of a “postgres” image, and if not found, looks on the official docker repository).

What is my docker host ip (Windows)?

On Docker for Windows, it wasn’t immediately obvious how to find out the host IP of docker. I tried docker-machine ip default to list the ip address of my docker host, most likely called ‘default’, but it doesn’t exist. In fact for whatever reason, when I run “docker-machine ls”, there are no host machines listed.

I checked ipconfig and see a DockerNAT virtual ethernet adapter at 10.0.75.1 (which is indeed my docker host ip). I can also see the docker host IP by right clicking on the Docker icon in the system tray, choosing settings, and network.

On Mac and Linux systems, “boot2docker ip” will suffice.

Create a launcher for Sublime Text which always opens a new window (in Windows)

The default behavior of the Sublime Text 3 launcher in Windows is to show any currently open Sublime Text windows. This really shouldn’t be the default behavior, but what-evs.

My quickfix: create a new batch file named sublime-launcher.bat with the following contents:
start “C:/Program Files/Sublime Text 3/sublime_text.exe” “–new-window”

For whatever reason, I’m not able to pin this to my start bar in Windows 10. Tired of these things–so as soon as I get the time I’ll be swapping my primary installation over to some flavor of linux.

Using get_template_part() in wordpress theme development.

get_template_part() calls some other functions and essentially has the action of doing a require(‘something.php’). Generally: get_template_part() -> locate_templates() -> load_template() -> require(‘theFoundTemplateFile.php’).

It takes two arguments. get_template_part(‘generic template file’, ‘specialized template file’). The first argument is commonly called the ‘slug’ and the second the ‘name’. No worries though.

Let’s contextualize this a little. Assume we have a file structure like this:

wp-content/themes/mytheme/themecore.php
wp-content/themes/mytheme/themecore-fullwidth.php
wp-content/themes/mytheme/themecore-nosidebar.php
wp-content/themes/mytheme/themecore-searchpage.php

wp-content/themes/mythemechild/themecore.php
wp-content/themes/mythemechild/themecore-nosidebar.php

—–

Example 1 (EX1):
And you use get_template_part(‘themecore’). This is the simplest situation. WordPress will search for the template in the child theme directory, then parent theme directory in that order, using the first template file it comes to with the right name.

It looks through these directories for ‘themecore.php’:
wp-content/themes/mythemechild/
wp-content/themes/mytheme/

WordPress finds wp-content/themes/mythemechild/themecore.php first and loads it, ignoring the other file in the parent theme.

Example 2 (EX2):
Let’s use two arguments: get_template_part(‘themecore’,’nosidebar’). WordPress searches for ‘themecore-nosidebar.php’ in the same order as in EX1.

It looks through these directories for ‘themecore-nosidebar.php’:
wp-content/themes/mythemechild/
wp-content/themes/mytheme/

WordPress finds wp-content/themes/mythemechild/themecore-nosidebar.php first and loads it, ignoring the other file in the parent theme.

Example 3 (EX3):
Still two arguments: get_template_part(‘themecore’,’fullwidth’). WordPress searches for ‘themecore-fullwidth.php’ in the child, then parent theme folders.

It looks through these directories for ‘themecore-fullwidth.php’:
wp-content/themes/mythemechild/
wp-content/themes/mytheme/

WordPress has no luck in the child theme folder, and finds wp-content/themes/mytheme/themecore-fullwidth.php and loads it.

Example 4 (EX4):
Still two arguments: get_template_part(‘themecore’,’idontexist’). WordPress searches for ‘themecore-idontexist.php’ in the child, then parent theme folders. But if it doesn’t find this appended file name, it is supposed to repeat the search without it.

It looks through these directories for ‘themecore-idontexist.php’:
wp-content/themes/mythemechild/
wp-content/themes/mytheme/

WordPress doesn’t find themecore-idontexist.php, so it searches in the same order for the base template ‘themecore.php’. It ends up using wp-content/themes/mythemechild/themecore.php.

Adding a google font to a wordpress theme (via child theme)

Anatomy: Background // What you came here for // Conclusion

Background:
Since I’ve gone freelance, a number of jobs I’ve done are WordPress updates, repairs, and migrations.

It’s been a challenge and forced me to rework my general approach to technical troubleshooting to save time. I have a locust of problems experienced locusting through my poor head right now from the site I’m currently working on, and situations worth mentioning from past sites. Do I fix these problems the right way, or blindly toss a patch in for the next poor soul 3 years later? I try to do it the right way–I’ll slowly get fast at all the right ways.

The site I’m currently working on is for a big-time music video producer–its theme hasn’t been updated in 3 years, and lives somewhat inharmoniously with an almost-up-to-date WordPress core. It’s slow, and barely responsive (to screens/mobile).

What you came here for:

The client wants this Google Font:
https://fonts.google.com/?query=arapey&selection.family=Arapey

  1. I tried the quick and wrong way first so I could instead work on more important problems. Keyword: tried.

    In /wp-content/themes/aware/header.php towards the top of <head></head> section, insert the code <link href=”https://fonts.googleapis.com/css?family=Arapey” rel=”stylesheet”>

    It is safer to do it towards the top of the head section, ensuring you don’t accidentally place the font-import after the CSS import which calls for the font. That is it say, you want this order of events: (1) Import font, (2) Import CSS file that says, “use this font.”

    Calling for google font above any other style sheets.
    Notice the stylesheet is called immediately after, and definitely outside of any php tags (though you could have it inside of php tags if you echo the <script> statement).

    I then modified /wp-content/themes/aware/styles.css. I searched for font-family in that file and added ‘Poiret One’ and an !important tag. That is: font-family: ‘Poiret One’, Arial, Helvetica, sans-serif !important;

    add the new google font as the first font to use
    Unashamed use of !important.

    But it didn’t work and I didn’t delve into why.

  2. With some quick google-fu, I found some tips on adding the font to this theme the correct way. Update with that in progress.