This will solve the PHP version upgrade! Copy and paste introductory docker
table of contents
Hello.
I'm Mandai, in charge of Wild on the development team.
It's been a while since PHP7 was released, but minor updates are still occurring frequently,
so deciding when to replace PHP is a very important issue.
Frequent updates are a pain.
But I want to use the latest version.
How about
docker at times like this In these times, there is an image of an official docker image, so I think you'll have to try it out at least once.
It comes with copy and paste commands that will make you want to try it!
Docker settings
Preparing Ubuntu Server for Work
So, this time I'd like to use vagrant and virtualbox to prepare an ubuntu server 15.10 and play with docker on top of that (commands are the same for Windows as well)
# Create an appropriate directory mkdir ubuntu1510 cd ubuntu1510 vagrant init boxcutter/ubuntu1510 vagrant up --provider virtualbox #--Change provider as appropriate.
Installing docker
Basically, the work is the same as the one listed at https://docs.docker.com/engine/installation/ubuntulinux/.
From now on, we will proceed with the work within Ubuntu.
Get GPG key
vagrant@vagrant:~$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Adding apt repository
vagrant@vagrant:~$ echo "deb https://apt.dockerproject.org/repo ubuntu-wily main" | sudo tee /etc/apt/sources.list.d/docker.list
Update apt repository
vagrant@vagrant:~$ sudo apt-get update
Purging unnecessary apt packages
vagrant@vagrant:~$ sudo apt-get purge lxc-docker
Installing linux-image-extra
vagrant@vagrant:~$ sudo apt-get install linux-image-extra-$(uname -r)
install docker
vagrant@vagrant:~$ sudo apt-get install docker-engine
Add vagrant user to docker group
vagrant@vagrant:~$ sudo usermod -aG docker vagrant # If not added, sudo will be required to run docker # It seems better for security to always run with sudo except in the verification environment
Quickly build a PHP7.0.2 environment
vagrant@vagrant:~$ docker pull php:7.0.2-apache # This time, choose PHP that can be executed in an apache environment
Once the various downloads are complete, check if docker recognizes it.
vagrant@vagrant:~$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE php 7.0.2-apache 797e8c22cdde 6 days ago 521 MB
Try running the imported docker image
vagrant@vagrant:~/docker_php7$ docker run php:7.0.2-apache AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Tue Feb 16 08:19:41.424790 2016] [mpm_prefork:notice] [pid 1] AH00163 : Apache/2.4.10 (Debian) PHP/7.0.2 configured -- resuming normal operations [Tue Feb 16 08:19:41.426135 2016] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND '
It seems to be working fine, but I Ctrl + z , and it no longer accepts input, so Ctrl + c to stop it.
Run docker image in background
Other than the fact that apache is running, I have no idea what's going on, so I'll try running the docker image from earlier in the background.
docker run -d php:7.0.2-apache
Let's check how it's working using the docker ps command.
vagrant@vagrant:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5cce300a61e5 php:7.0.2-apache "apache2-foreground" 8 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp small_lovelace
For now, I've confirmed it's moving. I'm excited.
I haven't done anything though. . .
Look inside the docker container
Let's take a look inside the running docker container and see what settings it has.
Let's stop the docker container that was started in the background earlier.
To stop a running docker container, use the docker stop command.
vagrant@vagrant:~$ docker stop 5cce300a61e5 5cce300a61e5
5cce300a61e5 is the container ID when you use docker ps. I entered all the IDs earlier, but if only one docker container is running, you can stop a container with a matching ID even if it is incomplete string, such as docker stop 5cc.
This container ID is unique when you start it, so if you run it in a different environment, it will be given a different container ID.
To see the contents of a docker container, specify the i and t options when running docker run, and you will be able to see inside the container.
The command to start the docker image is as follows.
docker run -it php:7.0.2-apache /bin/bash
The prompt notation has changed as follows.
root@ac4e7280d147:/var/www/html#
Check the currently running processes using the ps command.
root@b679b610fdae:/etc/apache2# ps aux user pid %cpu %mEM vsz rss time command root 1 0.0 0.0 0.0 T 113 0.0 0.0 17500 2068? R+ 18:01 0:00 ps aux
It doesn't seem to move at anything.
You can see that processes in the container are detached from the host OS.
For now, try starting apache.
root@b679b610fdae:/etc/apache2# /etc/init.d/apache2 start [....] Starting web server: apache2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message. ok # Check the apache process with the ps command root@b679b610fdae:/etc/apache2# ps auxf USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 20248 3320 ? Ss 17:56 0:00 /bin/bash root 130 0.0 0.3 166704 15264 ? Ss 18:02 0:00 /usr/sbin/apache2 -k start www-data 133 0.0 0.2 166728 8548 ? S 18:02 0:00 _ /usr/sbin/apache2 -k start www-data 134 0.0 0.2 166728 8548 ? S 18:02 0:00 _ /usr/sbin/apache2 -k start www-data 135 0.0 0.2 166728 8548 ? S 18:02 0 :00 _ /usr/sbin/apache2 -k start www-data 136 0.0 0.2 166728 8548 ? S 18:02 0:00 _ /usr/sbin/apache2 -k start www-data 137 0.0 0.2 166728 8548 ? S 18: 02 0:00 _ /usr/sbin/apache2 -k start root 188 0.0 0.0 17496 2104 ? R+ 18:42 0:00 ps auxf
It appears to be running safely.
Interestingly, the docker image I'm using this time comes with curl, so I'll try using it to see if I can communicate with HTTP inside a docker container.
# Prepare data for the response root@b679b610fdae:/etc/apache2# echo "hogehoge test" > /var/www/html/index.html root@b679b610fdae:/etc/apache2# curl http://127.0.0.1/ hogehoge test
I've seen a response return normally from the internal apache.
I would like to access this from an external source, but in the current state, there is no way to connect to this docker container from outside, so I will exit the container once I exit.
Try mapping the docker container port to the host OS
We will start the docker container again, but at this time, as a test, we will add settings to map port 80 in the docker container to port 8080 of the host OS and start the docker container.
vagrant@vagrant:~$ docker run -it -p 8080:80 php:7.0.2-apache /bin/bash # -p 8080:80 port-maps number 8080 in the host OS to number 80 in the docker container. meaning
Once started, check the apache process with the ps command.
root@949dd89c3341:/var/www/html# ps auxf USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 20248 3296 ? Ss 19:09 0:00 /bin/bash root 57 0.0 0.0 17496 2068 ? R+ 19:12 0:00 ps auxf
Apache is not running.
In fact, this is an important property of docker containers, and whenever you run docker each time, you create a container from the original docker image (hereafter php:7.0.2-apache).
Therefore, my work remains after I placed the file in /var/www/html/index.html.
Once you docker run, the state at the time of start will always be the same.
This requires you to do regular tasks (in this case, starting apache and creating index.html) every time.
If you are using it for full-scale development, it would be impossible to just start apache and transfer files to the container.
And even standard procedures change over time.
To do this, we will automatically perform the procedure at startup into a Dockerfile, and create a docker image with the procedure completed, but we plan to do that next time.
First of all, this time, we will manually start apache and place the contents.
root@949dd89c3341:/var/www/html# echo "hogehoge test" > /var/www/html/index.html root@949dd89c3341:/var/www/html# /etc/init.d/apache2 start
Now, in order to access the docker container from outside, you will exit the docker container and return to the host OS.
This action is called detaching, and shortcut keys are provided.
As mentioned earlier, when you enter the exit command, the docker container itself will stop, so
now you will press Ctrl P → Q and return to the ubuntu terminal.
root@949dd89c3341:/var/www/html# # In this state, press Ctrl+P, Q root@949dd89c3341:/var/www/html# vagrant@vagrant:~$
As it is, it remains running.
Incidentally, how to return to the docker container again
vagrant@vagrant:~$ docker attach 949dd
Let's say that. The detachment is when you return from the container to the host OS, so the opposite is attach.
949dd is part of the container ID of the docker container that is currently running.
Now, let's use curl from ubuntu to send a request to port 8080.
vagrant@vagrant:~$ curl http://localhost:8080 hogehoge test
The data was returned safely.
Incidentally, to see which ports on the host OS are mapped to, you can see by referring to iptables on the host OS.
Network settings can be checked using the ip command or iptables command, making it easy to understand.
Does PHP work too?
The docker image I used this time is a docker image with PHP 7.0.2, which is the latest version at the time of writing this article.I originally wanted to check the operation of PHP, so I will do a simple operation check.
Attach it to the docker container from earlier.
vagrant@vagrant:~$ docker attach 949dd
Next, let's run something that is standard when checking PHP operation.
root@949dd89c3341:/var/www/html# echo "<?php phpinfo();" > /var/www/html/info.php
If PHP information flows to the console, PHP is working without any problems.
Well, I think this is the starting line for docker.
Next time, I would like to touch on Dockerfiles, directory sharing, and docker container commits.
That's it.
If you want to consult a development professional
At Beyond, we combine our rich track record, technology and know-how in system development that we have cultivated to date with OSS technology and cloud technologies such as AWS to create contract development of web systems with reliable quality and excellent cost performance.
We also work on server-side/backend development and linkage development of our own APIs, using the technology and know-how of the construction and operation of web system/app infrastructure for large-scale, highly loaded games, apps, and digital content.
If you are having trouble with development projects, please visit the website below.
● Web system development
● Server-side development (API/DB)