Docker
Docker for local development
I use Docker Desktop for local development servers.
-
Docker Desktop provides a good interface to manage containers. It makes it easy to look at logs, run a terminal in a container, manage images, and more.
-
Docker Desktop is "the same" across Linux, Mac, and Windows, so hopefully my notes are useful to more people.
-
Using Docker containers for everything makes it easier to setup all kinds of services. Everything is declared in source files.
Files relevant for Docker
.
|-- .env (1)
|-- .gitignore (2)
|-- Dockerfile (3)
`-- docker-compose.yml (4)
1 | Local environment variables to be added. These contain for instance local passwords or configuration. |
2 | The .env file should be in .gitignore ,
and not be included in git version control. |
3 | An optional specification of the application server. |
4 | Specification of the running environment. This usually consists of the application server, one or more databases, and some utility services like a database manager. |
With Docker running, start the services as daemons, using:
$ docker compose up -d
General remarks
-
The
.env
file is automatically read by Docker Desktop when starting up services. However, you need to set up some dotenv helper to have those variables also set in your shell. -
By default, Docker generates a project name based on the base directory of the
docker-compose.yml
file. It is possible to override this by setting an environment variable (for instance in the .env file):COMPOSE_PROJECT_NAME=myproject
-
I usually bind mount the local application sources in the container. However, setting the right user and group id inside a container, so that files generated by the server can also be edited locally is often tricky.
-
Each service in Docker gets its own local domain name. To let for instance an application server also reach these service via
localhost
, add the following to its definition indocker-compose.yml
:extra_hosts: - localhost:host-gateway