简体   繁体   中英

Running PHP, Apache and MySQL with Docker & docker-compose

I can't figure out how to use my containers after they are running in Docker (through docker-compose).

I've been reading up on Docker for the last few weeks and I'm interested in building an environment that I could develop on that and I could replicate efficiently to collaborate with my colleagues.

I've read through the following articles:

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

I've installed Docker and Docker Compose through a Bash script made up of the commands found in the previous articles: (Run as 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

And I have a docker-compose.yml file for my Docker images. (For now I'm only pulling PHP and Apache as a proof-of-concept, I will include MySQL once can get PHP and Apache working together)

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

I call a sudo docker-compose up -d and I don't receive any errors:

Creating containerwrap_httpd_1
Creating containerwrap_php_1

Any my question is: When I run a php -v and service httpd status after the images are running I receive:

-bash: php: command not found

and

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

If the images are running why am I not able to use PHP or see the status of Apache? How am I supposed to utilize my PHP and Apache containers?

Update

I've asked a more informed version of this question again .

What you are missing is that containers are like different machines. Running the commands at your droplet, you will not see anything running in those machines. You need to connect to them. One easy way is to use something: docker exec -it CONTAINER_ID /bin/bash . This will give you access to each containers bash.

For your Apache server that would be containerwrap_httpd_1 and it will always change unless you have your docker-compose.yaml set up to use a constant name each time the service is created and started. Of course you can either curl localhost or open a browser and browse to your 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's default web page, because you have set up the export 80:80 rule for the service.

Seems you have some misunderstanding of 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. You cannot perform these commands in your host, because your host is a different machine from your containers. If you want to attach to the container bash, check out this command exec . You should be able to run php or httpd commands in the containers' bash.

If you want connect to your php container from your host, you can try docker run -p parameter, which will publish a container's port(s) to the host.

Or you want connect your php and httpd containers together, you should consider to read docker's network documents to figure out how to use link or docker network .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM