首页 文章

使用Docker和docker-compose运行PHP,Apache和MySQL

提问于
浏览
0

在Docker中运行后,我无法弄清楚如何使用我的容器(通过docker-compose) .

过去几周我一直在阅读Docker,我有兴趣 Build 一个我可以开发的环境,我可以高效地复制,与同事合作 .

我已经阅读了以下文章:

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-centos-7

https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-and-phpmyadmin-with-docker-compose-on-ubuntu-14-04

https://www.digitalocean.com/community/tutorials/how-to-work-with-docker-data-volumes-on-ubuntu-14-04

我已经通过Bash脚本安装了Docker和Docker Compose,该脚本由前面文章中的命令组成:(以sudo身份运行)

## Install Docker package
function docker_install {
    echo "__ installing docker"
    # Run installer script
    wget -qO- https://get.docker.com/ | sh
    # Add user parameter to docker group
    usermod -aG docker $1
    # Enable docker on boot
    sudo systemctl enable docker.service
    # Start docker now
    sudo systemctl start docker.service
}

## Install docker-compose package
function docker-compose_install {
    echo "__ installing docker-compose"
    # Update yum repo
    sudo yum install epel-release
    # Install pip with "yes" flag
    sudo yum install -y python-pip
    # Install SSL Extension
    sudo pip install backports.ssl_match_hostname --upgrade
    # Install docker-compose through pip
    sudo pip install docker-compose
    # Upgrade python
    sudo yum upgrade python*
}

# Call Functions
docker_install
docker-compose_install

我有一个用于Docker镜像的 docker-compose.yml 文件 . (目前我只是将PHP和Apache作为概念验证,我将包括MySQL一次可以让PHP和Apache协同工作)

php:
  image: php
  links:
    - httpd
httpd:
  image: httpd
  ports:
    - 80:80

我打电话给 sudo docker-compose up -d ,我没有收到任何错误:

Creating containerwrap_httpd_1
Creating containerwrap_php_1

我的任何问题是:当我运行图像后运行 php -vservice httpd status 时,我会收到:

-bash: php: command not found

● httd.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

如果图像正在运行,为什么我无法使用PHP或查看Apache的状态?我该如何利用我的PHP和Apache容器?

更新

我问过this question again的更明智的版本 .

2 回答

  • 2

    似乎你对Docker 's concepts. You can image that each docker container as a lightweight Virtual Machine(of course Docker container is different with real VM). So basically after you created php and httpd containers, these php and httpd commands only available in the containers' bash有一些误解 . 您无法在主机中执行这些命令,因为您的主机与容器不同 . 如果要附加到容器bash,请查看此命令exec . 你应该能够在容器的bash中运行php或httpd命令 .

    如果你想从你的主机连接你的php容器,你可以尝试docker run -p参数,这将 publish a container's port(s) to the host.

    或者你想将你的php和httpd容器连接在一起,你应该考虑阅读docker的网络文档来弄清楚如何使用linkdocker network .

  • 1

    你缺少的是容器就像不同的机器 . 在Droplet上运行命令,您将看不到在这些机器上运行的任何内容 . 你需要连接到它们 . 一个简单的方法是使用一些东西: docker exec -it CONTAINER_ID /bin/bash . 这将使您可以访问每个容器bash .

    对于您的Apache服务器,它将是containerwrap_httpd_1,除非您在每次创建和启动服务时将docker-compose.yaml设置为使用常量名称,否则它将始终更改 . 当然,您可以 curl localhost 或打开浏览器并浏览到您的Droplet 's IP address, provided it forwards http on the default port(or use the forwarding port). This will have the effect to see the Apache'的默认网页,因为您已为该服务设置了导出80:80规则 .

相关问题