Docker(2)Repositories and PHP example on CentOS

本文详细介绍了如何使用Docker在CentOS环境下构建PHP环境,包括私有与公共仓库的使用、登录认证、构建与推送镜像、容器运行与管理等步骤,并提供了实际操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: luohuazju@gmail.com
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/
### 可能的原因分析 当执行 `docker pull` 操作时,如果返回 HTTP 状态码 500 (Internal Server Error),通常表示服务器端发生了某种错误。这种错误可能由多种原因引起,包括但不限于: - 私有镜像仓库配置不正确。 - 镜像元数据损坏或不可用。 - Docker 守护进程与远程仓库之间的通信异常。 以下是一些常见的解决方法及其对应的解释[^1]。 --- ### 解决方案 #### 1. **验证私有仓库的可用性和权限** 如果正在使用的是一个私有的 Harbor 或其他自定义镜像仓库,则需要确认该仓库服务正常运行,并且当前用户具有拉取镜像的有效权限。可以通过访问仓库管理界面或者 API 接口来测试连接状态[^3]。 ```bash curl -u username:password https://harbor.example.com/api/repositories ``` 此命令会尝试获取存储库列表;如果没有收到预期响应(例如 JSON 数据),则可能是认证失败或其他网络层面的问题。 #### 2. **清理本地缓存并重试** 有时候旧版本的层文件可能会干扰新请求的成功完成。可以先清除所有未被任何容器引用的悬空图像以及构建阶段产生的临时对象再做进一步操作: ```bash docker system prune --all --volumes ``` 之后再次发起 `docker pull` 请求看看是否会有所改善[^2]. #### 3. **升级Docker客户端和服务端到最新稳定版** 考虑到软件可能存在 bug 导致特定情况下触发内部错误的情况发生,建议检查目前所安装的是哪个发行号并通过官方渠道更新至最近期发布版本. 对于 CentOS 用户来说,这一步骤可通过如下方式实现: ```bash sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum makecache fast sudo yum list docker-ce --showduplicates | sort -r sudo yum install docker-ce docker-ce-cli containerd.io ``` 最后重启相关服务以使更改生效[`systemctl restart docker`]。 #### 4. **启用调试模式捕获更多细节信息** 为了更深入地了解到底是什么地方出了差错,我们可以开启详细的日志记录功能以便于后续排查工作开展得更加顺利一些. 编辑 `/etc/docker/daemon.json`, 添加下面的内容(注意保持正确的JSON语法): ```json { "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" }, "debug": true } ``` 保存修改后的配置文档后记得重新加载守护程序设置:`systemctl reload docker`. 现在当你重复遇到同样的状况时候应该可以在/var/log/messages或者是journalctl里面找到更为详尽的消息描述帮助定位根本原因所在之处[^4]. --- ### 总结 通过以上几个方面的调整和优化措施往往能够有效缓解乃至彻底消除因各种潜在因素引发的此类现象继续出现的可能性。当然实际场景下也许还存在着其它特殊情形需具体对待处理才行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值