Docker – Beginner’s Notes

Well, after a year returning to Docker, I can still be considered as a beginner since I forgot almost things that I did before :shame: So this entry, Docker – Beginner’s Notes, is just a way that helps me in the future (hopefully).

  1. Useful commands to remember: docker images, docker ps -a, docker rm <container_name>, docker rmi <image_name>
  2. For docker-compose, useful commands are: docker-compose up -d, docker-compose down, docker-compose stop/start
  3. If you want to enter a container, use docker exec -ti as follows:

    [bash]docker exec -ti <container_name> /bin/bash[/bash]

  4. If you use PDO for connecting to MySQL, remember to include it into your PHP build environment (docker-php-ext-install pdo pdo_mysql), otherwise you will not be able to connect to your DB server.
  5. In case we want to update the docker containers to latest version, we will need to go with 2 steps
    1. Pull the latest image version. E.g.
      docker pull phpmyadmin/phpmyadmin
    2. Remove the old containers and run docker again to force docker to rebuild containers based on new images. If you are using docker-compose, then we can simply rebuild all containers with
      docker-compose build
    3. Remove old docker images. You can use docker images to list all available images and remove out-dated ones with the following command
      docker rmi IMAGE_ID

       

My sample docker-compose.yml is as follows:

version: "2"
services:
  nginx:
    build: ./nginx/
    ports:
      - 80:80
    volumes_from:
      - app
    networks:
      - server
    depends_on:
      - php
  php:
    build: ./php/
    expose:
      - 9000
    volumes_from:
      - app
    networks:
      - server
      - database
    depends_on:
      - mysql
  app:
    image: php:7.1-fpm
    volumes:
      - ${HOST_WEB_ROOT}:/var/www/html
    command: "true"
  mysql:
    build: ./mysql/
    ports:
      - 3306:3306
    volumes:
      - ${MYSQL_DB_ROOT}:/var/lib/mysql
      - ${HOST_WEB_ROOT}:/var/www/html
    networks:
      - database
    environment:
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${DB_NAME}"
      MYSQL_USER: "${DB_USERNAME}"
      MYSQL_PASSWORD: "${DB_PASSWORD}"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    networks:
      - database
    depends_on:
      - mysql
    environment:
      PMA_HOST: mysql
volumes:
  data:
networks:
  database:
  server:
    driver: bridge

Sample ./php/Dockerfile is as follows:

[bash]FROM php:7.1-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libxml2-dev \
curl libcurl4-openssl-dev apt-utils wget nano telnet \
&& sed -i "s|;*listen\s*=\s*127.0.0.1:9000|listen = 9000|g" /usr/local/etc/php-fpm.d/www.conf \
&& sed -e ‘s/;clear_env = no/clear_env = no/’ -i /usr/local/etc/php-fpm.d/www.conf \
&& docker-php-ext-install -j$(nproc) iconv mcrypt mysqli mbstring opcache \
&& docker-php-ext-install -j$(nproc) gd xml json pdo pdo_mysql zip curl \
&& pecl install redis && docker-php-ext-enable redis
RUN curl -sS https://getcomposer.org/installer | php — –install-dir=/usr/local/bin –filename=composer
RUN composer global require "laravel/installer"
## NodeJS, NPM — Install NodeJS
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash – && apt-get install -y nodejs
ENV PATH ${PATH}:~/.composer/vendor/bin
COPY ./ext/ioncube_loader_lin_7.1.so /usr/local/lib/php/extensions/no-debug-non-zts-20160303
COPY ./ext/00_docker-php-ext-ioncube_loader.ini /usr/local/etc/php/conf.d/
COPY ./docker-php-settings-uploads.ini /usr/local/etc/php/conf.d/
[/bash]

If we choose apache as the webserver instead of nginx, the sample docker-compose-apache.yml is as follows:

version: "2"
services:
  php_apache:
    build: ./php-apache/
    ports:
      - 80:80
    volumes:
      - ${HOST_WEB_ROOT}:/var/www/html
    networks:
      - server
      - database
    depends_on:
      - mysql
  mysql:
    build: ./mysql/
    ports:
      - 3306:3306
    volumes:
      - ${MYSQL_DB_ROOT}:/var/lib/mysql
      - ${HOST_WEB_ROOT}:/var/www/html
    networks:
      - database
    environment:
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${DB_NAME}"
      MYSQL_USER: "${DB_USERNAME}"
      MYSQL_PASSWORD: "${DB_PASSWORD}"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    networks:
      - database
    depends_on:
      - mysql
    environment:
      PMA_HOST: mysql
volumes:
  data:
networks:
  database:
  server:
    driver: bridge

Leave a comment

Your email address will not be published. Required fields are marked *