Diferencia entre revisiones de «Docker Engine»

De Wiki Castanedo.es
Ir a la navegación Ir a la búsqueda
Sin resumen de edición
Sin resumen de edición
 
(No se muestran 3 ediciones intermedias del mismo usuario)
Línea 75: Línea 75:
</syntaxhighlight>
</syntaxhighlight>


Nota: es toda esta configuración estamos asumiendo que estamos usando la zona public.
=== Consideraciones importantes ===
* Toda esta configuración estamos asumiendo que estamos usando la zona public.
* Cuando se creen contenedores en Docker hay que evitar usar la opción -p (--port), ya que creará reglas en el firewall (iptables) que habilitarán acceso directo desde Internet a los contenedores.
* Mi recomendación es no hacerlo así y exponer los servicios a través de un proxy inverso (ver artículo sobre servidor [[LEMP]]).
 
== Desinstalar versiones anteriores de Docker Engine ==
Comprobar que no existe una versión de Docker Engine instalada.
<syntaxhighlight lang="Bash">apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc | cut -f1)</syntaxhighlight>
 
== Añadir repositorios APT oficiales de Docker ==
<syntaxhighlight lang="Bash">apt update</syntaxhighlight>
<syntaxhighlight lang="Bash">apt install ca-certificates curl</syntaxhighlight>
<syntaxhighlight lang="Bash">install -m 0755 -d /etc/apt/keyrings</syntaxhighlight>
<syntaxhighlight lang="Bash">curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc</syntaxhighlight>
<syntaxhighlight lang="Bash">chmod a+r /etc/apt/keyrings/docker.asc</syntaxhighlight>
<syntaxhighlight lang="Bash">
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF
</syntaxhighlight>
 
== Instalar Docker Engine ==
<syntaxhighlight lang="Bash">apt update</syntaxhighlight>
<syntaxhighlight lang="Bash">apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin</syntaxhighlight>
 
== Comprobar Docker Engine ==
=== Comprobar servicio ===
<syntaxhighlight lang="Bash">
systemctl status docker
● docker.service - Docker Application Container Engine
    Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: enabled)
    Active: active (running) since Thu 2026-02-26 22:52:45 UTC; 2min 39s ago
TriggeredBy: ● docker.socket
      Docs: https://docs.docker.com
  Main PID: 23159 (dockerd)
      Tasks: 11
    Memory: 26.5M (peak: 29.2M)
        CPU: 284ms
    CGroup: /system.slice/docker.service
            └─23159 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
 
Feb 26 22:52:44 culturetas.net dockerd[23159]: time="2026-02-26T22:52:44.065056936Z" level=info msg="Deleting nftables IPv4 rules" error="exit s>
Feb 26 22:52:44 culturetas.net dockerd[23159]: time="2026-02-26T22:52:44.088078290Z" level=info msg="Deleting nftables IPv6 rules" error="exit s>
Feb 26 22:52:44 culturetas.net dockerd[23159]: time="2026-02-26T22:52:44.118636746Z" level=info msg="Firewalld: created docker-forwarding policy"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.117792420Z" level=info msg="Loading containers: done."
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.131108392Z" level=info msg="Docker daemon" commit=6bc6209 containerd-sn>
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.131332307Z" level=info msg="Initializing buildkit"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.149255659Z" level=info msg="Completed buildkit initialization"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.158529795Z" level=info msg="Daemon has completed initialization"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.158627568Z" level=info msg="API listen on /run/docker.sock"
Feb 26 22:52:45 culturetas.net systemd[1]: Started docker.service - Docker Application Container Engine.
</syntaxhighlight>
 
Si el servicio está apagado hay que arrancarlo y habilitarlo en el arranque:
<syntaxhighlight lang="Bash">systemctl start docker</syntaxhighlight>
<syntaxhighlight lang="Bash">systemctl enable docker</syntaxhighlight>
 
=== Desplegar contenedor de prueba (hello-world) ===
<syntaxhighlight lang="Bash">docker run hello-world</syntaxhighlight>
<syntaxhighlight lang="Bash">
docker ps --all
CONTAINER ID  IMAGE        COMMAND    CREATED        STATUS                    PORTS    NAMES
3995e4839733  hello-world  "/hello"  2 minutes ago  Exited (0) 2 minutes ago            elated_matsumoto
</syntaxhighlight>
<syntaxhighlight lang="Bash">docker stop elated_matsumoto</syntaxhighlight>
<syntaxhighlight lang="Bash">docker rm elated_matsumoto</syntaxhighlight>
<syntaxhighlight lang="Bash">
docker image list
                                                                                                                            i Info →  U  In Use
IMAGE                ID            DISK USAGE  CONTENT SIZE  EXTRA
hello-world:latest  ef54e839ef54      25.9kB        9.52kB
</syntaxhighlight>
<syntaxhighlight lang="Bash">docker image rm hello-world:latest</syntaxhighlight>


== Referencias ==
== Referencias ==

Revisión actual - 23:05 26 feb 2026

Instalación de Docker Engine en Ubuntu Server

El objetivo de este documento es instalar Docker Engine en Ubuntu Server 24.04.

Requisitos

Antes de instalar Docker hay que tener en cuenta una serie de consideraciones.

Usuario root

Todos los comandos que aquí se ponen, han de ejecutarse como root.

sudo -i

Desinstalar UFW

El firewall que viene por defecto en entornos tipo Debian (como Ubuntu) tiene problemas de compatibilidad con Docker Engine. Debido a que Docker crea reglas directamente con IPTables, se ocasiona que Docker cree reglas que se salten las reglas existentes. Para evitar que esto pase vamos a sustituir UFW por FirewallD (en modo IPTables), que evita este tipo de problemas.

ufw disable
systemctl disable ufw
systemctl stop ufw
apt remove --purge ufw

Instalar FirewallD

apt update
apt install firewalld
systemctl enable firewalld
systemctl start firewalld

Habilitar modo IPTables a FirewallD (opcional)

Hay problemas reportados de conexión entre contenedores de Docker cuando FirewallD está en modo nftables, se resuelven cambiando a modo IPTables.

vi /etc/firewalld/firewalld.conf
[...]
# FirewallBackend
# Selects the firewall backend implementation.
# Choices are:
#       - nftables (default)
#       - iptables (iptables, ip6tables, ebtables and ipset)
# Note: The iptables backend is deprecated. It will be removed in a future
# release.
#FirewallBackend=nftables
FirewallBackend=iptables
[...]
systemctl restart firewalld

Habilitar reglas anteriores

En mi paso prefiero habilitar puertos TCP en vez de servicios (aunque también es posible).

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --reload

Habilitar salida a Internet de contenedores

firewall-cmd --permanent --zone=public --add-masquerade
firewall-cmd --reload

Comprobar estado de FirewallD

firewall-cmd --list-all
public (default, active)
  target: default
  ingress-priority: 0
  egress-priority: 0
  icmp-block-inversion: no
  interfaces:
  sources:
  services: dhcpv6-client ssh
  ports: 80/tcp 443/tcp
  protocols:
  forward: yes
  masquerade: yes
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

Consideraciones importantes

  • Toda esta configuración estamos asumiendo que estamos usando la zona public.
  • Cuando se creen contenedores en Docker hay que evitar usar la opción -p (--port), ya que creará reglas en el firewall (iptables) que habilitarán acceso directo desde Internet a los contenedores.
  • Mi recomendación es no hacerlo así y exponer los servicios a través de un proxy inverso (ver artículo sobre servidor LEMP).

Desinstalar versiones anteriores de Docker Engine

Comprobar que no existe una versión de Docker Engine instalada.

apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc | cut -f1)

Añadir repositorios APT oficiales de Docker

apt update
apt install ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

Instalar Docker Engine

apt update
apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Comprobar Docker Engine

Comprobar servicio

systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Thu 2026-02-26 22:52:45 UTC; 2min 39s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 23159 (dockerd)
      Tasks: 11
     Memory: 26.5M (peak: 29.2M)
        CPU: 284ms
     CGroup: /system.slice/docker.service
             └─23159 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Feb 26 22:52:44 culturetas.net dockerd[23159]: time="2026-02-26T22:52:44.065056936Z" level=info msg="Deleting nftables IPv4 rules" error="exit s>
Feb 26 22:52:44 culturetas.net dockerd[23159]: time="2026-02-26T22:52:44.088078290Z" level=info msg="Deleting nftables IPv6 rules" error="exit s>
Feb 26 22:52:44 culturetas.net dockerd[23159]: time="2026-02-26T22:52:44.118636746Z" level=info msg="Firewalld: created docker-forwarding policy"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.117792420Z" level=info msg="Loading containers: done."
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.131108392Z" level=info msg="Docker daemon" commit=6bc6209 containerd-sn>
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.131332307Z" level=info msg="Initializing buildkit"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.149255659Z" level=info msg="Completed buildkit initialization"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.158529795Z" level=info msg="Daemon has completed initialization"
Feb 26 22:52:45 culturetas.net dockerd[23159]: time="2026-02-26T22:52:45.158627568Z" level=info msg="API listen on /run/docker.sock"
Feb 26 22:52:45 culturetas.net systemd[1]: Started docker.service - Docker Application Container Engine.

Si el servicio está apagado hay que arrancarlo y habilitarlo en el arranque:

systemctl start docker
systemctl enable docker

Desplegar contenedor de prueba (hello-world)

docker run hello-world
docker ps --all
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
3995e4839733   hello-world   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             elated_matsumoto
docker stop elated_matsumoto
docker rm elated_matsumoto
docker image list
                                                                                                                             i Info →   U  In Use
IMAGE                ID             DISK USAGE   CONTENT SIZE   EXTRA
hello-world:latest   ef54e839ef54       25.9kB         9.52kB
docker image rm hello-world:latest

Referencias