Connect to a locally installed database from a Docker container

Docker has its own networking space, trying to connect to the local IP Address of the container won’t work when the database is on the host machine.

Normally you would provide the hostname, ip address or use localhost if the database is installed on the same machine locally ie 127.0.0.1. But when the database is installed on the host machine that docker is running on top of we need a slightly different approach

What we need to do is add a loopback ip address to our local network card and connect the container to the loopback address

sudo ifconfig lo0 alias x.x.x.x up

sudo ip addr add x.x.x.x dev lo label lo:1

Setting Up Docker on AWS EC2

Install Docker On Ubuntu 18.04 Bionic Beaver

Aim

Install Docker production ready on Ubuntu 18.04 LTS Bionic Beaver AWS EC2 instance

Requirements

AWS EC2 Account

Level

Easy

Introduction

AWS and Docker have revolutionised how web applications are hosted and managed in the cloud and how they are developed and deployed to customers.

Docker allows developers to containerise their applications with the power of virtual machines, but without the weight, they are easier to deploy and move from dev, staging, qa and production having a consistent environment across all platforms and stages.

Installing Docker on Ubuntu hosted by AWS is simple.

Install Docker

First you need to ssh onto the EC2 instance, then run the following commands to update the package manager and install docker

sudo apt-get update
sudo apt-get install docker.io

Start the docker daemon and enable the service to start on boot

sudo systemctl start docker
sudo systemctl enable docker

It’s recommended to add the current user to the docker group. This will prevent you running into permissions issues when running docker commands or having to run the commands as sudo

sudo usermod -a -G docker $USER

Running a container

docker run -d -p 80:80 dockerup/quicksite

This will start an engine server with a single landing page.
Make sure port 80 is open on your firewall by checking the security group assigned to your EC2 instance, you can then load the site by browsing to http://<EC2PublicIpAddress>

Working with Containers

Working with containers is pretty straightforward. The aboveruncommand along with the -d flag detaches the container, so it’s not taking up your terminal and starts the internal process in this case nginx and binds the container to port 80

Since you can have multiple containers either running at the same time or terminated, its good to be able to keep track of them all. To list out all the running containers on your system, use the ps option. Use the -a flag to see all containers either stopped or running

docker ps -a

If you want to stop a container, use stop followed by the hash or name of the container.

docker stop <CONTAINER NAME OR HASH> 

Removing a container is easy too. Use rm.

docker rm <CONTAINER NAME OR HASH>

If you would like to stop and remove all containers you can combine commands

docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

Summary

Combining Docker and a cloud provider like AWS is very powerful. AWS and the Docker ecosystem provide far more options to managing container environments shown here, and Docker is capable of a wide range of different configurations. With the details here, you can at least get started with Docker, AWS and Ubuntu and use it to configure a production ready Docker server for launching your apps

Docker Official PHP container how to enable PDO

The Official PHP & Apache[1] container is a great starting point for a local development environment. One thing that might trip you up is if you try to install or run queries against a database.

Out of the box, PDO drivers are not enabled. This will likely resort to the following errors:

[PDOException]  could not find driver

This might lead you to think you need to install the driver via your own Dockerfile. Using the following command:

RUN sudo apt-get update && apt-get install -y mysql7.0-mysql

Which quickly results in too the following list of error.

Package php7.0-mysql is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package ‘php7.0-mysql’ has no installation candidate
ERROR: Service ‘web’ failed to build: The command ‘/bin/sh -c apt-get update && apt-get install -y php7.0-mysql’ returned a non-zero code: 100

The PHP container doesn’t have a php.ini file either, so why not copy one and uncomment the line: extension=php_pdo_mysql.dll

Well, this is likely to end up with the following error:

HP Warning: PHP Startup: Unable to load dynamic library ‘php_pdo_mysql.dll’ (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20170718/php_pdo_mysql.dll (/usr/local/lib/php/extensions/no-debug-non-zts-20170718/php_pdo_mysql.dll: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20170718/php_pdo_mysql.dll.so (/usr/local/lib/php/extensions/no-debug-non-zts-20170718/php_pdo_mysql.dll.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

So whats the solution? Turns out you need to use the docker-php-ext-install option, via Dockerfile built FROM the php container.

Your Dockerfile would look something similar to:

FROM php:7.2-apache
RUN docker-php-ext-install pdo pdo_mysql

Links

[1] https://hub.docker.com/_/php/