This doth be a machine-wrought text which may contain errors!
Docker as Automation
With Compose thou describest thy desired setup in a file and lettest Docker create the containers. Here thou beginnest with an Nginx web server from a ready-made image. Then mayst thou add a service and examine what is created of its own accord.
Before thou commencest: Docker Engine and Compose from [[Docker Level 1 - Get started|Docker Level 1]] must work. Run docker compose version in the Linux virtual machine. Thou needest no Flask app, Dockerfile, or GitHub account. [[Git on the command line Level 3 - YAML|The YAML reference]] giveth support to indentation and lists; thou maist read it without having forged a GitHub workflow.
First doth “Task 1” require thee to verify the web response. Updating is a lookup for later labours. “Task 2” permitte thee to try multiple services within the same project.
What is Docker Compose?
With Docker Compose thou dost describe one or more services within a file. In this exercise each service runneth but one container. Herein is used docker-compose.yml.
compose.yaml and docker-compose.yml are names which Compose doth support. Usest thou one of them in thy project folder, and execute the commands from that same place. Herein is used docker-compose.yml.
The provided YAML code block contains no comments (lines starting with #) to translate; therefore, it is returned unchanged as per the requirement to preserve executable content exactly.
services:
web:
image: nginx:latest
ports:
- "127.0.0.1:8083:80"
One command doth set up all:
docker compose up -d
# Starter tjenestene i bakgrunnen.
# Doth start the services in the background.
The Compose-file may be employed upon another host; yet thou mustst also transport requisite datae and local files, and ordain passwords, networks, and sundry settings. Tags such as latest may point unto a newer image in time to come. If the builds are to remain identical, thou mustst likewise bind which image is used.
Why is this automation?
Ponder yon distinction:
| Manual Setup | With Docker Compose |
|---|---|
| Install Nginx by hand | image: nginx:latest |
| Configure ports | ports: "80:80" |
| Install Grafana by hand | image: grafana/grafana:latest |
| Document all the steps | Describe desired setup in the Compose file |
| Repeat it all on next server | docker compose up -d |
The Compose-file is a part of technical documentation and describes the desired state. Check actual status with docker compose ps and logs. Justifications, test results and routines for backups must be documented additionally.
Peruse this modest example
services gathereth the services unto themselves. web is the appellation we bestow upon the web server. image chooseth a pre-fabricated vessel of images, whilst ports bindeth port 8083 upon the virtual machine’s loopback address to port 80 within the container. Henceforth, thou shalt trial from the virtual machine itself. The forging from source code shall arrive at Docker Level 2.
Profitable Docker-Commandments
| Command | What it doth accomplish |
|---|---|
docker compose up -d | Set all services a-running in the background |
docker compose down | Halt and expel the project’s containers and networks |
docker compose logs -f | Follow the logs as they unfold in real time |
docker compose pull | Fetch those images unto which the project’s tags point |
docker compose restart | Rekindle the project’s containers anew, without altering the image |
docker ps | Display the running containers |
When the Service Requieth Storage
To Preserve Data Beyond the Vessel expoundeth upon bound mounts and named volumes. The Nginx exercise below requireth no storehouse of records nor tethering to storage. Employ the aid of preservation ere thou expandest thy project with a service that must retain its treasures.
Update of Services
To update a Docker service doth prove most simple:
# Fetch the latest version
docker compose pull
# Restart with the new version
docker compose up -d
up -d doth employ the fresh image by recreating the vessel anew. Verify that the service doth function post-update; a new iteration of the image may demand alterations unto its settings or data.
Task I - To Set Forth Nginx with Docker Compose
Labour thou within the console of that virtual machine. Create a fresh folder, lest this exercise alter another project of Compose:
# Forthwith create a place for composition of other matters
mkdir compose-ovelse
# Proceed thither into that very place
cd compose-ovelse
# Open with quill pen the scroll named docker-compose.yml
nano docker-compose.yml
If the folder doth exist already, open it forthwith and examine its contents ere thou proceedest further. Inscribe the Nginx example hailing from “What is Docker Compose?” into the file. Save with Ctrl+O, confirm with Enter, and conclude with Ctrl+X. Verify and commence:
docker compose config --quiet
docker compose up -d
docker compose ps
curl http://127.0.0.1:8083
The command config --quiet shall conclude without any error. Shouldst thou amend a fault within the file, run the verification anew ere commencement. The ps command must display web as in motion, and curl shall reveal the contents of Nginx bearing the words “Welcome to nginx!”.
If the port be occupied, choose another vacant host port within the file and employ that same port in curl. If the vessel doth cease its labor, read upon docker compose logs web. Thou shalt not commence a fresh endeavor in an arbitrary folder merely to attempt again.
Explain before proceeding: Which file describeth this service, and what did docker compose up -d create? Cease the project with docker compose stop, raise it once more with docker compose up -d, and repeat the trial via curl. Dost thou use this page as support for Compose in some other task? Then return thither now.
Task the Second - To Add a Service
Expand upon docker-compose.yml, as set forth in task one, with an additional service of thine own devising. For instance:
- Uptime Kuma upon port 3001
- Grafana upon port 3000
Employ a distinct name for this service beneath services, at the same indent level as web. Do read the official Docker example thereof to ascertain the requisite image, port, and storage. Choose a host port that is unoccupied, and do limit access unto the practice environment alone. A tag of latest is no fixed version.
Firstly, run docker compose config --quiet. Thereafter, commence with docker compose up -d and observe that both services run in concert. Shouldst thou use feedback binding akin to Nginx, test the port from within the VM; access from another machine requireth its own determination of address, port, and network.
Display docker compose ps along with the expected response from each service on the very address and port thou hast configured. Explain by word or writing what Compose doeth create of itself, and what remaineth for thee to attend thereto, such as data, settings, and backups. If one service ceaseth operation, peruse docker compose logs ere thou alterest thy configuration. Conclude any log-following with Ctrl+C.
Summe of Matters
- Docker Compose granteth thee power to define sundry services within a single scroll
- The Compose-scroll is both configuration and recordation
- Renewal is wrought by
docker compose pullanddocker compose up -d - Docker maketh it simple to transport thy betwixt servers and other machines
Sources: Docker Compose: first venture and Compos’d services.