Docker :: Docker-compose example

One example with a Standard Web application

1- Install Docker Compose … …………….. not include in Docker

  • curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  • chmod +x /usr/local/bin/docker-compose

 

2- Launch Docker- compose with the command

Docker-compose up -d

 

3- The file docker-compose.yml

# Adopt version 3 syntax:
# https://docs.docker.com/compose/compose-file/#/versioning
version: ‘3’

services:
###########################
# Setup the Apache container
###########################
httpd:
container_name: apache_docker
restart: always
image: httpd:2.4.38
ports:
– 80:80
volumes:
– ./apache/httpd.conf:/usr/local/apache2/conf/httpd.conf
– ./apache/vhosts/:/usr/local/apache2/conf/vhosts
depends_on:
– php

###########################
# Setup the PHP container ( with a Dockerfile  )
###########################
php:
container_name: php_docker
restart: always
build: ./php/
expose:
– 9000
volumes:
– ./www/:/usr/local/apache2/htdocs
– ./php/ssmtp.conf:/etc/ssmtp/ssmtp.conf:ro
– ./php/php-mail.conf:/usr/local/etc/php/conf.d/mail.ini:ro
depends_on:
– mysql

###########################
# Setup the MySQL container
###########################
mysql:
container_name: mysql_docker
restart: always
image: mysql:8.0.15
ports:
– 3306:3306
volumes:
– ./mysql/data2:/var/lib/mysql
– ./mysql/conf-mysql.cnf:/etc/mysql/mysql.conf.d/conf-mysql.cnf:ro
environment:
MYSQL_ROOT_PASSWORD: iado
MYSQL_DATABASE: iado
MYSQL_USER: project
MYSQL_PASSWORD: project

###########################
# Setup the phpmyadmin
###########################

phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: z_phpmyadmin
ports:
– 8080:80
links:
– mysql

 

4- the Dockerfile for PHP 

FROM php:7.1-apache

# Get repository and install wget and vim
RUN apt-get update && apt-get install –no-install-recommends -y \
wget \
gnupg \
git \
unzip

# Install PHP extensions deps
RUN apt-get update \
&& apt-get install –no-install-recommends -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
zlib1g-dev \
libicu-dev \
g++ \
unixodbc-dev \
libxml2-dev \
libaio-dev \
libmemcached-dev \
freetds-dev \
libssl-dev \
openssl

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php — \
–install-dir=/usr/local/bin \
–filename=composer

# Install PHP extensions
RUN docker-php-ext-configure gd –with-freetype-dir=/usr/include/ –with-jpeg-dir=/usr/include/ \
&& docker-php-ext-configure pdo_dblib –with-libdir=/lib/x86_64-linux-gnu \
&& pecl install sqlsrv-4.1.6.1 \
&& pecl install pdo_sqlsrv-4.1.6.1 \
&& pecl install redis \
&& pecl install memcached \
&& pecl install xdebug \
&& docker-php-ext-install \
iconv \
mbstring \
intl \
mcrypt \
gd \
mysqli \
pdo_mysql \
pdo_dblib \
soap \
sockets \
zip \
pcntl \
ftp \
&& docker-php-ext-enable \
sqlsrv \
pdo_sqlsrv \
redis \
memcached \
opcache \
xdebug

# Install APCu and APC backward compatibility
RUN pecl install apcu \
&& pecl install apcu_bc-1.0.3 \
&& docker-php-ext-enable apcu –ini-name 10-docker-php-ext-apcu.ini \
&& docker-php-ext-enable apc –ini-name 20-docker-php-ext-apc.ini

# Clean repository
RUN apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# The path that will be used to make Apache run under that user
ENV VOLUME_PATH /var/www/html/public

# Move files
COPY src/ /var/www/html/

COPY vhost.cnf /etc/apache2/sites-available/000-default.conf

WORKDIR /var/www/html/

RUN chown -R www-data:www-data /var/www/html/ \
&& a2enmod rewrite

 

5- Some commands usefull :

docker-compose down
docker rm $(docker ps -a -q)

 

Docker: Modify the current value of Apache LogLevel directive within the configuration file

1. Run the Apache web server container

Running the httpd container
$ docker run -d httpd:latest

 

2. Get the id of the running container

Output from `docker ps`
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
801e3b4a29bd httpd:latest "httpd-foreground" 16 seconds ago Up 14 seconds 80/tcp condescending_easley
35b5493e239c rancher/server "/usr/bin/entry /u..." 2 months ago Up 15 hours 3306/tcp, 0.0.0.0:8080->8080/tcp keen_agnesi

A simple docker ps will list the running containers and from there you can get the container id.
You can see that the id of the container we want begins with 801e…

3. Open a shell in the running container

Open a bash shell
# docker exec -it 801e /bin/bash
root@801e3b4a29bd:/usr/local/apache2#

Once the command is executed you enter a root shell within the container (shown by the presence of ‘root’ and ‘#’).

4. Check the current value of the LogLevel directive within the configuration file

Check the LogLevel
root@801e3b4a29bd:/usr/local/apache2# cat conf/httpd.conf | grep -i loglevel
# LogLevel: Control the number of messages logged to the error_log.
LogLevel warn

Explanation of the command:
cat conf/httpd.conf | grep -i loglevel.
cat conf/httpd.conf prints the content of the file conf/httpd.conf.
The | (pipe) redirects the output printed by cat to the next command which is grep.
grep is called with the argument loglevel and the flag -i.
The -i flag makes sure the loglevel string is treated in a case-insensitive way by grep.
The command results in all lines with any occurrence of loglevel (case-insensitive) being printed to the screen.
From the output we can see that the LogLevel is set to warn – whereas we would like it set to debug.

5. Use sed to search and replace the line with what we want

root@801e3b4a29bd:/usr/local/apache2# sed 's/LogLevel warn/LogLevel debug/' conf/httpd.conf > conf/httpd.conf.changed && mv conf/httpd.conf.changed conf/httpd.conf

Most likely, as is in this case, the container will not have a text editor installed – even vi and nano will not present. As a result we use sed (which is installed) to make the change.
An alternative would be to use your container’s package manager to install an editor. For example if the container is Debian based you could run apt-get update && apt-get install vim to install vim . Once installed you could use vim to edit the file.
Explanation of the command:
sed ‘s/LogLevel warn/LogLevel debug/’ conf/httpd.conf …
Substitute LogLevel warn with LogLevel debug in the contents of the file conf/httpd.conf. Note sed just streams this substitution to stdout so it still needs to be written to disk to persist.
… > conf/httpd.conf.changed && mv conf/httpd.conf.changed conf/httpd.conf
Write the output from the substitution to the file conf/httpd.conf.changed (it could be called anything) and then rename (move) the file so it overwrites the original. This is done to avoid the issue of creating an empty file as output (as would be the case if we wrote to the same file we read in from).
Essentially this just replaces the occurrence of LogLevel warn with LogLevel debug in the conf/httpd.conf file .

6. Check that your file change has been made correctly

LogLevel has been changed
root@801e3b4a29bd:/usr/local/apache2# cat conf/httpd.conf | grep -i LogLevel
# LogLevel: Control the number of messages logged to the error_log.
LogLevel debug

We can see from the output the LogLevel has been changed from warn to debug as we wanted.

7. Exit the shell (also exits the container)

Exit the shell and container
root@801e3b4a29bd:/usr/local/apache2# exit
exit

8. Restart the container for the changes to take effect

Restart the container
$ docker restart 801e

The actual file change occurs immediately within the container but the global configuration file for the Apache server (httpd.conf) is only read when the server starts.
As a result we need to restart the server/container for the changes to take effect.

Docker :: How to work with your Favorite Editor into a Container ?

Java Developers  don’t like vi  editor … 2  easy options  to work with netbeans or eclipse and git

Option 1: Using Shared Volumes

Docker allows for mounting local directories into containers using the shared volumes feature.

Just use the -v switch to specify the local directory path that you wish to mount,  along with the location where it should be mounted within the running container:

docker run -d -P –name <name of your container> -v /path/to/local/directory:/path/to/container/directory <image name> …

Using this command, the host’s directory becomes accessible to the container under the path you specify.
This is particularly useful when developing locally, as you can use your favorite editor to work locally, commit code to Git, and pull the latest code from remote branches.

Your application will run inside a container,
isolating it away from any processes you have running on your development laptop.  The container instance will also have access to other instances, such as those running to provide databases, message brokers and other services.

You can read more about how volumes work from the Docker user guide.

In this scenario, all containers on the same host would share the same shared codebase and binaries at the same time.
Versioning of code should occur within the Docker image, not at the host volume.
Therefore, it’s not recommended to use shared volumes in production.

Option 2: Using the ADD or COPY command

You can use the COPY command within a Dockerfile to copy files from the local filesystem into a specific directory within the container.

The following Dockerfile example would recursively add the current working directory into the /app directory of the container image:

# Dockerfile for a Ruby 2.6 container

FROM ruby:2.6

RUN mkdir /app
COPY . /app

The ADD command is similar to the COPY command, but has the added advantage of fetching remote URLs and extracting tarballs.

 

To access the port 81 —>  http://localhost:81/

docker run -d -p 81:80 -v /path/to/local/directory:/path/to/container/directory <image name> ...

To access the port 82 —>  http://localhost:82/

docker run -d -p 82:80 -v /path/to/local/directory:/path/to/container/directory <image name> ...