JoBins採用企業サイト JoBinsエージェントサイト

アカウントをお持ちの方はこちら 採用企業ログイン
エージェントログイン


background

Docker for web development

Feb 1, 2018


As we know that web projects are not application dependencies and in most of the cases the local development environment is different from production so sometimes in production environment we will get issues in the system so I think docker is one of the best solutions to overcome environment variation problem and we don't have to install local servers like XAMPP, LAMP, WAMP etc according to local machine which eliminates the issues on code when cooperating with others developers . In a simple way, docker is open source tool to locally manage the development environment as our production environment virtually and can efficiently build apps that run consistently on any machine. It is a tool for running containers on Linux making the deployment of apps inside the container.The same container that created at the development environment can run in production (on VM, in the cloud).

Installation of docker for windows

Download docker windows installer from docker official site
Install the docker on your local machine
After successful installation run the docker as administrator

After running the docker you can check by following ways
Open the Windows PowerShell as administrator
Run docker run hello-world to verify that docker can pull sample image from docker hub and run images and latest pulled images can be checked by command docker images. To become more clear about docker images and container concept you can learn from docker official webpage.

For testing docker locally in the windows environment we are integrating laravel with MYSQL.

For this application, we are going to create3 containers i.e
for PHP, Nginx server and for MYSQL database

To install laravel application if you have already installed composer in your pc locally open the windows command line and go to drive where you want to setup laravel project and run the command laravel new dockerTest (dockerTest is project name). After successfully install the laravel in our drive lets create docker setup files inside root of our project directory.

To build the envionment we have to create 4 files inside our dockerTest folder they are app.dockerfile, web.dockerfile, docker-compose.yml and vhost.conf.

 

app.dockerfile

app.dockerfile is application file to define a configuration of PHP that we are going to use.

FROM php:7.0.4-fpm
RUN apt-get update && docker-php-ext-install mbstring pdo pdo_mysql

Note: PHP:7.0.4-fpm is used from where we can find other PHP versions in docker hub repository and from CRUD we install docker PHP extension and mbstring

 

web.dockerfile

FROM nginx:1.10
ADD vhost.conf /etc/nginx/conf.d/default.conf

Note: nginx:1.10 is open source web server for HTTP,HTTPS,SMTP, POP3 and IMAP protocols. In the second line we add local vhost.conf file to our server.

 

vhost.conf

server {
listen 80;
index index.php index.html;
root /var/www/public;   location / {
try_files $uri /index.php?$args;
} location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

Note: in vhost file, we handle PHP request and define root path and index file. For detail information, you can check in nginx official page.

Now we need docker-compose.yml file to build the Docker container

docker-compose.yml

version: "3"
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database" # The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
ports:
- 8080:80 # The Database
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306" volumes:
dbdata:

Notes

  • Here we have to define PHP, web server and MYSQL. After we run the compose file it will automatically configure the web server, PHP, and database by defining a separate container for each.
  • we have to define volumes that means the container mounted to our local development machine. Volumes are for persisting data generated by and used by the container.
  • we map port for each host container. ports 8080:80 means mapping port 80 to hosting server and we can access locally from localhost:8080 or 0.0.0.0:8080 likewise we have to define ports for PHP and MYSQL.
  • we have define MySQL database name, mysql_user, mysql_password same as laravel default configuration for easy but database information can be changed.

Now our docker-compose.yml file is ready to build the container services.
Note: To install all services successfully we need to check the formatting of the yml file. There online yml checker from where we can confirm whether there is format error in our compose file or not
link for yml checker: http://yaml-online-parser.appspot.com/

After checking yml file lets build the environment and start the services

Open the Windows PowerShell as administrator and go to your project directory and run the following command

docker-compose up --build

After successfully build the container you can check your container by

docker ps

docker container sample

the above command list all the running container and we can all the build images by

docker images

docker images sample

Before checking the application we have to generate the artisan key in laravel

docker-compose exec app PHP artisan key: generate

Note we use "app" after exec because "app" is our service name that MySQL in docker-compose.yml file.

Now check your application from localhost:8080 you will laravel home page.
Note: sometime there will be permission issue error in server storage folder if there is such error happen you have to add permission in laravel storage folder

cmd to access enter the Linux server

docker exec -ti CONTAINER NAME /bin/sh

Eg docker exec -ti laradoc_web_1

after entering the bin path run the chmod command

chmod 777 storage

Let's create users table from migrations table

docker-compose exec app PHP artisan make:migration create_users_table

this above command create users table in our database and we can access database and modify the database from any MySQL client tool like: MySQL workbench

Connection
host: our pc ip (192.168.11.11)
database:homestead
user: root
password: secret

Notes: Command to start, stop and remove the container
cmd to list running containers

docker ps


cmd to list stop containers

docker ps -a


cmd to start a container

docker start CONTAINER NAME


cmd to stop a container

docker stop CONTAINER NAME


cmd to remove a container

docker rm CONTAINER IMAGE