Docker (software): Difference between revisions

 
(8 intermediate revisions by the same user not shown)
Line 157: Line 157:


===Compose file===
===Compose file===
See [https://github.com/compose-spec/compose-spec/blob/master/spec.md#compose-file compose-file specs] and [https://docs.docker.com/compose/compose-file/compose-file-v3/ compose file v3 reference].
See [https://docs.docker.com/compose/compose-file/compose-file-v3/ Compose file specification].


Previously, the Compose file (<code>docker-compose.yml</code>) required a version. Version 2 and version 3 had different options and not all options from version 2 were available in version 3. However, as of docker-compose v1.27+, you should no longer specify a version and options from both versions are supported.
Previously, the Compose file (<code>docker-compose.yml</code>) required a version. Version 2 and version 3 had different options and not all options from version 2 were available in version 3. However, as of docker-compose v1.27+, you should no longer specify a version and options from both versions are supported.


{{hidden | Example docker-compose.yml |
{{hidden | Example docker-compose.yml |
<syntaxhighlight lang="yaml">
<syntaxhighlight lang="yaml">
version: "3.9"
services:
services:
   web:
   web:
     image: registry.gitlab.davidl.me/dli7319/davidl_me:latest
     image: registry.gitlab.davidl.me/dli7319/davidl_me:latest
    restart: unless-stopped
</syntaxhighlight>
</syntaxhighlight>
}}


==Accessing the Host==
==Accessing the Host==
Line 184: Line 184:
* You do not need to add <code>expose</code>.
* You do not need to add <code>expose</code>.


By default, networks are allocated with ip ranges:
* 172.17.0.0/12 with size /16
* 192.168.0.0/16 with size /20
If you want this to be more consistent, you can change it as follows:
Set the following in <code>/etc/docker/daemon.json</code>:
Set the following in <code>/etc/docker/daemon.json</code>:
<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
Line 193: Line 197:
</syntaxhighlight>
</syntaxhighlight>
Then restart your docker: <code>sudo systemctl restart docker</code> and prune networks <code>docker network prune</code>.<br>
Then restart your docker: <code>sudo systemctl restart docker</code> and prune networks <code>docker network prune</code>.<br>
This will force docker to assign subnets from 172.16.0.0/24 to 172.32.255.0/24 instead of 172.16.0.0/12 to 172.32.0.0/12.<br>
Thus it won't overflow to 192.168.0.0/24.<br>
Next, in your firewall, allow connections to your localhost from 172.16.0.0/12.
Next, in your firewall, allow connections to your localhost from 172.16.0.0/12.
<pre>
<pre>
ufw allow from 172.16.0.0/12 to any comment "from_docker"
ufw allow from 172.16.0.0/12 to any comment "from_docker"
</pre>
</pre>
See also https://hub.docker.com/r/defreitas/dns-proxy-server.<br>
This modifies your host system's DNS resolver so that you can access docker containers by their hostname.<br>
Additionally,you can access the host using <code>host.docker</code>.


==Registries==
==Registries==
Line 210: Line 208:
Alternative public registries:
Alternative public registries:
* [https://gallery.ecr.aws/ AWS ECR Gallery] has a mirror for all official docker containers.
* [https://gallery.ecr.aws/ AWS ECR Gallery] has a mirror for all official docker containers.
==Caching==
If you want your builds to be fast on CICD, you have to setup [https://docs.docker.com/build/cache/ caching].
In particular you should:
* Enable [https://docs.docker.com/build/buildkit/ buildkit] by setting the environment variable <code>DOCKER_BUILDKIT=1</code>
* Use <code>--cache-from</code> in your build.
* Setup external caching, e.g. with <code>--build-arg BUILDKIT_INLINE_CACHE=1</code>.
** Use <code>--arg BUILDKIT_INLINE_CACHE=1</code> if using <code>docker buildx build</code>.
* For multistage builds, cache each stage in your container registry.
;Resources
* https://michalwojcik.com.pl/2021/01/21/using-cache-in-multi-stage-builds-in-gitlab-ci-docker/
* https://testdriven.io/blog/faster-ci-builds-with-docker-cache/#multi-stage-builds
==Useful Services==
* [https://containrrr.dev/watchtower/ https://containrrr.dev/watchtower/] is a tool which will automatically update your docker containers when new images are published. It also has an http endpoint to trigger checks manually e.g. from CI/CD.
==My Images==
I have a few custom container images below:
* [https://github.com/dli7319/docker-anki-server ghcr.io/dli7319/docker-anki-server:main]
* [https://github.com/dli7319/docker-nextcloud ghcr.io/dli7319/docker-nextcloud:main]
* [https://github.com/dli7319/docker-mediawiki ghcr.io/dli7319/docker-mediawiki:main]


==Resources==
==Resources==
* [https://www.youtube.com/watch?v=fqMOX6JJhGo freeCodeCamp.org Docker Tutorial for Beginners Video]
* [https://www.youtube.com/watch?v=fqMOX6JJhGo freeCodeCamp.org Docker Tutorial for Beginners Video]