To build LEMP environment through docker under mac
- 2020-06-07 05:50:29
- OfStack
Under mac, LEMP environment is built through docker
1. Install virtualbox. Because docker is the container in the lxc environment
2. Install boot2docker to communicate with docker client
> brew update
> brew install docker
> brew install boot2docker
3. Initialize boot2docker, that is, install 1 docker host environment on virtualbox
boot2docker init
At this point, a mirror is downloaded
4. Start virtual machine host
:~$ boot2docker up
Waiting for VM and Docker daemon to start...
....................ooo
Started.
To connect the Docker client to the Docker daemon, please set:
export DOCKER_HOST=tcp://192.168.59.103:2375
unset DOCKER_CERT_PATH
The host environment is then booted up and prompted to set the environment variables
export DOCKER_HOST=tcp://192.168.59.103:2375
boot2docker can then connect to the docker client in the host environment
5. Connect to docker client for host environment
MacBook-Pro:~$ boot2docker ssh
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
boot2docker with VirtualBox guest additions version 4.3.14
boot2docker: 1.2.0
master : e75396e - Fri Aug 22 06:03:48 UTC 2014
docker@boot2docker:~$
This allows us to successfully log into the host environment in virtualbox and proceed with the docker operation
Install nginx, php, mysql based on ubuntu14:04, below I installed via Dockerfile
Here is the DockFile
6. Generate mysql images
Dockerfile
# LEMP stack as a docker container
FROM ubuntu:14.04
MAINTAINER Daniel Watrous <email>
#ENV http_proxy http://proxy.example.com:8080
#ENV https_proxy https://proxy.example.com:8080
RUN apt-get update
RUN apt-get -y upgrade
# seed database password
COPY mysqlpwdseed /root/mysqlpwdseed
RUN debconf-set-selections /root/mysqlpwdseed
RUN apt-get -y install mysql-server
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
RUN /usr/sbin/mysqld & \
sleep 10s &&\
echo "GRANT ALL ON *.* TO admin@'%' IDENTIFIED BY 'secret' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql -u root --password=secret &&\
echo "create database test" | mysql -u root --password=secret
# persistence: http://txt.fliglio.com/2013/11/creating-a-mysql-docker-container/
EXPOSE 3306
CMD ["/usr/bin/mysqld_safe"]
mysqlpwdseed
mysql-server mysql-server/root_password password secret
mysql-server mysql-server/root_password_again password secret
Enter Dockefile of mysql and generate 1 mysql image
docker build -t "local/mysql:v1" .
After the success with
docker images
View the generated image
Run the mysql image to generate 1 container
docker run -d --name mysql local/mysql:v1
Once it's up and running, you can use it
boot2docker init
0
To see the running container
7. Generate nginx, php, these two generate 1 mirror with 1 dockerfile
dockerfile
boot2docker init
1
default 1 default nginx configuration file
boot2docker init
2
wall. php 1 test file used to test connection to mysql
<?php
// database credentials (defined in group_vars/all)
$dbname = "test";
$dbuser = "admin";
$dbpass = "secret";
$dbhost = "mysql";
// query templates
$create_table = "CREATE TABLE IF NOT EXISTS `wall` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`content` text NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$select_wall = 'SELECT * FROM wall';
// Connect to and select database
$link = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Could not connect: ' . mysql_error());
echo "Connected successfully\n<br />\n";
mysql_select_db($dbname) or die('Could not select database');
// create table
$result = mysql_query($create_table) or die('Create Table failed: ' . mysql_error());
// handle new wall posts
if (isset($_POST["title"])) {
$result = mysql_query("insert into wall (title, content) values ('".$_POST["title"]."', '".$_POST["content"]."')") or die('Create Table failed: ' . mysql_error());
}
// Performing SQL query
$result = mysql_query($select_wall) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
<form method="post">
Title: <input type="text" name="title"><br />
Message: <textarea name="content"></textarea><br />
<input type="submit" value="Post to wall">
</form>
Put all three files in the same directory
Enter the directory
dockerfile is used to generate images
boot2docker init
4
After the successful generation of view sentence is you
boot2docker init
5
run
boot2docker init
6
This is done by looking at the running container
boot2docker init
7
You can see nginx and mysql in action, where nginx is connected to the mysql container via link,
through
--link mysql:mysql
Command to add the address of the mysql container to the host file of the nginx parent container, after which you can access the database by accessing the alias mysql.
Browser to access: 192.168.59.103 / wall php, just take good web server can be accessed
8. Load the directory into the container
We want to hold a local directory in the container for web content that may need to be modified later. Here we pass
boot2docker init
9
Store the web directory contents in the container so that web files such as wall.php can be placed in the /Users/lyc/web directory for container access
However, since the host host is virtualbox in the mac environment, we need to bind the directory under mac to the virtual machine first
First stop boot2docker
> boot2docker down
then
> curl http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso > ~/.boot2docker/boot2docker.iso
> VBoxManage sharedfolder add boot2docker-vm -name home -hostpath /Users
> boot2docker up
Reconnect docker
First, delete the stopped container
docker rm mysql
docker rm nginx
Mount our directory into nginx's directory when the container is started
:~$ boot2docker up
Waiting for VM and Docker daemon to start...
....................ooo
Started.
To connect the Docker client to the Docker daemon, please set:
export DOCKER_HOST=tcp://192.168.59.103:2375
unset DOCKER_CERT_PATH
3
So that you can modify the contents of the/Users lyc/web to update/usr share nginx/html