This article is going to show steps and configurations on how to build a development box (PHP CodeIgniter + Apache + PostgreSQL) using Docker on Windows.
Why Docker
- Comparing to VirtualBox, it’s a lot easier and probably faster
- Portable.
Files to prepare
1. Install Docker for Windows. Unfortunately native Docker is only available on Windows10 so all other Windows have to use Docker Toolbox which is based on VirtualBox.
2. Prepare your folder structure for your project. Note when using “Docker Toolbox”, you’ll need to put your project folder under your home folder which is normally c:\users\USERNAME on Windows (Native Docker allows you to change this to other place)
Below is my project folder
3. Create files
- Dockerfile
FROM php:7.2.5-apache MAINTAINER YourName@Tarwis Tech COPY . /var/www/tarwis #copy a custom php.ini COPY config/php.ini /usr/local/etc/php/ COPY docker/php/vhost.conf /etc/apache2/sites-available/000-default.conf WORKDIR /var/www/tarwis RUN apt-get update && apt-get install -y libpq-dev RUN docker-php-ext-configure pgsql --with-pgsql=/usr/local/pgsql RUN docker-php-ext-install pgsql RUN docker-php-ext-install pdo_pgsql #below is critical if you run on Windows host but want to change user 1000 to www-data! RUN usermod -u 1000 www-data RUN chown -R www-data:www-data /var/www/tarwis RUN a2enmod rewrite
- Apache vhost.conf
<VirtualHost *:80> DocumentRoot /var/www/tarwis <Directory "/var/www/tarwis"> AllowOverride all Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
- Docker-compose.yml
version: '3' services: app: build: context: . dockerfile: docker/php/Dockerfile image: ci-docker container_name: tarwis_crm_container ports: - "8080:80" volumes: #bind mount app to container folder /var/www/tarwis - ./app:/var/www/tarwis #by defining the same "user defined network", all containers can talk to each other #do NOT use link as it may not be supported in future networks: - appnet environment: #env variables, used by your application DB_HOSTNAME: postgres #this points to the postgres db's name DB_DATABASE: tarwis_crm DB_PORT: 5432 DB_USERNAME: yourDBUser DB_PASSWORD: yourDBUserPassword postgres: image: "postgres:10.4" #when container_name is specified, as #container name is always unique you cannot scale a service beyond 1 container container_name: pg_container restart: always environment: #specify db name POSTGRES_DB: tarwis_crm #default is postgres but custom it here for clarity POSTGRES_USER: postgres POSTGRES_PASSWORD: hardToGuess ports: - "15432:5432" #by defining the same "user defined network", all containers can talk to each other networks: - appnet #definition of all networks networks: #with default settings (which is a load-balanced overlay network) appnet: #no need of volumes #volumes: # app-vol: # external: true
4. Build Docker images.
docker-compose build
5. Start Docker container. Docker container is a running instance of a Docker image. Load balancing is supported by running multiple containers of the same image (we don’t use this feature here)
docker-compose up
6. Done
7. Common commands
- docker-compose build
- docker-compose build
- –list Docker containers
- docker ps
- –connect to a Docker container
- docker exec -it container_name bash