Docker(2)Repositories and PHP example on CentOS
1. Repositories
for example, dl.dockerpool.com/ubuntu, dl.dockerpool.com is a registry server, ubuntu is the repository name.
Here is my private repo
https://registry.hub.docker.com/u/sillycat/machinelearning/
Here is my public repo
https://registry.hub.docker.com/u/sillycat/public/
Login the docker hub
> sudo docker login
Username: sillycat
Password:
Email:
[email protected]
Login Succeeded
The information will be stored here
> sudo vi ~/.dockercfg
Build the image
> sudo docker build -t="sillycat/public" .
List the info
> sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sillycat/public latest 8800fd804e5c About a minute ago 301 MB
This will push the images to my repo
> sudo docker push sillycat/public
Pull the images
> sudo docker pull sillycat/public
For private repo with tag
> sudo docker build -t="sillycat/machinelearning:v1.0" .
> sudo docker push sillycat/machinelearning
> sudo docker pull sillycat/machinelearning:v1.0
2. Example to Build PHP Env on CentOS
Pull the images
> sudo docker pull centos
Start the container for trying purpose
> sudo docker run -t -i centos:7 /bin/bash
Build the Image for PHP
> sudo docker build -t sillycat/public:centos7-php .
Remove the images with their IMAGE ID
> sudo docker rmi -f 70f8f9ad6d7f
List the current running containers
> sudo docker ps
Stop the container by their names
> sudo docker stop grave_morse
Check the IP address for a container
> sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' centos7-php
Inspect all the data for one container
> sudo docker inspect centos7-nginx
Introduce some key things in sillycat-docker project
for centos7-php
We have a docker file Dockerfile, the content will be something like this:
FROM centos:7
#PHP 5.6.10 and PHP-FPM
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
RUN yum install -y php56w php56w-fpm
RUN yum install -y telnet
# Add in the php fpm configuration
ADD www.conf /etc/php-fpm.d/
#Prepare the Content
RUN mkdir -p /app/htdocs
ADD htdocs/ /app/htdocs/
#VOLUME /app/htdocs
#Prepare the shell
ADD start.sh /app/
#excute the shell
CMD /app/start.sh
This will prepare the system, pre-install a lot of software for me and copy the binary to work directory and start the application at last.
The start.sh will be just simple command to start the php-fpm
#!/bin/sh -ex
php-fpm --nodaemonize
The most import part to make www.conf work for php-fpm is the listen configuration
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen=9000
It will listen to all the client IP addresses.
Under the htdocs/index.php, it is just a really simple hello word.
<?php
echo "Hello, sillycat!\n\n\n";
phpinfo();
?>
And the Makefile Command file really help me, it is hard to remember all the options and command in docker,
IMAGE=sillycat/public
TAG=centos7-php
NAME=centos7-php
docker-context:
build: docker-context
sudo docker build -t $(IMAGE):$(TAG) .
run:
sudo docker run -d --name $(NAME) $(IMAGE):$(TAG)
run-volume:
sudo docker run -d --name $(NAME) -v $(shell pwd)/htdocs:/app/htdocs:ro $(IMAGE):$(TAG)
# docker run -ti --name $(NAME) -v $(shell pwd)/htdocs:/app/htdocs:ro $(IMAGE):$(TAG) /bin/bash
debug:
sudo docker run -ti --name $(NAME) $(IMAGE):$(TAG) /bin/bash
clean:
sudo docker stop ${NAME}
sudo docker rm ${NAME}
logs:
sudo docker logs ${NAME}
publish:
sudo docker push ${IMAGE}
These command will do the build, run, debug and clean publish.
> make build
> make publish
Little new things for nginx to running on docker
php-fpm.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /app/htdocs;
fastcgi_pass centos7-php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Dockerfile
#fetch and pick the OS first
FROM centos:7
#Install nginx
RUN yum install -y epel-release
RUN yum install -y nginx
RUN yum install -y telnet
EXPOSE 80
ADD php-fpm.conf /etc/nginx/default.d/
RUN mkdir -p /app/
ADD start.sh /app/
CMD /app/start.sh
Then we can visit this page URLs to verify
http://ubuntu-pilot:8080/index.php
http://ubuntu-pilot:8080
May read more things on VOLUME and WORKDIR in the future.
Compile and Run on Dockerfile is working as well.
#fetch and pick the OS first
FROM centos:7
#prepare build env
RUN yum install -y wget gcc make
#prepare libraries for nginx
RUN yum install -y zlib-devel pcre-devel
#download resource and build and install nginx
RUN mkdir /install/
WORKDIR /install/
RUN wget http://nginx.org/download/nginx-1.9.2.tar.gz
RUN tar zxvf nginx-1.9.2.tar.gz
WORKDIR /install/nginx-1.9.2
RUN ./configure --prefix=/tool/nginx-1.9.2
RUN make && make install
EXPOSE 80
ADD nginx.conf /tool/nginx-1.9.2/conf/
RUN mkdir -p /app/
ADD start.sh /app/
CMD /app/start.sh
References:
http://sillycat.iteye.com/blog/2223733
http://dockerpool.com/static/books/docker_practice/repository/README.html
http://blog.arungupta.me/pushing-docker-images-registry-techtip58/