At the moment I have a basic Vagrant file. How can I add to my configuration dynamic memory allocation?
Here is my code:
Vagrant.configure(2) do |config|
config.vm.box = "centos7"
config.vm.box_url = "centos-7.0-x86_64.box"
config.vm.provision "shell" do |s|
s.inline = "sudo localectl set-locale LANG=en_GB.UTF-8
sudo touch /home/asd.txt
sudo yum install mc > /drv/null 2>&1"
end
end
Related
I need assistance putting together a Vagrantfile.
I am trying to setup a virtual machine on my Windows desktop for working on an existing PHP/MySQL application. I've been instructed to use Vagrant and VirtualBox. I've been going through the documentation for Vagrant and found this to be over my head with a lot of information out of date. I have some background in general Linux usage, but none in setting up LAMP servers on them.
What I have:
Vagrant 2.1.2
VirtualBox 5.2.18
Things I need in the VM:
Centos 6
Apache
MySQL 5.5
PHP 5.6
MySQLi/Mysqlnd (PDO optional)
Curl
DOM/SimpleXML
Any PHP extensions needed for a typical PHP application
It has taken a few weeks and a lot of wading through out of date tutorials, but I got it done. I am sharing so others on the LAMP service stack have a place to start in crafting their vagrantfile. Note that this was not done for elegance. It is a quick starter that is easy-ish to understand and adjust to one's needs.
A few small deviations of note were made from the above original post:
I went with Ubuntu/bionic64 instead of Centos 6, mostly because of the abundance of tutorial material I was able to find. Modifying this file for CentOS 6 shouldn't be too hard. CentOS uses Yum instead of Apt-get for package management. I'm not entirely certain what else is different.
I went with PHP 7.2 instead of 5.6.
I found PHP 7.2 comes with MySQLi and the native driver already installed out of the box.
Working vagrantfile and shell bootstrapper included, heavily commented for comprehension: https://pastebin.com/Eqvhq8KZ
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = '2'
#########################################################################
# VM Setup for LAMP stack application
#
# - Install PHP and packages
# - Install Apache
# - Configure /vagrant as document root
# - Config PHP for development and logging
# - Install MySQL
# - Setup database and permissions (username and password are "vagrant")
# - Install Composer
#########################################################################
#script = <<SCRIPT
#################### PHP ####################
apt-get install -y apt-utils php7.2 php7.2-bcmath php7.2-bz2 php7.2-cli php7.2-curl php7.2-intl php7.2-json php7.2-mbstring php7.2-opcache php7.2-soap php7.2-sqlite3 php7.2-xml php7.2-xsl php7.2-zip unzip
#################### APACHE2 ####################
apt-get install -y apache2 libapache2-mod-php7.2
# Remove /var/www default
rm -rf /var/www
# Symlink /vagrant to /var/www
ln -fs /vagrant /var/www
# Add ServerName to httpd.conf
echo "ServerName localhost" > /etc/apache2/httpd.conf
# Setup hosts file
VHOST=$(cat <<EOF
<VirtualHost *:80>
DocumentRoot "/vagrant"
ServerName localhost
<Directory "/vagrant">
Options Indexes FollowSymLinks MultiViews Includes
AllowOverride All
Require all granted
Order allow,deny
Allow from all
AddType text/html .shtm .shtml
AddOutputFilter INCLUDES .htm .html .shtm .shtml
</Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-enabled/000-default.conf
# Enable mod_rewrite
a2enmod rewrite
# Put PHP into development configuration
mv /etc/php/7.2/apache2/php.ini /etc/php/7.2/apache2/php.ini.back
cp /usr/lib/php/7.2/php.ini-development /etc/php/7.2/apache2/php.ini
# Enable PHP extensions in php.ini
#sed -i 's/;extension=mysqli/extension=mysqli/' /etc/php/7.2/apache2/php.ini
# PHP will log its errors in a /log/error_log file
sed -i 's:;error_log = php_errors.log:error_log = /vagrant/log/error_log:' /etc/php/7.2/apache2/php.ini
# Restart apache
systemctl restart apache2.service
#################### MYSQL ####################
apt-get install -y mysql-server mysql-client-core-5.7 php7.2-mysql
systemctl start mysql.service
# Reset root password
#/usr/bin/mysqladmin -u root password 'root'
mysqladmin -u root password 'root'
# Setup database from root user and setup the application user
mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS app"
mysql -uroot -proot app < /vagrant/db/schema.sql
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* to 'vagrant'#'localhost' IDENTIFIED BY 'vagrant'"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* to 'vagrant'#'%' IDENTIFIED BY 'vagrant'"
mysql -uroot -proot -e "FLUSH PRIVILEGES"
# Allow remote connections for MySQL Workbench
MYSQLCONF=$(cat <<EOF
[mysqld]
bind-address = 0.0.0.0
EOF
)
echo "${MYSQLCONF}" >> /etc/mysql/my.cnf
# Restart mysql
/etc/init.d/mysql restart
#################### COMPOSER ####################
curl -sS http://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# Run composer install
cd /vagrant && composer install
#################### FINISHED! ####################
echo "** [PHP] Visit http://localhost:8080 in your browser for to view the application **"
SCRIPT
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'ubuntu/bionic64'
config.ssh.insert_key = false
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 443, host: 8081
config.vm.network "forwarded_port", guest: 3306, host: 3307
#config.vm.synced_folder '.', '/var/www/html'
# Provision runs only on the first "Vangrant up" command
config.vm.provision 'shell', privileged: true, inline: #script
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
vb.customize ['modifyvm', :id, "--natdnshostresolver1", "on"]
end
end
I'm trying to run 3 services at my container startup (snmpd, sshd and centengine)
As runlevel is unknown in the container, services won't start.
I built an image with this Dockerfile :
FROM centos:6.7
MAINTAINER nael <me#mail>
# Update CentOS
RUN yum -y update
# Install wget
RUN yum install -y wget
# Get Centreon Repo
RUN wget http://yum.centreon.com/standard/3.0/stable/ces-standard.repo -O /etc/yum.repos.d/ces-standard.repo
# Install Packages (SSH, sudo, Centreon Poller & Engine, SNMP)
RUN yum install -y --nogpgcheck openssh-clients openssh-server centreon-poller-centreon-engine sudo net-snmp net-snmp-utils
# Install supervisord
RUN rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN yum --enablerepo=epel install -y supervisor
RUN mv -f /etc/supervisord.conf /etc/supervisord.conf.org
ADD supervisord.conf /etc/
# For sshd & centengine
EXPOSE 22 5669
# Change user password
RUN echo -e "password" | (passwd --stdin user)
# Disable PAM (causing issues while ssh login)
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config
# Start supervisord
CMD ["/usr/bin/supervisord"]
Here is the supervisord.conf file
[supervisord]
nodaemon=true
pidfile=/var/run/supervisord.pid
logfile=/var/log/supervisor/supervisord.log
[program:centengine]
command=service centengine start
[program:snmpd]
command=service snmpd start
[program:sshd]
command=service sshd start
But with this Dockerfile and supervisord.conf, when I start my container theses services aren't running.
What could be the problem ?
Not anymore using supervisord.
I just include a script with all the services ... start commands in the Dockerfile. When I create my container with docker run ... I just specify that I want to start it with my script.
& that's working very well.
Thanks #warmoverflow for trying to solve this.
You may find my dockerfy utility useful starting services, pre-running initialization commands before the primary command starts. See https://github.com/markriggins/dockerfy
For example:
RUN wget https://github.com/markriggins/dockerfy/releases/download/0.2.4/dockerfy-linux-amd64-0.2.4.tar.gz; \
tar -C /usr/local/bin -xvzf dockerfy-linux-amd64-*tar.gz; \
rm dockerfy-linux-amd64-*tar.gz;
ENTRYPOINT dockerfy
COMMAND --start bash -c "while false; do echo 'Ima Service'; sleep 1; done" -- \
--reap -- \
nginx
Would run a bash script as a service, echoing "Ima Service" every second, while the primary command nginx runs. If nginx exits, then the "Ima Service" script will automatically be stopped.
As an added benefit, any zombie processes left over by nginx will be automatically cleaned up.
You can also tail log files such as /var/log/nginx/error.log to stderr, edit nginx's configuration prior to startup and much more
After installing mongodb on 14.04, mongo doesn't work out of the box:
$ sudo apt install mongodb-server
[...]
$ sudo start mongodb
mongodb stop/waiting
What's happening here, how can I fix it?
TL;DR: change no to yes in /etc/default/mongodb and restart the service
The interesting part is in the upstart script (/etc/init/mongodb.conf), at line 19 and 21:
[...]
script
ENABLE_MONGODB="yes"
if [ -f /etc/default/mongodb ]; then
. /etc/default/mongodb
fi
if [ "$ENABLE_MONGODB" = "yes" ]; then
exec start-stop-daemon --start --quiet --chuid mongodb \
--exec /usr/bin/mongod -- --config /etc/mongodb.conf
fi
end script
So if ENABLE_MONGODB is yes, mongod starts and everything is good. It is set to yes in the script, but /etc/default/mongodb is sourced.
A peek inside /etc/default/mongodb shows the problem:
ENABLE_MONGODB="no"
I don't know why this ships disabled, change it to yes and you'll be fine after a sudo restart mongodb.
My name Trang,
I have created Docker image on https://registry.hub.docker.com/u/trangunghoa/railo-mysql/
It is run ok.
Now I created Dockerfile but I can't start automatic Railo service. Please help me.
I have start by some commands at shell script:
exec /opt/railo/railo_ctl start
exec /opt/railo/railo_ctl start -D FOREGROUND
service railo_ctl restart
exec service railo_ctl restart
No command it work.
I looked inside your Dockerfile and identified the problem.
You can only use one CMD inside a Dockerfile. (if you use multiple CMD the old one will override) More info : https://docs.docker.com/reference/builder/#cmd
You need to know that Docker isn't made for running multiple process without a little bit of help. I suggest using supervisord : https://docs.docker.com/articles/using_supervisord/
You can't use RUN service inside a Dockerfile, the reason is simple the command service will be executed and start a daemon, then notify the execution was successful. The temporary container will be killed (and the daemon too) and after that the change will be committed.
What your Dockerfile should look like :
FROM ubuntu:trusty
MAINTAINER Trang Lee <trangunghoa#gmail.com>, Seta International Vietnam(info#setacinq.vn)
#Install base packages
RUN apt-get -y update
RUN apt-get install -y openjdk-7-jre-headless
RUN apt-get install -y tomcat7 tomcat7-admin apache2 libapache2-mod-jk
RUN apt-get purge -y openjdk-6-jre-headless icedtea-6-jre-cacao openjdk-6-jre-lib icedtea-6-jre-jamvm
RUN apt-get install -y supervisor
# config to enable .htaccess
ADD apache_default /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
# start service
ADD start-apache2.sh /start-apache2.sh
ADD railo.sh /railo.sh
ADD run.sh /run.sh
RUN chmod +x /*.sh
#RUN sudo service apache2 start
# install railo
RUN apt-get install -y wget
RUN wget http://www.getrailo.org/railo/remote/download42/4.2.1.000/tomcat/linux/railo-4.2.1.000-pl2-linux-x64-installer.run
RUN chmod -R 744 railo-4.2.1.000-pl2-linux-x64-installer.run
RUN ./railo-4.2.1.000-pl2-linux-x64-installer.run --mode unattended --railopass “123456”
# remove railo setup
#RUN rm -rf railo-4.2.1.000-pl2-linux-x64-installer.run
#RUN sudo service railo_ctl start
RUN mkdir -p /etc/service/railo
ADD start-railo.sh /etc/service/railo/run
RUN chmod 755 /etc/service/railo/run
# EXPOSE <port>
EXPOSE 80 8888
#CMD ["/railo.sh"]
#CMD ["/start-apache2.sh"]
# Supervisord configuration
RUN mkdir /var/log/supervisor
ADD ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord"]
With your supervisord.conf file looking something like that :
[supervisord]
nodaemon=true
[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
[program:railo]
command=/bin/bash -c "exec /opt/railo/railo_ctl start -D FOREGROUND"
I found that the most convenient way of installing virtualenv + virtualenvwrapper is by using virtualenvburrito.
Now I can manage to automate my pip installs in a vagrant provision by the following:
Line in Vagrantfile:
config.vm.provision :shell, :path => "bootstrap.sh"
Lines in bootstrap.sh:
curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | $SHELL
source /root/.venvburrito/startup.sh
cd /vagrant
mkvirtualenv my_project
pip install -r requirements.txt
Then I run vagrant ssh but then I have to run the following to access my virtual environment:
sudo -i
source /root/.venvburrito/startup.sh
workon my_project
I don't want to always have to run sudo -i and source /root/.venvburrito/startup.sh, I just want to be able to run workon my_project directly.
But
(I.) I can't seem to append source /root/.venvburrito/startup.sh to my ~/.profile and
(II.) even if it was appended to that file I'd get a permissionerror. I can't seem to change the permissions for any protected file either.
The best way to deal with (I.) and (II.) is to set the privileged attribute in the Vagrantfile to false.
See here