Working with containers is a necessity today for developers. You will likely need to work with some kind of storage or interact with some external service, like Solr or Postgres.
You can install the required service on your local machine, which comes with the burden of choosing the correct installation, configuration, etc… Nothing too complicated but takes time.
Docker and Containers
In short, the container is an isolated process on the host machine. It runs in the isolated file system, a container image. Docker would be a platform, a set of tools, for building, running, sharing applications packed in the container image.
If I need to run some application like the a Postgres database I can:
- download installation for target host(my local machine)
- or run Postgres Docker image
The easiest solution these days would be searching docker image on dockerhub and running it locally. That way, I skip all configuration issues and go into the depths of Postregs, because I want quick results.
For example, if I want to run Postgres database I can do something like this:
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 -d postgres
Above would spin Postgres database in a container using Docker on my machine. If I need some other service, I can search for its Docker image and run it similarly (first checking documentation for a particular service). Now you can access the local Postgres database with:
psql -h localhost -p 5432 -U postgres
Or just the compose is the tool for building, running, and managing multi-container Docker applications. The compose allows any developer to define application parameters when running it. Everything you need to describe or define a Docker application is stored in one YAML file like:
version: "3.9" services: app: container_name: echo build: . ports: - "9999:9999" next: container_name: echo2 build: . ports: - "8888:8888" pg: container_name: pg image: postgres:13.1 healthcheck: test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ] timeout: 45s interval: 10s retries: 10 restart: always environment: - POSTGRES_USER=root - POSTGRES_PASSWORD=password - APP_DB_USER=docker - APP_DB_PASS=docker - APP_DB_NAME=docker volumes: - ./db:/docker-entrypoint-initdb.d/ ports: - 5432:5432
In the example above, there are three containers: app, next, and pg. The pg service uses Docker image
postgres:13.1. The app and next context are built from Dockerfile stored in the same folder as this compose
When starting this docker application, the compose checks if Docker images for app and next services exist. If any of them do not, the compose triggers Docker build process based on the available Dockerfile.
Start Docker application, and detach output:
docker-compose up -d
To stop all services at once:
Some useful docker-compose commands
List running docker applications:
Checks service logs:
docker-compose logs pg -f