Docker: Difference between revisions

From Alpine Linux
m (make addgroup command copy and pastable)
 
(41 intermediate revisions by 24 users not shown)
Line 1: Line 1:
Alpine makes a great docker container, because it is so small and optimized to be run in RAM.
It might also might make a good controller for several docker containers with enough RAM.  I haven't tested this yet
Docker's setup is easy to use from command line.  Commands can be run from an interactive shell, or through a configuration file called a "Dockerfile".
docker.com has excellent walk-throughs on how to run, pull, setup a container, commit an image, and create a configuration file.  hub.docker.com is a freemium setup, where the first private repository is free.
== Installation ==
== Installation ==
Run <code>apk add docker</code> to install Docker on Alpine Linux.
The Docker package is in the 'Community' repository, so if the '''apk add''' fails with '''unsatisfiable constraints''', you need to edit the '''/etc/apk/repositories''' file to add (or uncomment) a line like:
<code>http://dl-cdn.alpinelinux.org/alpine/edge/community</code>
then run <code>apk update</code> to index the repository.


'''To start the Docker daemon at boot, run:'''
The {{Pkg|docker}} package is in the ''community'' repository. See [[Repositories]] how to add a repository.


<code>rc-update add docker boot</code>
apk add docker


Connecting to the Docker daemon through its socket requires you to add yourself to the <code>docker</code> group.


'''Then to start the Docker daemon manually, run:'''
addgroup ${USER} docker


<code>service docker start</code>
To start the Docker daemon at boot, see [[OpenRC]].


rc-update add docker default
service docker start


{{Note|On older version of Alpine Linux with older version of docker you'll also need to disable some kernel security flags in order to build images:}}
=== Docker rootless ===


<code>sysctl -w kernel.grsecurity.chroot_deny_chmod=0</code>
Docker rootless allows unprivileged users to run the docker daemon and docker containers in user namespaces. This is not the same as dockremap explained in the section below. With dockremap the daemon still runs as root.


<code>sysctl -w kernel.grsecurity.chroot_deny_mknod=0</code>
This requires the {{Pkg|docker-rootless-extras}} package (available in ''community'') and enabling <code>cgroups v2</code>:
edit <code>/etc/rc.conf</code> and set <code>rc_cgroup_mode="unified"</code>. Then start the service on boot:


For more information, have a look at the [https://github.com/docker/docker/issues/20303 corresponding Github issue].
rc-update add cgroups


Anyway, this weakening of security is not necessary to do with Alpine 3.4.x and Docker 1.12 as of August 2016 anymore.
Additionally, the <code>/etc/subuid</code> and <code>/etc/subgid</code> files need to be set up as explained in [https://docs.docker.com/engine/security/rootless/ the official documentation].


=== Docker Compose ===
=== Docker Compose ===


'''To install docker-compose, first install pip:'''
{{Pkg|docker-cli-compose}} is in the ''community'' repository starting with Alpine Linux 3.15.
 
<code>apk add py-pip</code>


 
apk add docker-cli-compose
'''Then install docker-compose, run:'''
 
<code>pip install docker-compose</code>


== Isolate containers with a user namespace ==
== Isolate containers with a user namespace ==
<pre>
<pre>
adduser -SDHs /sbin/nologin dockremap
adduser -SDHs /sbin/nologin dockremap
addgroup -S dockremap
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f3):65536 >> /etc/subuid
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f3):65536 >> /etc/subuid
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f4):65536 >> /etc/subgid
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f4):65536 >> /etc/subgid
</pre>
</pre>


and add in '''/etc/conf.d/docker'''
add to <code>/etc/docker/daemon.json</code>


<pre>
<pre>
DOCKER_OPTS="--userns-remap=default"                  
        "userns-remap": "dockremap"
}
</pre>
</pre>


''You may also consider these options : '''--icc=false --no-new-privileges'''''
''You may also consider these options : ''
        "experimental": false,
        "live-restore": true,
        "ipv6": false,
        "icc": false,
        "no-new-privileges": false'''''


PS: it's also here (at DOCKER_OPTS) you must specify the '''-H tcp://$IP:$PORT -H unix:///var/run/docker.sock'''
You'll find all possible configurations [https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file here].


== Example: How to install docker from Arch ==
== Example: How to install docker from Arch ==
Line 67: Line 60:
https://wiki.archlinux.org/index.php/Docker
https://wiki.archlinux.org/index.php/Docker


== '''How to use docker''' ==
== "WARNING: No {swap,memory} limit support" ==
 
You might encounter this message when executing <code>docker info</code>.
To correct this situation, we have to enable the <code>cgroup_enable=memory swapaccount=1</code>
 
==== Alpine 3.8 ====
It may not have been the case before, but with Alpine 3.8, you must configure cgroups properly
 
{{Warning|This seems ''not'' to work with Alpine 3.9 and Docker 18.06. Follow the instructions for grub or extlinux below instead.}}
 
<pre>echo "cgroup /sys/fs/cgroup cgroup defaults 0 0" >> /etc/fstab</pre>
<pre>
cat >> /etc/cgconfig.conf <<EOF
mount {
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
}
EOF
</pre>
 
=== Grub ===
If you use Grub, add the cgroup condition into <code>/etc/default/grub</code>, then upgrade your grub
 
<pre>GRUB_CMDLINE_LINUX_DEFAULT="... cgroup_enable=memory swapaccount=1"</pre>
 
=== Extlinux ===
With Extlinux, you add the cgroup condition, but inside of <code>/etc/update-extlinux.conf</code>
 
<pre>default_kernel_opts="... cgroup_enable=memory swapaccount=1"</pre>
 
then update the config and reboot
 
<code>update-extlinux</code>


The best documentation for how to use Docker and create containers is at the main docker site.  Adding anything more to it here would be redundant.
== How to use docker ==


'''http://docs.docker.com/'''
Check the [https://docs.docker.com/ official documentation] for details on general usage of docker, including creating and management of containers. Repeating these instructions here be redundant.


if you create an account at docker.com you can browse through other user's images and learn from the syntax in contributor's dockerfiles.
Public images can be browsed at the [https://hub.docker.com/ Docker Hub]. These should also serve as further reference on the Dockerfile format.


Official Docker image files are denoted by a blue ribon on the website.
Official Docker image files are denoted on the website by a special badge.


== See also ==
== See also ==
* [https://www.erianna.com/creating-a-alpine-linux-repository/ Creating & Hosting an Alpine Linux Package Repository for Docker Packages]
* [https://www.erianna.com/creating-a-alpine-linux-repository/ Creating and Hosting an Alpine Linux Package Repository for Docker Packages]
* [[Running Alpine in a Docker Container]]


[[Category:Virtualization]]
[[Category:Virtualization]]

Latest revision as of 23:44, 28 August 2024

Installation

The docker package is in the community repository. See Repositories how to add a repository.

apk add docker

Connecting to the Docker daemon through its socket requires you to add yourself to the docker group.

addgroup ${USER} docker

To start the Docker daemon at boot, see OpenRC.

rc-update add docker default
service docker start

Docker rootless

Docker rootless allows unprivileged users to run the docker daemon and docker containers in user namespaces. This is not the same as dockremap explained in the section below. With dockremap the daemon still runs as root.

This requires the docker-rootless-extras package (available in community) and enabling cgroups v2: edit /etc/rc.conf and set rc_cgroup_mode="unified". Then start the service on boot:

rc-update add cgroups

Additionally, the /etc/subuid and /etc/subgid files need to be set up as explained in the official documentation.

Docker Compose

docker-cli-compose is in the community repository starting with Alpine Linux 3.15.

apk add docker-cli-compose

Isolate containers with a user namespace

adduser -SDHs /sbin/nologin dockremap
addgroup -S dockremap
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f3):65536 >> /etc/subuid
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f4):65536 >> /etc/subgid

add to /etc/docker/daemon.json

{  
        "userns-remap": "dockremap"
}

You may also consider these options :

       "experimental": false,
       "live-restore": true,
       "ipv6": false,
       "icc": false,
       "no-new-privileges": false

You'll find all possible configurations here.

Example: How to install docker from Arch

https://wiki.archlinux.org/index.php/Docker

"WARNING: No {swap,memory} limit support"

You might encounter this message when executing docker info. To correct this situation, we have to enable the cgroup_enable=memory swapaccount=1

Alpine 3.8

It may not have been the case before, but with Alpine 3.8, you must configure cgroups properly

Warning: This seems not to work with Alpine 3.9 and Docker 18.06. Follow the instructions for grub or extlinux below instead.


echo "cgroup /sys/fs/cgroup cgroup defaults 0 0" >> /etc/fstab
cat >> /etc/cgconfig.conf <<EOF
mount {
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
}
EOF

Grub

If you use Grub, add the cgroup condition into /etc/default/grub, then upgrade your grub

GRUB_CMDLINE_LINUX_DEFAULT="... cgroup_enable=memory swapaccount=1"

Extlinux

With Extlinux, you add the cgroup condition, but inside of /etc/update-extlinux.conf

default_kernel_opts="... cgroup_enable=memory swapaccount=1"

then update the config and reboot

update-extlinux

How to use docker

Check the official documentation for details on general usage of docker, including creating and management of containers. Repeating these instructions here be redundant.

Public images can be browsed at the Docker Hub. These should also serve as further reference on the Dockerfile format.

Official Docker image files are denoted on the website by a special badge.

See also