Based on a YAML file (that works as a manifet file), the Docker Compose tool manages Docker containers.
First of all, we have to create a docker-compose.yaml
file. This file is going to specify which services we want to run through Docker. It'll work as our manifesto.
version: {docker-compose-version}
services:
{service-name}:
image: {docker-image}
container_name: {container-name}
networks:
- {network-name}
ports:
- "{host-port}:{container-port}"
{service-name}:
image: {custom-image-name}
build:
context: ./myimage
dockerfile: Dockerfile
container_name: {container-name}
networks:
- {network-name}
ports:
- "{host-port}:{container-port}"
{service-name}:
image: {custom-image-name}
command: {custom-command}
container_name: {container-name}
restart: {restart-value}
tty: {true | false}
volumes:
- {host-directory}:{container-directory}
environment:
- {var_name}={var_value}
networks:
- {network-name}
ports:
- "{host-port}:{container-port}"
networks:
{network-name}:
driver: {network-type}
version
: The Docker Compose version that we're going to use (At this moment, it would be "3”
- with quotes)services
: A list of services which we want to run within our Docker Compose file
{service-name}
: For each service we want to run in our Docker Compose environment, we must define a name (which is going to be this variable). Since it's a property of our YAML file, it doesn't need quotes around
image
: The name of the Docker Image that is going to be run in this service. If the property build
is provided, then this property will be used only as the image name and become optionalbuild
: Provides a Dockerfile to generate the image of this container
context
: The directory where our Dockerfile is stored (without quotes)dockerfile
: The name of our Dockerfile file (without quotes). If it is the default name (only Dockerfile
), we don't need to provide this property then.container_name
: A name for the container that will go up when running our Docker Compose filenetworks
: A list of the networks attached to our container
{network-name}
: The name of the network that we want to attach to our container, without quotesports
: A list of the ports that we want to expose in our container
{host-port}
and {container-port}
, separated by a comma (:
) and with quotes. For example: "8080:80"
command
: A command to be ran after the entrypoint of our service/container/image
{custom-command}
: Any command to be executed (without quotes)restart
: A property defining the restart policy of our container
{restart_value}
: could be: always
| ...tty
: If it should enable interactiveness, allowing us to access the container through our host (boolean value)volumes
: A list of volumes that must be attached to our container
{host-directory}
and {container-directory}
, separated by a comma (:
) and without quotes. For example: ./mysql:/var/lib/mysql
environment
: A list with the environment variables that we want to provide to our container
{var_name}
and the {var_value}
, separated by an equal sign (=
) and without quotes. For example: MYSQL_ROOT_PASSWORD=123
depends_on
: Specifies a service that must be ran before this services starts
{service-name}
: the name of the service which our service must waitnetworks
: A list of the networks that must be created in our Docker Compose environment (and that are going to be used in our services)
{network-name}
: The name of the network that we want to. create
driver
: The name of the type of driver which we want to use in our network
After preparing our Docker Compose file, we can run everything through the command: docker-compose up
docker-compose up
: Runs all services/containersup
command is going to run all services/containers defined in our docker-compose.yaml
file in the opene d directory<aside>
💡 Example: docker-compose up {commands}
</aside>
{commands}
:
-d
: Detached. Optional command for dettaching our container execution from our terminal--build
: Optional command for re-building our services images, comparing with the old/cached onedocker-compose down
: Closes all services/containersdown
command is going to stop and kill all services/containers defined in our docker.compose.yaml
file in the opened directorydocker-compose ps
: Lists all services/containers of our Docker Compose environment