This document discusses using Docker to deploy PHP projects. It begins with an overview of some common challenges in deploying PHP projects, like different PHP version requirements across projects. It then introduces Docker and some of its key concepts like containers, images, and layered filesystems. The remainder of the document provides examples of basic Docker commands for pulling images, running containers, and listing containers. The goal is to illustrate how Docker can help isolate environments for different PHP projects and more easily manage varying PHP version requirements.
13. One Server Multi Projects
Server
App 1
PHP
MySQL
etc…
App 2
PHP
MySQL
etc…
App n
PHP, DB,
etc…
……
14. One Server Multi Projects
Server
App 1
PHP 5.3.2,
MySQL 5.0.2,
etc…
App 2
PHP 5.4,
MySQL 5.0.15,
etc…
App n
PHP 5.4,
MariaDB,
etc…
……
BUT DIFFERENT
REQUIRMENT
29. How docker can help us?
Built specify environment for target project.
30. How docker can help us?
Built specify environment for target project.!
Isolate every project space.
31. How docker can help us?
Built specify environment for target project.!
Isolate every project space.!
Package it and be portable.
32. How docker can help us?
Built specify environment for target project.!
Isolate every project space.!
Package it and be portable.!
Deploy lightweight and fast.
86. Version
$ docker version
Client version: 1.3.0
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): c78088f
OS/Arch (client): linux/amd64
Server version: 1.3.0
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): c78088f
93. List images
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.10 2185fd50e2ca 28 hours ago 237.2 MB
ubuntu utopic 2185fd50e2ca 28 hours ago 237.2 MB
ubuntu 14.04.1 9cbaf023786c 28 hours ago 192.8 MB
ubuntu 14.04 9cbaf023786c 28 hours ago 192.8 MB
ubuntu trusty 9cbaf023786c 28 hours ago 192.8 MB
ubuntu latest 9cbaf023786c 28 hours ago 192.8 MB
ubuntu precise a9561eb1b190 28 hours ago 120.2 MB
ubuntu 12.04.5 a9561eb1b190 28 hours ago 120.2 MB
ubuntu 12.04 a9561eb1b190 28 hours ago 120.2 MB
ubuntu 12.10 c5881f11ded9 3 months ago 172.2 MB
ubuntu quantal c5881f11ded9 3 months ago 172.2 MB
ubuntu 13.04 463ff6be4238 3 months ago 169.4 MB
ubuntu raring 463ff6be4238 3 months ago 169.4 MB
ubuntu 13.10 195eb90b5349 3 months ago 184.7 MB
ubuntu saucy 195eb90b5349 3 months ago 184.7 MB
……
94. Run and List container
$ docker run ubuntu:trusty /bin/echo "hello, world"
hello, world
95. Run and List container
$ docker run ubuntu:trusty /bin/echo "hello, world"
hello, world
image name
96. Run and List container
$ docker run ubuntu:trusty /bin/echo "hello, world"
hello, world
image name execute
97. Run and List container
$ docker run ubuntu:trusty /bin/echo "hello, world"
hello, world
image name execute argument
98. Run and List container
$ docker run ubuntu:trusty /bin/echo "hello, world"
hello, world
image name execute argument
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73ec6de5630b ubuntu:14.04 "/bin/echo 'hello, w 2 hours ago Exited (0) sad_engelbart
99. Run and List container
$ docker run ubuntu:trusty /bin/echo "hello, world"
hello, world
image name execute argument
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73ec6de5630b ubuntu:14.04 "/bin/echo 'hello, w 2 hours ago Exited (0) sad_engelbart
The container is finished, so we use -a
100. Run and list running container
docker run ubuntu:latest /bin/sh -c
"while true; do echo hello, world; sleep 1; done;"
hello, world
hello, world
……
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e46bc2bd17d ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
101. Run and list running container
docker run ubuntu:latest /bin/sh -c
"while true; do echo hello, world; sleep 1; done;"
hello, world
hello, world
……
The container is running until we stop it
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e46bc2bd17d ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
102. Run running container in deamon
docker run -d ubuntu:latest /bin/sh -c
"while true; do echo hello, world; sleep 1; done;"
90f0ca0a13014ef82f33a2fbef646d8c2cd2b1b260b0d6e71fc46e553560babb
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90f0ca0a1301 ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
103. Run running container in deamon
docker run -d ubuntu:latest /bin/sh -c
"while true; do echo hello, world; sleep 1; done;"
90f0ca0a13014ef82f33a2fbef646d8c2cd2b1b260b0d6e71fc46e553560babb
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90f0ca0a1301 ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
104. Run running container in deamon
docker run -d ubuntu:latest /bin/sh -c
"while true; do echo hello, world; sleep 1; done;"
90f0ca0a13014ef82f33a2fbef646d8c2cd2b1b260b0d6e71fc46e553560babb
We use container id to manage it
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90f0ca0a1301 ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
105. Logs and Attach Container
$ docker logs 90f0ca
hello, world
hello, world
……
$ docker attach 90f0ca
hello, world
hello, world
……
106. Logs and Attach Container
$ docker logs 90f0ca
hello, world
hello, world
……
$ docker attach 90f0ca
hello, world
hello, world
……
Get logs
107. Logs and Attach Container
$ docker logs 90f0ca
hello, world
hello, world
……
$ docker attach 90f0ca
hello, world
hello, world
……
Get logs
Back to container console
108. Logs and Attach Container
$ docker logs 90f0ca
hello, world
hello, world
……
$ docker attach 90f0ca
hello, world
hello, world
……
Get logs
Back to container console
「CTRL-p CTRL-q」 to detach.!
109. Commit
$ docker run -i -t php:5.6.1-apache /bin/bash!
root@4441f36ebab1:/#
You do some change ……
Sets STDIN and open a PTY
110. Commit
$ docker run -i -t php:5.6.1-apache /bin/bash!
You do some change ……
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse
111. Commit
$ docker run -i -t php:5.6.1-apache /bin/bash!
You do some change ……
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse
$ docker commit 6a495e871389 fntsr/first-image!
2006ba62eb6508560ae38782ee514a4212d29ea4e4a2e7495b1eedf155fe13df
112. Commit
$ docker run -i -t php:5.6.1-apache /bin/bash!
You do some change ……
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse
$ docker commit 6a495e871389 fntsr/first-image!
2006ba62eb6508560ae38782ee514a4212d29ea4e4a2e7495b1eedf155fe13df
<username>/<image_name>
113. Commit
$ docker run -i -t php:5.6.1-apache /bin/bash!
You do some change ……
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse
$ docker commit 6a495e871389 fntsr/first-image!
2006ba62eb6508560ae38782ee514a4212d29ea4e4a2e7495b1eedf155fe13df
$ docker images!
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
fntsr/first-image latest 2006ba62eb65 22 minutes ago 803.6 MB
114. Expose Port
docker run -d -p 80 nginx
c63fda2248d0dcf90a90cc817d1acb6c9546db841128ca7558ff1f496ad4f8fb
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
c63fda2248d0 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:49153->80/tcp
115. Expose Port
-p <guest_port>!
docker run -d -p 80 nginx
c63fda2248d0dcf90a90cc817d1acb6c9546db841128ca7558ff1f496ad4f8fb
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
c63fda2248d0 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:49153->80/tcp
116. Expose Port
-p <guest_port>!
docker run -d -p 80 nginx
c63fda2248d0dcf90a90cc817d1acb6c9546db841128ca7558ff1f496ad4f8fb
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
c63fda2248d0 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:49153->80/tcp
Expose 80 to 49153
(random)
Expose to random port
117. Expose Port
docker run -d -p 10080:80 nginx
afd45d0f7d34baf90dbef109ae167c99a49432080adc8621e8cd7e18a4004658
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
afd45d0f7d34 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:10080->80/tcp
118. Expose Port
-p <host_port>:<guest_port>
docker run -d -p 10080:80 nginx
afd45d0f7d34baf90dbef109ae167c99a49432080adc8621e8cd7e18a4004658
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
afd45d0f7d34 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:10080->80/tcp
119. Expose Port
-p <host_port>:<guest_port>
docker run -d -p 10080:80 nginx
afd45d0f7d34baf90dbef109ae167c99a49432080adc8621e8cd7e18a4004658
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
afd45d0f7d34 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:10080->80/tcp
Expose 80 to 10080
(assigned)
Expose to assigned port
120. Expose Port
docker run -d -p 127.0.0.1:10080:80 nginx
76d00613ccb3576ef604c61f4d086aa6f090ed7268c42638f1931e235c0fe526
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
76d00613ccb3 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 127.0.0.1:10080->80/tcp
121. Expose Port
-p <hostname>:<host_port>:<guest_port>
docker run -d -p 127.0.0.1:10080:80 nginx
76d00613ccb3576ef604c61f4d086aa6f090ed7268c42638f1931e235c0fe526
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
76d00613ccb3 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 127.0.0.1:10080->80/tcp
122. Expose Port
Expose to host with hostname
-p <hostname>:<host_port>:<guest_port>
docker run -d -p 127.0.0.1:10080:80 nginx
76d00613ccb3576ef604c61f4d086aa6f090ed7268c42638f1931e235c0fe526
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
76d00613ccb3 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 127.0.0.1:10080->80/tcp
Expose with host name
123. Expose Port
docker run -d -P nginx
2123f55d26fc3d5824492ed8ad4d7f9e28ed5d749e7d401e21a1bb0002f65048
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED PORTS
afd45d0f7d34 nginx:1 "nginx… Up 5 seconds 0.0.0.0:49154->443/tcp, 0.0.0.0:49155->80/tcp
124. Expose Port
-P
docker run -d -P nginx
2123f55d26fc3d5824492ed8ad4d7f9e28ed5d749e7d401e21a1bb0002f65048
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED PORTS
afd45d0f7d34 nginx:1 "nginx… Up 5 seconds 0.0.0.0:49154->443/tcp, 0.0.0.0:49155->80/tcp
125. Expose Port
-P
Expose all port of container !
with host random port
docker run -d -P nginx
2123f55d26fc3d5824492ed8ad4d7f9e28ed5d749e7d401e21a1bb0002f65048
$ docker ps -a!
CONTAINER ID IMAGE COMMAND CREATED PORTS
afd45d0f7d34 nginx:1 "nginx… Up 5 seconds 0.0.0.0:49154->443/tcp, 0.0.0.0:49155->80/tcp
443 expose to 49154
(random)
126. How could I back to running
container and do another thing?
• attach!
127. How could I back to running
container and do another thing?
• attach! It can only run one process.
128. How could I back to running
container and do another thing?
• attach!
• exec
It can only run one process.
129. How could I back to running
container and do another thing?
• attach!
• exec
It can only run one process.
It's added on docker 1.3 (rock)
130. Alias
$ docker run nginx --name my-nginx -d nginx
4441f36ebab1975004782ea6d03198fd8ced8472cf34721f9906cbebfe61872d"
131. Alias
$ docker run nginx --name my-nginx -d nginx
4441f36ebab1975004782ea6d03198fd8ced8472cf34721f9906cbebfe61872d"
$ docker exec -it nginx bin/bash
CONTAINER ID IMAGE …… STATUS PORTS NAMES
4441f36ebab1 nginx:latest Up 4 seconds 443/tcp, 0.0.0.0:80->80/tcp my-nginx
132. $ docker run nginx --name my-nginx -d nginx
4441f36ebab1975004782ea6d03198fd8ced8472cf34721f9906cbebfe61872d"
$ docker exec -it my-nginx bin/bash
root@4441f36ebab1:/#
Alias
$ docker exec -it nginx bin/bash
CONTAINER ID IMAGE …… STATUS PORTS NAMES
4441f36ebab1 nginx:latest Up 4 seconds 443/tcp, 0.0.0.0:80->80/tcp my-nginx
159. Single app container
Volume
PHP App
/var/www/html PHP 5.6
…… Nginx
MySQL
……
Port
160. Single app container
Volume
PHP App
/var/www/html PHP 5.6
…… Nginx
MySQL
……
Port
Not recommend use for production
161. Container Groups
Volume /var/www/html
PHP App
…… Nginx
/var/lib/mysql/
……
PHP 5.6
MySQL
……
App Container
DB Container
Port
Port
Easy to manage
162. Separate containers
FS nginx container
FS
php container
…… PHP-apache
FS MySQL container
…… MySQL
Volume
PHP App
……
Link
Link
Port
…… Nginx
volume
Using in production
163. Data Only Container
Volume /data
8421917fbed8aa018ad3….
……
/data
……
……
DB-data Container
volume from
DB Container
volume