2. Develop with Docker Flashcards
What is Docker Compose?
A tool for defining and running multi-container Docker applications, which uses a YAML file to configure the application’s services.
How do you configure Docker Compose?
By using a YAML file name docker-compose.yml
.
What does the Docker Compose config file do?
It allows us to specify what services we want to compose using Docker Compose, as well as their configurations.
How do you define your services using a Docker Compose config file?
By using the services
key.
eg.
services:
myapp …
mongo …
~~~
~~~
How do you define the container name for a service to run using a Docker Compose config file?
By using the container_name
key.
eg.
services:
myapp:
container_name: myapp
…
~~~
~~~
How do you define the configuration options to be applied at build time for a service using a Docker Compose config file?
By using the build
key.
build
can be specified either as a string containing a path to the build context or as an object with the path specified under context and optionally Dockerfile and args.
eg.
services:
myapp:
build: path/to/context
~~~
OR
~~~
services:
myapp:
build:
context: path/to/context
~~~
~~~
What is a restart policy, and how do you define it for a service, using a Docker Compose config file?
It’s an object that configures if and how to restart containers when they exit. It is defined by using the restart_policy
key for service.
eg.
services:
myapp:
restart_policy:
…
~~~
~~~
How do you specify a restart policy condition using a Docker Compose config file?
Inside a service object, either by passing the condition to the restart
key, or specifying using condition
key within a restart_policy
object.
eg.
services:
myapp:
restart: always
~~~
OR
~~~
services:
myapp:
restart_policy:
condition: always
~~~
~~~
How do you expose a port to the host machine for a service using a Docker Compose config file?
Inside a service object, by using the ports
key with either an array of simple single values or objects.
eg.
services:
myapp:
ports:
- “8000:8000”
- …
~~~
OR
~~~
services:
myapp:
ports:
- target: 80
published: 8080
protocol: tcp
mode: host
- …
~~~
~~~
How do you specify a link to another service for a given service using a Docker Compose config file?
Using the links
key inside the service object and specifying an array of service names or of service name and alias combinations each separated by a column.
eg.
services:
myapp:
links:
- mongo
# OR
- mongo:database
~~~
~~~
How do you mount host paths or named volumes for a service?
By using the volumes
key inside the service object and specifying an array of source and destination path combinations each separated by columns.
eg.
services:
myapp:
volumes:
# Specify a path and let the Engine create volume:
- /var/lib/mysql
# Specify an absolute path mapping:
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file:
- ./cache:/tmp/cache
# User-relative path:
- ~/configs:/etc/configs/:to
# Named volume:
- datavolume:/var/lib/mysql
~~~
~~~
How do you specify the ports to be exposed to linked services without publishing them to the host machine for a given system using the Docker Compose config file?
By using the expose
key within the service object and specifying an array of ports to be exposed.
eg. ``` services: myapp: links: expose: - "8000"
~~~
How do you specify the image to start the container from using either a repository/tag or a partial image ID in the docker-compose file?
By using the image
keyword and specifying the repository/tag or the partial image ID.
eg. ``` services: myapp: image: mongo
~~~
How do you build your services specified in the docker-compose file?
By executing the docker-compose build
command inside the folder where the docker-compose file is.
How do you create and start the associated containers for the services you specified in the docker-compose file?
By executing the docker-compose up
command inside the folder where the docker-compose file is.