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 has been some time since PHP7 was released, but minor version updates are still occurring frequently, so
I think the question of when to replace PHP is a very important one.
Frequent updates are a hassle,
but I want to use the latest version.
In this situation, how about using
Docker In this day and age, with official Docker images, you have no choice but to try it out at least once.
It comes with copy and paste commands to make you feel like you've accomplished something!
Docker configuration
Preparing a working Ubuntu Server
So, this time, I'll use Vagrant and VirtualBox to prepare Ubuntu Server 15.10 and play around with Docker on it (the commands are the same on Windows)
# Create a directory mkdir ubuntu1510 cd ubuntu1510 vagrant init boxcutter/ubuntu1510 vagrant up --provider virtualbox #Change --provider as appropriate
Installing Docker
Basically, it is the same as the steps described in https://docs.docker.com/engine/installation/ubuntulinux/.
From here on, we will proceed with the steps within Ubuntu.
Obtaining the GPG key
vagrant@vagrant:~$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Adding the apt repository
vagrant@vagrant:~$ echo "deb https://apt.dockerproject.org/repo ubuntu-wily main" | sudo tee /etc/apt/sources.list.d/docker.list
Updating the 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 the vagrant user to the docker group
vagrant@vagrant:~$ sudo usermod -aG docker vagrant # If not added, sudo is required to run docker # It is better for security reasons to always run with sudo except in a testing environment
Quickly build a PHP7.0.2 environment
vagrant@vagrant:~$ docker pull php:7.0.2-apache # This time, choose PHP that can be run in the Apache environment
Once all the downloads are complete, check to see if Docker recognizes them
vagrant@vagrant:~$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE php 7.0.2-apache 797e8c22cdde 6 days ago 521 MB
Run 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 Ctrl + z and it no longer accepts input, so I'll stop it with Ctrl + c
Run a docker image in the background
I have no idea what's going on other than the fact that Apache is somehow running, 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 by 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
I was able to confirm that it was working. I'm excited.
I haven't done anything yet though...
Take a look inside the Docker container
Let's take a look inside the running Docker container and check what the settings are.
Let's stop the Docker container that we 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 run docker ps. We entered the entire ID earlier, but if there is only one Docker container running, you can stop the container with an ID that matches even an incomplete string, such as docker stop 5cc.
This container ID is unique and is assigned when the container is started, so if you run it in a different environment, a different container ID will be assigned.
To see the contents of a Docker container, specify the i and t options when using docker run.
The command to start a Docker image is as follows:
docker run -it php:7.0.2-apache /bin/bash
The prompt has changed to the following:
root@ac4e7280d147:/var/www/html#
Use the ps command to check the currently running processes
root@b679b610fdae:/etc/apache2# ps aux 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 113 0.0 0.0 17500 2068 ? R+ 18:01 0:00 ps aux
It seems like nothing is running.
You can see that the processes in the container are separated from the host OS.
For now, let's 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 seems to have started up successfully.
Interestingly, the Docker image I'm using this time has curl built in, so I'll try using that to see if I can communicate via HTTP within the Docker container.
# Prepare response data root@b679b610fdae:/etc/apache2# echo "hogehoge test" > /var/www/html/index.html root@b679b610fdae:/etc/apache2# curl http://127.0.0.1/ hogehoge test
We have confirmed that a normal response is returned from the internal Apache.
We want to access this from the outside, but in its current state there is no way to connect to this Docker container from the outside, so we will first exit the container by using exit.
Mapping Docker container ports to the host OS
We will start the Docker container again, but this time, as a test, we will add a setting to map port 80 in the Docker container to port 8080 on 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 means port mapping of 8080 on the host OS to 80 on the Docker container
Once started, use the ps command to check for apache processes
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 isn't running.
This is actually an important characteristic of Docker containers: every time you run Docker Run, a container is created from the original Docker image (php:7.0.2-apache in this case).
Therefore, my previous work of placing a file in /var/www/html/index.html is not retained.
When you run Docker Run, the initial state is always the same.
This means you have to perform routine tasks (in this case, starting Apache and creating index.html) every time.
For full-scale development, starting Apache and transferring files to the container is not enough.
Even routine procedures change over time.
To achieve this, you can write startup procedures in a Dockerfile to perform them automatically, or create a Docker image with those procedures already completed. We'll cover that in a future post.
For now, we will manually start Apache and place the content
root@949dd89c3341:/var/www/html# echo "hogehoge test" > /var/www/html/index.html root@949dd89c3341:/var/www/html# /etc/init.d/apache2 start
Now, to access the Docker container from outside, we will exit the Docker container and return to the host OS.
This action is called detaching, and there is a shortcut key for it. As
mentioned earlier, entering the exit command will stop the Docker container itself, so
this time, hold down Ctrl P → Q on the keyboard to return to the Ubuntu terminal.
root@949dd89c3341:/var/www/html# # Press Ctrl+P, Q in this state root@949dd89c3341:/var/www/html# vagrant@vagrant:~$
As you can see, it is still running.
By the way, to return to the Docker container again,
vagrant@vagrant:~$ docker attach 949dd
Let's say. Detaching is returning from a container to the host OS, so the opposite is attaching.
949dd is part of the container ID of the currently running Docker container.
Now, let's try sending a request to port 8080 using curl from ubuntu
vagrant@vagrant:~$ curl http://localhost:8080 hogehoge test
The data was returned successfully.
By the way, to check which port on the host OS the Docker container port is mapped to, you can refer to iptables on the host OS.
Network settings can be checked with the ip command or iptables command, so it's easy to understand.
Does PHP work too?
The Docker image used this time is a Docker image containing the latest PHP 7.0.2 at the time of writing this article. Originally, I wanted to check the operation of PHP, so I will do a simple operation check
Attach to the Docker container you just created
vagrant@vagrant:~$ docker attach 949dd
This time, we will try running the standard test to check that PHP is working
root@949dd89c3341:/var/www/html# echo "<?php phpinfo();" > /var/www/html/info.php
If you see PHP information in the console, then PHP is working fine
Well, I think this is the starting point for Docker.
Next time, I would like to talk about Dockerfile, directory sharing, and committing Docker containers.
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)
0