Docker: Difference between revisions

From Resonite Wiki
add documentation link to bigger compose file sample
better example compose file
 
Line 14: Line 14:
== Compose ==
== Compose ==


Compose is a tool provided by Docker that makes it trivial to deploy software following a defined configuration file written in YAML. A compose file is usually named <code>docker-compose.yml</code> and a very basic sample one looks like this:
Compose is a tool provided by Docker that makes it trivial to deploy software following a defined configuration file written in YAML. A compose file is usually named <code>docker-compose.yml</code> and a sample one looks like this (the text after # characters is comments and it's ignored by Compose):


<syntaxhighlight lang="yaml">
<syntaxhighlight lang="yaml">


version: '3.8'
version: '3.8' # not actually needed, but defined in the spec for backward compatibility


services:
services: # you can define one or multiple services here
   hello-world:
   whoami: # name of the service
     image: hello-world
    image: traefik/whoami # the Docker image to run, the example image is a small webserver that returns some system info
    restart: always # restart policy, can be always, on-failure, unless-stopped or "no"; you usually want always or unless-stopped
    volumes: # works like the -v flag for Docker; volumes aren't needed for the whoami image, they're in the example for illustrative purposes
      - /path/local:/path/inside # mounts /path/local on the host to /path/inside in the container; creates the directory on the host if it doesn't exist
      - /also/local:/also/inside:ro # mounts /also/local on the host to /also/inside in the container with read only permissions
     ports: # works like the -p flag for Docker
      - 8080:80 # exposes port 80 in the container to port 8080 on the host


</syntaxhighlight>
</syntaxhighlight>


Refer to the [https://docs.docker.com/compose/compose-application-model/#illustrative-example documentation] for a more complete example.
Refer to the [https://docs.docker.com/compose/compose-application-model/#illustrative-example documentation] for more examples.


== Compose Commands ==
== Compose Commands ==

Latest revision as of 17:42, 26 March 2024

Here's some basic Docker information. That might help.

Basic commands

  • docker pull <image> - Downloads the image locally
  • docker run <parameters> <image> - Creates and starts a container with the specified image (doing CTRL+C or closing the prompt will stop it by default), some common parameters are:
    • -p <sourcePort>:<desinationPort> - Opens a port from within the container to your machine (specify 127.0.0.1: before the port specification to only open it locally)
    • -d - Detaches from the image, AKA runs it in the background
    • -e SOMETHING=value - Sets an environment variable, in this case sets the env variable SOMETHING to the value value.
    • -v /path/local:/path/inside - Binds a local path into the container, usually used to pass files or persist data such as databases
  • docker container list --all - Lists all created containers
  • docker container <stop/start/rm> <container name/ID> - Will stop, start or remove the specified container

Compose

Compose is a tool provided by Docker that makes it trivial to deploy software following a defined configuration file written in YAML. A compose file is usually named docker-compose.yml and a sample one looks like this (the text after # characters is comments and it's ignored by Compose):

version: '3.8' # not actually needed, but defined in the spec for backward compatibility

services: # you can define one or multiple services here
  whoami: # name of the service
    image: traefik/whoami # the Docker image to run, the example image is a small webserver that returns some system info
    restart: always # restart policy, can be always, on-failure, unless-stopped or "no"; you usually want always or unless-stopped
    volumes: # works like the -v flag for Docker; volumes aren't needed for the whoami image, they're in the example for illustrative purposes
      - /path/local:/path/inside # mounts /path/local on the host to /path/inside in the container; creates the directory on the host if it doesn't exist
      - /also/local:/also/inside:ro # mounts /also/local on the host to /also/inside in the container with read only permissions
    ports: # works like the -p flag for Docker
      - 8080:80 # exposes port 80 in the container to port 8080 on the host

Refer to the documentation for more examples.

Compose Commands

  • docker compose up <service(s) (optional)> - Will start all the services or the ones specified, from the configuration file (doing CTRL+C or closing the prompt will close the service)
    • To run all the services in the background, you will need to append the -d argument, for instance: docker compose up -d
  • docker compose logs <service(s) (optional)> - Will display the logs of all the services or specified ones
    • You can also see the logs in real time by adding the argument -f, for instance docker compose logs -f (you will need to do CTRL+C to break out of this)
  • docker compose stop <service> - Will stop the container created for the service, for instance docker compose stop hello-world
  • docker compose down - Will stop all the services specified in the configuration and destroy their containers
  • docker compose pull - Will check if any service has a newer image available and if so, will download it
    • After running this, you will still need to do docker compose up so containers are re-created with the latest version downloaded

See Also