Storyline
I will never learn all the technologies and The technology always is changing,then I thought I must choose which I like and to do this my backbone, I would have a basic platform for deal with it, good and reliable tools.
On a project I has a problem is created thinking in windows and all would be so easy and save a lot of time if I had chosen windows but I love Archlinux and Neovim, use the terminal (sigh cheesy), and wherever it can be my future like programmer I want to be a master with this tools.
Archlinux is wonderful the documentation is excellent and install a software is easy peasy has a strong community beautiful always update .. and yes always update, this was my problem one day I use php 5.x BOOM! now I have php 7.0 great but the project does not use 7.0 and although it is possible control which package is updating I don't want always take care about it.
Laravel has homestead then vegrant was my first option I was learning but I found a compatibility problems between vagrant and virtualbox I needed downgrade my vegrant, the case it the same I don't want deal with this then I tried docker.
Docker
Docker has IMAGES and CONTAINERS
- IMAGES is your base like a image of Ubuntu or Fedora etc.
- CONTAINERS is the place where you can play do everything you want experiment.
- Host is your computer.
Everytime you run an image this will become container this mean you can run 5 times ubuntu and in an instant, really! in an instant you have 5 different places where you can play delete/install everything you want, it does not work throw it in trash (delete your container) and run ubuntu (image) one more time, beautiful no?.
Docker has 2 options to custom your cointaner
-
Dockerfile: it is a file where you write every instruction, build this file and SHAZAM! you have your environment.
write your file DOCKERFILE => BUILD => a new IMAGE - run your image install everything you want and commit the changes. UBUNTU => CONTAINER UBUNTU (configure your environment) => commit => a new IMAGE
I chose the number 2 because I feel control everything I do, sometimes I feel so stupid, understand this take me a lot of time in essence is an easy concept, well let's continue.
Using Docker and Laravel
The fact is simple, I need other environment (SO) but I want to use my neovim I want this.
- Laravel and Mysql in container.
- I saw vagrant can communicate the files in the same directory...of course I want it.
- Mysql workbreach and Neovim in host (neovim not within of a container because I don't want stay setting every container, in my life, take time and space).
let's go
Installation
sudo pacman -S docker sshfs
docker pull ubuntu:14.04
docker images (you can see ubuntu)
docker run -it --name NAMECONTAINER -p 8090:8000 -p 8050:22 -p 3308:3306 ubuntu:14.04 bash
Considerations
- user/nameproject is the convection name but I prefer nameprojectc, in the end a c only indicates the name and c of container because is a short name.
- You can choose every port you want: -p HOST:CONTAINER
- Command exit stop and exit your container
- ctrl+p ctrl+q go to host but doesn't stop your container.
- PROMPTHOST > docker attach NAMECONTAINER return to CONTAINERPROMPT but if it does not appear, press ctrl+c
- PROMPTHOST > docker inspect NAMECONTAINER show you gateway= IP host and IPAddress = IP cointainer
Now, in the prompt of the container
apt-get update
apt-get -y install vim openssh-server git curl
apt-get -y install mysql-server libapache2-mod-auth-mysql php5-mysql
apt-get -y install php5 php5-mcrypt php5-cli
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Edit /etc/php5/cli/php.ini search the section of mcrypt and adds extesion=mcrypt.so
Create your project laravel, I am in /home.
composer create-project laravel/laravel HelloDocker "~5.0.0" --prefer-dist
cd /home/HelloDocker
php artisan serve --host=0.0.0.0 --port=8090
go to your browser IPCONTAINER:8090, great! you can see the website.
Mysql
Comment this line in /etc/mysql/my.cf
bind-address = 127.0.0.1
PROMPTCONTAINER > service mysql start
on mysql run
GRANT ALL PRIVILEGES ON * . * TO 'USER'@'IPGATEWAY';
FLUSH PRIVILEGES;
on mysql workbreanch, create a new connection, hostname: GATEWAY and port:3308; Do you remember? we used -p 3308:3306
as said steve jobs, one more thing
SSHFS
San wikipedia:
SSHFS (SSH Filesystem) is a filesystem client to mount and interact with directories and files located on a remote server or workstation over a normal ssh connection
PROMPTCONTAINER >
mkdir /var/run/sshd
echo 'root:screencast' | chpasswd
sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ssh-keygen
/usr/sbin/sshd -D &
PROMPTHOST>
sshfs root@HostIp:/path/to/project /path/to/directory/to/mount/ -p 8050
# password is screencast
cd /path/to/directory/mounted
Everything is ready, there is the laravel project.
if you want to save your container like a image
PROMPTHOST> docker commit NAMECONTAINER NEWNAMEIMAGE
PROMPTHOST> docker images
Tl;dr
Docker rocks
Resources
[Docker ssh] (https://docs.docker.com/engine/examples/running_ssh_service/)
digitalocean composer
digitalocean mysql
sshfs