huong dan chay php tren docker

Hướng dẫn chạy ứng dụng PHP thông qua Docker

Trong bài viết trước chúng tôi đã giới thiệu các bạn về Docker là gì rồi, và trong bài này chúng tôi sẽ hướng dẫn các bạn sử dụng Docker để  chạy ứng dụng PHP

Cài Đặt

Việc cài đặt nó khá là dễ dàng, trong ví dụ này tôi sẽ dùng cài đặt nó trên Fedora nếu các bạn chạy hệ điều hanh nào thì có thể tham khảo tại đây
, mặt định Fedora nó đã có repo Docker do đó bạn chỉ cần chạy lệnh sau:

sudo yum install docker

nếu như hệ thống của bạn có sản Docker rồi thì chỉ cần cập nhật phiên bản mới nhất của nó, chạy lệnh sau:

sudo yum -y update docker

Hello World

Như bao ứng dụng khác ta sẽ chạy ứng dụng đầu tiên là Hello World, chạy lệnh sau

sudo docker run ubuntu:14.04 /bin/echo 'Hello world'
Hello world

Chuyện gì vừa xảy ra, à ta vừa chạy một container có images là phiên bản ubuntu 14.04 , sau đó chạy lệnh echo trong bash shell của linux để
in ra câu Hello world. Chú ý rằng cái images ubuntu 14.04 nó được đăng ký trên Docker Hub

Tương Tác Với Container

Để đăng nhập vào container của images tương ứng ta chỉ cần chạy lệnh sau:

$ sudo docker run -t -i ubuntu:14.04 /bin/bash
root@af8bae53bdd3:/#

ở trên tôi thêm vào 2 tham số -t có nghĩa là tạo 1 cái giả terminal trong container đó còn -i ở đây có nghĩa là cho phép chúng ta tương tác thông qua chuẩn STDIN của container
, ngoài ra Docker có rất nhiều lệnh các bạn có thể tham khảo trên trang chủ.

Commands:
attach    Attach to a running container
build     Build a container from a Dockerfile
commit    Create a new image from a container's changes
cp        Copy files/folders from the containers filesystem to the host path
diff      Inspect changes on a container's filesystem
events    Get real time events from the server
export    Stream the contents of a container as a tar archive
history   Show the history of an image
images    List images
import    Create a new filesystem image from the contents of a tarball
info      Display system-wide information
insert    Insert a file in an image
inspect   Return low-level information on a container
kill      Kill a running container
load      Load an image from a tar archive
login     Register or Login to the docker registry server
logs      Fetch the logs of a container
port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
ps        List containers
pull      Pull an image or a repository from the docker registry server
push      Push an image or a repository to the docker registry server
restart   Restart a running container
rm        Remove one or more containers
rmi       Remove one or more images
run       Run a command in a new container
save      Save an image to a tar archive
search    Search for an image in the docker index
start     Start a stopped container
stop      Stop a running container
tag       Tag an image into a repository
top       Lookup the running processes of a container
version   Show the docker version information
wait      Block until a container stops, then print its exit code

Chạy PHP Trên Docker

Trước tiên ta cần một cái image cơ bản của Docker, trong ví dụ này tôi sữ dụng image là ubuntu, tôi sẽ tạo một tập tin Dockerfile như sau:

FROM ubuntu:trusty
MAINTAINER Thien Tran  <[email protected]>

# Install base packages
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -yq install \
curl \
apache2 \
libapache2-mod-php5 \
php5-mysql \
php5-gd \
php5-curl \
php-pear \
php-apc && \
rm -rf /var/lib/apt/lists/* && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf && \
sed -i "s/variables_order.*/variables_order = \"EGPCS\"/g" /etc/php5/apache2/php.ini

ENV ALLOW_OVERRIDE **False**

# Add image configuration and scripts
ADD run.sh /run.sh
RUN chmod 755 /*.sh

# Configure /app folder with sample app
RUN mkdir -p /app && rm -fr /var/www/html && ln -s /app /var/www/html
ADD sample/ /app

EXPOSE 80
WORKDIR /app
CMD
- /run.sh

để hiểu các tham số trên bạn có thể truy cập tại đây

sau khi có tập tin Dockerfile tôi chạy lệnh sau trong thư mục docker đó

docker build -t="duythien/lamp"  .

lệnh trên có nghĩa là tôi build một images có tên là duythien/lamp, sau khi chạy ta sẽ có kết quả như dưới đây

$ docker build -t="duythien/lamp" .
Sending build context to Docker daemon 119.3 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:trusty
trusty: Pulling from docker.io/ubuntu
e9e06b06e14c: Pull complete
a82efea989f9: Pull complete
37bea4ee0c81: Pull complete
07f8e8c5e660: Already exists
docker.io/ubuntu:trusty: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:014fa1d5b72b4fe0ec2b4642610fbbfdd52f502da8e14e80de07bd1dd774e4ef
Status: Downloaded newer image for docker.io/ubuntu:trusty
---> 07f8e8c5e660
Step 1 : MAINTAINER Fernando Mayo <[email protected]>
---> Running in 103b9b517f46
---> bf5dcab532e6
Removing intermediate container 103b9b517f46
Step 2 : RUN apt-get update &&     DEBIAN_FRONTEND=noninteractive apt-get -yq install         curl         apache2         libapache2-mod-php5         php5-mysql         php5-gd         php5
-curl         php-pear         php-apc &&     rm -rf /var/lib/apt/lists/* &&     curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
---> Running in 39d86308cfb1
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Hit http://archive.ubuntu.com trusty Release.gpg
Get:1 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
Get:2 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
Hit http://archive.ubuntu.com trusty Release

[...]
Removing intermediate container 61c9d38f7e8e
Step 5 : ADD run.sh /run.sh
---> 47f3065f7eb0
Removing intermediate container 0562155b6e0b
Step 6 : RUN chmod 755 /*.sh
---> Running in 2694b0344292
---> aa03ea433c90
Removing intermediate container 2694b0344292
Step 7 : RUN mkdir -p /app && rm -fr /var/www/html && ln -s /app /var/www/html
---> Running in 09c8efa6466b
---> 66710ec9cca7
Removing intermediate container 09c8efa6466b
Step 8 : ADD sample/ /app
---> c05c6081bb70
Removing intermediate container 21b31e37f837
Step 9 : EXPOSE 80
---> Running in 0b3b3a4d053a
---> 247b37c7c21b
Removing intermediate container 0b3b3a4d053a
Step 10 : WORKDIR /app
---> Running in 97048e368bd1
---> 575343d2a782
Removing intermediate container 97048e368bd1
Step 11 : CMD /run.sh
---> Running in e00312d323ae
---> 7ab8c0942a37
Removing intermediate container e00312d323ae
Successfully built 7ab8c0942a37

nhiều lúc trên máy localhost chúng ta có các dịch vụ chạy ở cổng 80, vì vậy để thây đổi port của images docker ta chạy lệnh sau:

docker run -p 80:8080 -d  duythien/lamp

để test thử webserver của bạn có chạy không, bạn chạy lệnh sau đây nếu có chữ hello word thì mọi thứ chạy ok

curl http://localhost:8080

Quá dễ phải không các bạn, trên đây tôi chỉ mới dùng Docker nên chưa thấy sự vượt trội của nó so với Chef, huy vọng tôi sẽ có kinh nghiệm Docker trong thời gian tới đó có thể đánh giá ưu điểm của nó so với cac IT tools:

Kết Luận

Tôi mất khoảng 2 ngày để dùng và cấu hình cơ bản Docker cho PHP nói chung cũng không khó khăn lắm so với sự tưởng tuợng của tôi, huy vọng bạn sẽ thích thú khi dùng Docker. Toàn bộ demo của tôi bạn có thể lấy tại github

Leave a Comment

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Scroll to Top