====== Docker basics ======
===== Installation =====
Read the docs
===== Web interface =====
use "portainer" [[https://github.com/portainer/portainer|Github repo]]
===== Remote API =====
use "sherpa" [[https://github.com/djenriquez/sherpa|Github repo]]
====== Websites ======
===== Nginx + php-fpm =====
[[http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/|Source]]
==== Create files ====
- Create a folder that will contain the config files and go inside. (The containers names will take the name of the folder)
- Create a folder named "www" (that will be mounted in /srv/www (equivalent of /var/www))
- Create a folder ./www/first_website and put your php code inside
- Create a folder ./www/second_website and put your php code inside
- Create a folder named "conf.d" (that will be mounted in /etc/nginx/conf.d)
- Create the following files :
=== docker-compose.yml ===
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./www:/srv/www
- ./conf.d:/etc/nginx/conf.d
links:
- php
php:
image: php:7-fpm
volumes:
- ./www:/srv/www
=== ./conf.d/site.conf ===
server {
index index.php index.html;
server_name php-docker.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /srv/www/first_website;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
=== ./conf.d/site2.conf ===
server {
index index.php index.html;
server_name php-docker2.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /srv/www/second_website;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
==== Run the containers ====
docker-compose up
===== Mysql =====
docker run \
--name="mysql-dck" \
-v "/path/to/my.cnf":"/etc/my.cnf" \
-e MYSQL_ROOT_PASSWORD=mypassword \
-e MYSQL_ROOT_HOST=172.17.0.1 \
-d \
mysql/mysql-server:5.6
Note: i was not able to use root credentials as /root/.my.cnf
==== Script to write to mysql ====
=== First solution ===
add this line to the previous command :
-v /tmp/mysql-temp:/tmp \
run this command :
docker exec mysql-dck /bin/sh -c 'mysql -u root -pmypassword < /tmp/mysql-temp/test-create.sql'
content of the file "test-create.sql" :
CREATE DATABASE IF NOT EXISTS `mybase` DEFAULT CHARACTER SET utf8;
GRANT ALL PRIVILEGES ON `mybase`.* TO 'foo'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
=== Second solution ===
run :
docker exec mysql-dck /bin/sh -c 'mysql -u root -pmypassword -e "CREATE DATABASE IF NOT EXISTS `mybase` DEFAULT CHARACTER SET utf8"'
docker exec mysql-dck /bin/sh -c 'mysql -u root -pmypassword -e "GRANT ALL PRIVILEGES ON `mybase`.* TO \'foo\'@\'localhost\' IDENTIFIED BY \'password\' WITH GRANT OPTION;"'
docker exec mysql-dck /bin/sh -c 'mysql -u root -pmypassword -e "FLUSH PRIVILEGES;"'
===== Docker and OpenSimulator (work in progress) =====
[[https://github.com/Makopo/docker-opensimulator-simple|Source]]
==== Dockerfile ====
# start from this image
FROM mono:5.2.0.215
# create an "opensim" user
RUN useradd -ms /bin/bash opensim
# get the opensim bin folder
COPY opensim.tar.gz /home/opensim/opensimulator/opensim.tar.gz
WORKDIR /home/opensim/opensimulator
RUN tar -zxf opensim.tar.gz && rm opensim.tar.gz
WORKDIR /home/opensim/opensimulator/bin
USER opensim
ENTRYPOINT ["mono", "--server", "OpenSim.exe"]
#CMD ["mono", "--server", "OpenSim.exe"]
#!/bin/bash
HOST_IP="192.168.1.217"
DEFAULT_SIMNAME="opensimulator"
# check if params
if [ $# -lt 1 ];then
echo "Simulator name was not set so default is opensimulator"
else
DEFAULT_SIMNAME=$1
fi
# get port
SIMPORT=$(/srv/docker/get_port opensimulator)
# define the sim name
SIMNAME="opensimulator-${SIMPORT}"
RADMINPORT=$((${SIMPORT} + 1))
REGIONPORT_START=$((${RADMINPORT} + 1))
REGIONPORT_END=$((${REGIONPORT_START} + 97))
# check if config folder exists
if [ ! -d "/srv/docker/opensimulator/sims/${SIMNAME}" ]; then
cp -r "/srv/docker/opensimulator/sims/default" "/srv/docker/opensimulator/sims/${SIMNAME}"
fi
# add the config to the inis
cat < "/srv/docker/opensimulator/sims/${SIMNAME}/config/02-network.ini"
[Const]
BaseHostname = "${HOST_IP}"
PublicPort = "${SIMPORT}"
PrivatePort = "${SIMPORT}"
[Network]
http_listener_port = ${SIMPORT}
[RemoteAdmin]
enabled = true
port = ${RADMINPORT}
access_password = "test"
enabled_methods = all
EOM
# run the container
docker run \
-d \
--name=${SIMNAME} \
-v /srv/docker/opensimulator/sims/${SIMNAME}/config:/home/opensim/opensimulator/bin/config \
-v /srv/docker/opensimulator/sims/${SIMNAME}/config-include:/home/opensim/opensimulator/bin/config-include \
-v /srv/docker/opensimulator/sims/${SIMNAME}/archives:/home/opensim/opensimulator/bin/archives \
-v /srv/docker/opensimulator/sims/${SIMNAME}/log:/home/opensim/opensimulator/bin/log \
-v /srv/docker/opensimulator/sims/${SIMNAME}/OpenSim.exe.config:/home/opensim/opensimulator/bin/OpenSim.exe.config \
--link mysql-dck:mysql \
-p ${SIMPORT}:${SIMPORT} \
-p ${RADMINPORT}:${RADMINPORT} \
-p ${REGIONPORT_START}-${REGIONPORT_END}:${REGIONPORT_START}-${REGIONPORT_END}/udp \
opensimulator
File to get port "get_port"
#!/bin/bash
# check if params
if [ $# -lt 1 ];then
echo $"Usage : get_next_available_sim_port "
exit;
fi
i="10100"
status=false
PORTS_FILE="/srv/docker/ports"
while [ $status = false ]
do
cat "${PORTS_FILE}" | grep $i &> /dev/null
if [ ! $? -eq 0 ]; then
echo "${i}"
echo "${1};${i}" >> "${PORTS_FILE}"
status=true
fi
i=$[$i+100]
if [ $i -gt 48000 ]; then
status=true
exit 1
fi
done
Script to delete port "delete-port"
#!/bin/bash
# check if params
if [ $# -lt 1 ];then
echo $"Usage : delete_port "
exit;
fi
PORTS_FILE="/srv/docker/ports"
#sed -i '/${1}/d' ${PORTS_FILE}
grep -v "${1}" ${PORTS_FILE} > temp; mv temp ${PORTS_FILE}
====== Some notes ======
===== Get container name =====
Get containers containing the letter "t" in name.
docker ps -a --format '{{.Names}}' --filter "name=t"
===== Get exposed ports =====
docker inspect --format='{{range $p, $conf := .HostConfig.PortBindings}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' containername
===== Docker one mysql instance =====
[[https://severalnines.com/blog/mysql-docker-containers-understanding-basics|Source]]
==== Run the container ====
docker run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql
==== Get container ip ====
This is optionnal but noted as reference.
docker inspect test-mysql | grep IPAddress
==== Run client ====
docker run --detach --name test-debian --link test-mysql:mysql debian
==== Enter client ====
docker exec -it test-debian bash
See that the ip is mapped in /etc/hosts.
==== Expose to the outside ====
docker run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" --publish 6603:3306 mysql
The mysql server will be accessible from the outside on the port 6603.
== User Namespaces ==
[[https://resourcepool.io/fr/2016/09/09/francais-le-user-namespace-dans-docker/|Source]]
[[https://www.jujens.eu/posts/en/2017/Jul/02/docker-userns-remap/|Source2]]
BEWARE IT CAN BREAK AN OLD DOCKER INSTALLATION !!!
groupadd -g 500000 dockremap &&
groupadd -g 501000 dockremap-user &&
useradd -u 500000 -g dockremap -s /bin/false dockremap &&
useradd -u 501000 -g dockremap-user -s /bin/false dockremap-user
echo "dockremap:500000:65536" >> /etc/subuid &&
echo "dockremap:500000:65536" >>/etc/subgid
{
"userns-remap": "default"
}
systemctl daemon-reload && systemctl restart docker
== Portainer ==
in normal mode :
docker run -d --privileged -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer
in usernamespace mode :
docker run -d --privileged --userns=host -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer
===== php-fpm nginx with php extensions =====
./docker-compose.yml
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./htdocs:/srv/www
- ./conf.d:/etc/nginx/conf.d
links:
- php
php:
build: ./docker/php
volumes:
- ./htdocs:/srv/www
./docker/php/Dockerfile
FROM php:7-fpm
RUN apt-get update && apt-get install -y --fix-missing zip libzip-dev libpng-dev libonig-dev \
&& docker-php-ext-install gd \
&& docker-php-ext-install mbstring \
&& docker-php-ext-enable gd \
&& docker-php-ext-enable mbstring
{{tag>docker}}