Docker Compose
Docker Compose
Raspberry Pi OSThanks Ahmed MerabetAbrir la Terminal--Open Terminal
sudo apt install -y uidmap
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
Ahora debemos decidir si queremos o no correr docker sin ser root, si tu respuesta es no, saltate este paso, si es afirmativa ingresa el siguiente comando--Now we have to choose run docker like root or non-root-user, if you want run like root jump this step, if you want run like non-root-user introduce this command:
dockerd-rootless-setuptool.sh install
Vemos un mensaje sobre dependencias que nos faltan, copiamos el mensaje en la Terminal para que se instale--We see that need some dependencies, so copy the message and paste on Terminal.
sudo sh -eux <<EOF
# Install newuidmap & newgidmap binaries
apt-get install -y uidmap
EOF
Volvemos a ejecutar--Run again:
dockerd-rootless-setuptool.sh install
Vemos un mensaje que nos advierte soobre agregar 2 variables al bashrc--Then we see another message about add 2 variables to bashrc:
cd
nano .bashrc
Ingresa las 2 líneas al final del archivo--Add this 2 lines to the end of file:
export PATH=/usr/bin:$PATH
export DOCKER_HOST=unix:///run/user/1000/docker.sock
Guardamos, cerramos y reiniciamos la Raspberry Pi--Close and save with Ctrl+x, Y, Enter then reboot the Raspberry Pi:
sudo reboot
Agregamos nuestro usuario al grupo docker--Add our user to docker's group(Optional):
sudo usermod -aG docker $USER
Tras reiniciar abrimos la Terminal y vemos la versión--After reboot open Terminal and check docker compose version:
docker compose version
Docker Compose Version v2.21.0.
Probamos su funcionamiento con un ejemplo--Test with an example:
Esta es la forma normal de correr docker--This is the docker way:
sudo docker run --name web -itd -p 8080:80 nginx
Esta es la forma en que hacemos correr nuestro contenedor con Docker compose--This is the Docker Compose way to run a container:
Abrimos otra Terminal sin cerrar la anterior--Open another Terminal without close the other:
cd
mkdir coffeetime
cd coffeetime
nano docker-compose.yaml
Ingresamos--Type:
version: "3.9"
services:
website:
image: nginx
ports:
- "8081:80"
restart: always
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Lo ejecutamos--Run:
sudo docker compose up -d
Notemos que a diferencia del video de Network Chuck no hemos instalado docker.io ni docker-compose desde el repositorio, es por esto que al ejecutar ingresamos docker compose en lugar de docker-compose.
Notice that at difference of Network Chuck video, we don't install docker.io and docker-compose from repo, is for this that when run it we use docker compose instead of docker-compose.
Abrimos el navegador e introducimos--Open browser and type:
localhost:8081
Te preguntarás cuál es la diferencia?, en este ejemplo se ve más sencillo solo utilizar docker run, pero que si queremos correr 314 contenedores?. Bueno, necesitariamos ejecutar 314 veces docker run, pero con docker compose solo editamos un archivo (docker-compose.yaml) con las configuraciones de los 314 contenedores y luego solo ejecutamos una linea:
sudo docker compose up -d
Solo una, para correr 314 contenedores(314 es pi * 100 (^- *) ).Esto lo podemos observar en el siguiente ejemplo del video de Network Chuck.
Which is the diferencey you ask, in this example seems to be more easy just use docker run, but what if you want to run 314 containers? Well you need to execute 314 times docker run. But with docker compose you build one file (docker-compose.yaml) with the 314 containers setup then just run it all with:
sudo docker compose up -d
Just one line to run 314 containers(314 is pi by 100 (^- *) ). This we can see in the next example of Network Chuck Video.
Wordpress-MariaDB-PhpMyAdmin-Apache:
Raspberry pi Os 32bit & 64bitRaspberry Pi OS 32 bit
Abre la--Open Terminal:
cd
mkdir webpage
cd webpage
nano docker-compose.yaml
Escribimos--Type:
En ports, si lo tenemos ocupado cambiamos por otro--If we have ports busy, change by another
version: '3.9'
services:
wordpress:
image: wordpress:5.7.2
ports:
- 80:80
environment:
- "WORDPRESS_DB_USER=root"
- "WORDPRESS_DB_PASSWORD=vFvpKjJ7HUbkD3wyLDp4"
restart: always
dns: 8.8.8.8
volumes:
- /srv/wordpress:/var/www/html
mysql:
image: jsurf/rpi-mariadb
volumes:
- /srv/wordpress-mysql:/var/lib/mysql
environment:
- "MYSQL_ROOT_PASSWORD=vFvpKjJ7HUbkD3wyLDp4"
- "MYSQL_DATABASE=wordpress"
restart: always
phpmyadmin:
image: phpmyadmin:apache
environment:
- PMA_ARBITRARY=1
restart: always
ports:
- 9999:80
volumes:
- /sessions
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Lo ejecutamos--Run:
sudo docker compose up -d
Raspberry Pi OS 64 bit
Abre la--Open Terminal:
Clonaremos la repo awsome-compose--We will clone the repo awesome-compose.
cd
git clone https://github.com/docker/awesome-compose.git
cp -r awesome-compose/wordpress-mysql/ .
cd wordpress-mysql
sudo docker compose up -d
Abrimos el navegador--Open the browser:
http://localhost
Detener y borrar todo si lo deseamos con--Stop and delete all if you wish:
sudo docker compose stop
sudo docker compose down
sudo docker compose down -v
sudo docker network rm wordpress-mysql_default
Portainer
Abre la--Open Terminal:
cd
mkdir portainer
cd portainer
nano docker-compose.yaml
Escribimos--Type:
En ports, si lo tenemos ocupado cambiamos por otro--If we have ports busy, change by another
services:
portainer:
image: portainer/portainer-ce:alpine
container_name: portainer
command: -H unix:///var/run/docker.sock
ports:
- "9000:9000"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "portainer_data:/data"
restart: always
volumes:
portainer_data:
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Lo ejecutamos--Run:
sudo docker compose up -d
PHP application with Apache2
Abre la--Open Terminal:
cd
mkdir apache
cd apache
mkdir app
nano app/Dockerfile
Escribimos--Type:
# syntax=docker/dockerfile:1.4
FROM --platform=$BUILDPLATFORM php:8.0.9-apache as builder
CMD ["apache2-foreground"]
FROM builder as dev-envs
RUN <<EOF
apt-get update
apt-get install -y --no-install-recommends git
EOF
RUN <<EOF
useradd -s /bin/bash -m vscode
groupadd docker
usermod -aG docker vscode
EOF
# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /
CMD ["apache2-foreground"]
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Hacemos el archivo de la web--Make an index web file:
nano app/index.php
Escribimos--Type:
<?php
echo '<h1>Remember recommend Raspberry Pi Buenos Aires to your friends</h1>';
?>
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Ahora creamos el archivo yaml--Now the yaml file:
nano docker-compose.yaml
Escribimos--Type:
En ports, si lo tenemos ocupado cambiamos por otro--If we have ports busy, change by another
services:
web:
build:
context: app
target: builder
ports:
- '80:80'
volumes:
- ./app:/var/www/html/
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Lo ejecutamos--Run:
sudo docker compose up -d
O clonando desde Awesome-Compose Repo--Or Clone Awesome-Compose Repo
Abre la--Open Terminal:
Clonaremos la repo awsome-compose--We will clone the repo awesome-compose.
cd
git clone https://github.com/docker/awesome-compose.git
cp -r awesome-compose/apache-php .
cd apache-php
docker compose up -d
Abrimos el navegador--Open the browser:
http://localhost
MariaDB & PhpMyAdmin
Raspberry Pi OS 64bitAbre la--Open Terminal:
cd
mkdir mariamyadmin
cd mariamyadmin
nano docker-compose.yaml
Escribimos--Type:
# Use root/example as user/password credentials
version: '3.9'
services:
db:
image: mariadb
restart: always
environment:
MARIADB_ROOT_PASSWORD: example
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
Guardar y cerrar--Close and save with Ctrl+x, Y, Enter.
Lo ejecutamos--Run:
sudo docker compose up -d
Abrimos el navegador e iniciamos sesion--Open the browser and login:
http://localhost:8080
Server: Leave empty
User: root
Password: example
Angular
Raspberry Pi OS 64bitAbre la--Open Terminal:
Clonaremos la repo awsome-compose--We will clone the repo awesome-compose.
cd
git clone https://github.com/docker/awesome-compose.git
cp -r awesome-compose/angular .
cd angular
docker compose up -d
Abrimos el navegador--Open the browser:
http://localhost:4200
ASP.net & Microsoft My SQL Server app
Abre la--Open Terminal:
Clonaremos la repo awsome-compose--We will clone the repo awesome-compose.
cd
git clone https://github.com/docker/awesome-compose.git
cp -r awesome-compose/aspnet-mssql/ .
cd aspnet-mssql
sudo docker compose up -d
Abrimos el navegador--Open the browser:
http://localhost
Detenemos la app y borramos todo con--Stop and delete the app with:
sudo docker compose stop
sudo docker compose down
sudo docker compose down -v
Comandos Básicos--Basic Commands:
Correr Docker--Run Docker:
sudo docker compose up -d
Detener Docker--Stop Docker:
sudo docker compose down
Eliminar volumenes creados--Delete volumes:
sudo docker compose down -v
Eliminar Huerfanos-- Remove Orphans:
sudo docker compose down --remove-orphans
Listar proyectos Docker Compose encendidos--List run projects:
sudo docker compose ps
Usage: docker compose [OPTIONS] COMMAND
Docker Compose
Options:
--ansi string Control when to print ANSI control characters ("never"|"always"|"auto")
(default "auto")
--compatibility Run compose in backward compatibility mode
--env-file string Specify an alternate environment file.
-f, --file stringArray Compose configuration files
--profile stringArray Specify a profile to enable
--project-directory string Specify an alternate working directory
(default: the path of the, first specified, Compose file)
-p, --project-name string Project name
Commands:
build Build or rebuild services
convert Converts the compose file to platform's canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service.
down Stop and remove containers, networks
events Receive real time events from containers.
exec Execute a command in a running container.
images List images used by the created containers
kill Force stop service containers.
logs View output from containers
ls List running compose projects
pause Pause services
port Print the public port for a port binding.
ps List containers
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service.
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker Compose version information
Github:
Docs:
Awesome docker compose Samples:
How to Write a Docker Compose File:
Language specific guides:
Build and Run an Image guide:
Home Assistant
Abre la--Open Terminal:
cd
mkdir homeAssistant
cd homeAssistant
nano docker-compose.yaml
Escribimos--Type:
En ports, si lo tenemos ocupado cambiamos por otro--If we have ports busy, change by another
version: '3'
services:
homeassistant:
container_name: homeassistant
image: "ghcr.io/home-assistant/home-assistant:stable"
volumes:
- /PATH_TO_YOUR_CONFIG:/config
- /etc/localtime:/etc/localtime:ro
- /run/dbus:/run/dbus:ro
restart: unless-stopped
privileged: true
network_mode: host
Guardar y cerrar--Close and save with Ctrl+S, Ctrl+x.
Lo ejecutamos--Run:
docker compose up -d
Si usamos root para docker compose:
sudo docker compose up -d
Accedemos al dashboard desde el navegador, reemplazamos <host> con nuestra IP:
http://<host>:8123
Ejemplo:
http://192.168.0.33:8123
Otra manera de instalación:
docker run -d \
--name homeassistant \
--privileged \
--restart=unless-stopped \
-e TZ=MY_TIME_ZONE \
-v /PATH_TO_YOUR_CONFIG:/config \
-v /run/dbus:/run/dbus:ro \
--network=host \
ghcr.io/home-assistant/home-assistant:stable
cd
mkdir HASS
MY_TIME_ZONE:
Debe ser reemplazado por la zona horaria de tu residencia, puedes averiguar la tuya aquí, en mi caso en buenos aires quedando:
-e TZ=America/Argentina/Buenos_Aires \
/PATH_TO_YOUR_CONFIG
Debe ser reemplazado por el directorio que creamos:
-v /HASS:/config \
docker run -d \
--name homeassistant \
--privileged \
--restart=unless-stopped \
-e TZ=America/Argentina/Buenos_Aires \
-v /HASS:/config \
-v /run/dbus:/run/dbus:ro \
--network=host \
ghcr.io/home-assistant/home-assistant:stable
Lo ejecutamos--Run:
docker compose up -d
Si usamos root para docker compose:
sudo docker compose up -d
Accedemos al dashboard desde el navegador, reemplazamos <host> con nuestra IP:
http://<host>:8123
Ejemplo:
http://192.168.0.33:8123
Docker Compose Reference:
Common Reference:
Run:
docker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...]
Start:
docker compose start [SERVICE...]
Stop:
docker compose stop [OPTIONS] [SERVICE...]
Up:
docker compose up [OPTIONS] [SERVICE...]
Down:
docker compose down [OPTIONS] [SERVICES]