Docker is one of the terms that we hear at least once in the course of our lives. Either it’s referring to a person related to the docks or the well-renowned platform for packaging and running an application.
Docker is an open platform that enables us to distinguish applications from infrastructure and is used for standardizing the environments for development. This means having an application that isn’t bound to a specific infrastructure and can be considered portable. These applications are then packaged with their necessary dependencies to execute in a lightweight, Container structure. Lightweight containers enable us to execute multiple containers on a single host system.
Note: To go in-depth on how we use Docker, visit this Answer.
As mentioned, we can see how different applications can be executed on a host system. But what if we need multiple applications that interlink to execute the needed application? This would lead us to start each individual application, which can be considered tedious and inefficient. Docker Compose is a tool specifically developed to resolve the problem at hand. The central concept that enables us to overcome the problem is called Multi Containers.
For example, we need two different containers interlinked for a specific project. This could be solved through multi containers that create a single
When we execute the docker-compose
command, we use different flags to customize the Docker Compose operations and how the service works. These flags are passed through as arguments, which will affect the outcome. We can look at the structure of the ‘docker compose’ command and the most common flags used.
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
-f
: Specify the path to the docker-compose.yml
file (can be used with multiple files).
-p
: Specify an alternate project name (overrides the directory name).
Up
: Create and start containers.
Down
: Stop and remove containers, networks, and volumes.
Start
: Start services.
stop
: Stop services.
restart
: Restart services.
build
: Build images before starting containers.
pull
: Pull service images.
logs
: View output logs from services.
ps
: List running containers.
The --service-ports
flag is used in conjunction with the ‘run’ command. Let’s see what the run command does and how it is implemented.
run
commanddocker compose run [OPTIONS] <SERVICE> [COMMAND]
[options]: We use this argument to pass options and other flags to modify the result of the command.
<service>: We pass the name of the service defined in the Docker Compose configuration.
[command]: We use this argument to pass the command we want the service container to execute.
docker-compose run
: command is a one-time command against a service. We can further break this down to understand that this command executes a specific command within a service container. Moreover, the container is stopped and removed after the command completes its execution, explaining the term ‘one-time command'.
In the run
command, Docker does not create any ports specified in the service configuration. This is done to prevent port collisions with the already-opened ports. However, if you want the ports to be created and mapped to the host system ports, we use the ‘--service-ports’ flag.
A further explanation tells us that this refers to exposing and accessing the ports of a service running in a Docker container on the host system.
docker compose run --service-ports <SERVICE> [COMMAND]
Lets take a small quiz to build a stronger understanding.
Assessment
What is Docker Compose?
A containerization platform
A tool for managing multiple containers
A programming language
A version control system
Docker Compose is a tool to alleviate the problems faced when multiple containers have to be started for one specific service. There are a multitude of flags and other arguments that can modify the behavior of the commands to the user’s liking.
Free Resources