Very polite! Introduction to docker by copying and pasting
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 using docker at times like this?
In this day and age, where there is an official docker image, I think you just have to try it out at least once.
It comes with copy and paste commands that will make you want to try it!
Preparing an Ubuntu Server for work (commands are the same even on Windows)
So, this time, I would like to use Vagrant and virtualbox to prepare Ubuntu Server 15.10 and play with Docker on it.
# 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
I was able to confirm that it was working for the time being. 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 run docker ps, and earlier you entered the entire ID, but if only one docker container is running, you can enter an incomplete string like docker stop 5cc. You can stop a container with a matching ID.
This container ID is unique and assigned at startup, 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 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
Nothing seems to be moving.
You can see that the processes inside the container are detached from the host OS.
For now, let's start 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 be starting up successfully.
Interestingly, the docker image we are using this time includes curl, so we will use it to see if we can perform HTTP communication within the docker container.
# Prepare data for 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 confirmed that the response was returned normally from the internal apache.
I would like to access this from the outside, but in the current state there is no way to connect to this docker container from the outside, so I will exit from the container using 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 isn't starting.
Actually, this is an important property of docker containers; every time you run docker, a container is created from the original docker image (php:7.0.2-apache in this case).
Therefore, the work I did last time when I placed the file in /var/www/html/index.html will not remain.
When you run docker, the state will always be the same as when you started.
With this, you will need to perform routine tasks (in this case, starting apache and creating index.html) every time.
If you are using it for full-scale development, you can't just start apache and transfer files to the container.
Furthermore, even routine procedures change over time.
To do this, we will create a Dockerfile with the startup procedures and run them automatically, and we will create a docker image with those procedures 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 the outside, exit from the docker container and return to the host OS.
This action is called detaching, and a shortcut key is provided.
If you enter the exit command, the docker container itself will stop, as mentioned earlier, so
hold down Ctrl P → Q on your keyboard to 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 you can see, it is still starting up.
By the way, to return to the docker container again
vagrant@vagrant:~$ docker attach 949dd
Let's say. Detach is when you return from the container to the host OS, so attach is the opposite.
949dd is part of the container ID of the currently running docker container.
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 successfully.
By the way, to check which port on the host OS the docker container port is mapped to, you can check iptables on the host OS.
Network settings can be checked using the ip and iptables commands, 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.