This will solve the PHP version upgrade! An introduction to Docker that can be done with copy and paste

table of contents
Hello.
I'm Mandai, the Wild team member in charge of development.
Although some time has passed since PHP7 was released, minor version updates are still occurring frequently,
making the timing of when to replace PHP a very important issue.
Frequent updates are a hassle,
but I still want to use the latest version.
In situations like this,Dockerwhy not try
With official Docker images available these days, you should definitely give it a try.
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, the process is the same as described at https://docs.docker.com/engine/installation/ubuntulinux/.
From here on, we will proceed 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, butCtrl+Z , and it's stopped accepting input, soCtrl+C.I'll stop it for now with
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've confirmed that it's working, at least for now. It's exciting!
Even though I haven't done anything yet...
Take a look inside the Docker container
Let's take a look inside the running Docker container to see what settings it has.
Let's stop the Docker container that we started in the background earlier.
To stop a running Docker container, we use the `docker stop` command.
vagrant@vagrant:~$ docker stop 5cce300a61e5 5cce300a61e5
5cce300a61e5 is the container ID when you run `docker ps`. Earlier, we entered the full ID, but if only one Docker container is running, you can stop the container with the matching ID even with an incomplete string, like `docker stop 5cc`.
This container ID is unique and assigned at startup, so if you run it in a different environment, it will have a different container ID.
To view the contents of a Docker container, specify the `i` and `t` options when running `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
Nothing seems to be running.
It appears the processes within the container are disconnected from the host OS.
For now, I'll 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 we're using includes curl, so let's try using it to see if we 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
I've confirmed that the internal Apache is responding correctly.
I want to access this from the outside, but currently there's no way to connect to this Docker container from the outside, so I'll exit the container for now.
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`, it creates a container from the base Docker image (in this case, php:7.0.2-apache).
Therefore, my previous work of placing the file in `/var/www/html/index.html` is not preserved.
When you run `docker run`, the starting state is always the same.
This means that you have to perform routine tasks (in this case, starting Apache and creating index.html) every single time.
If you're using this for serious development, it's not going to end with just starting Apache and transferring files to the container.
Moreover, even routine procedures change over time.
That's why we write the startup procedures into a Dockerfile to automate them, or create a Docker image that has already completed those procedures, but we'll cover that in future posts.
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, in order to access the Docker container from the outside, we need to 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, typing the exit command will stop the Docker container itself, so
this timeCtrl, while holding downPthenQon 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 remains running.
By the way, to return to the Docker container...
vagrant@vagrant:~$ docker attach 949dd
Let's assume that detaching means returning from the container to the host OS, so the reverse 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 port of the Docker container is mapped to, you can refer to iptables on the host OS.
Network settings can be checked using the ip command and iptables command, which is 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
So, I think this is the starting point for learning about Docker.
If there's a next time, I'd like to touch on Dockerfiles, directory sharing, and committing Docker containers.
That's all
If you want to consult with a development professional
At Beyond, we combine our extensive track record, technology, and know-how in system development with OSS technology and cloud technologies such as AWS to provide contracted development of web systems with reliable quality and excellent cost performance
We also handle server-side/back-end development and proprietary API collaboration development, making full use of our technology and know-how in building and operating web system/application infrastructure for large-scale, high-load games, applications, and digital content
If you have any problems with your development project, please visit the following website
● Web system development
● Server-side development (API / DB)
0
