banner



How To Install Docker Compose On Windows

Use Docker Compose

Estimated reading time: eleven minutes

Docker Compose is a tool that was developed to assistance define and share multi-container applications. With Compose, nosotros can create a YAML file to define the services and with a single control, can spin everything up or tear it all down.

The big advantage of using Etch is you can ascertain your application stack in a file, keep information technology at the root of your projection repo (it's now version controlled), and easily enable someone else to contribute to your project. Someone would but need to clone your repo and offset the compose app. In fact, you lot might see quite a few projects on GitHub/GitLab doing exactly this now.

So, how practice nosotros go started?

Install Docker Compose

If you lot installed Docker Desktop/Toolbox for either Windows or Mac, you already accept Docker Compose! Play-with-Docker instances already have Docker Compose installed as well. If you are on a Linux auto, you will demand to install Docker Etch.

Afterward installation, you should exist able to run the following and run into version data.

Create the Compose file

  1. At the root of the app project, create a file named docker-etch.yml.

  2. In the compose file, nosotros'll outset off by defining the schema version. In nearly cases, it's all-time to use the latest supported version. You can look at the Compose file reference for the electric current schema versions and the compatibility matrix.

  3. Adjacent, we'll define the listing of services (or containers) we want to run as part of our application.

And now, nosotros'll commencement migrating a service at a time into the compose file.

Define the app service

To remember, this was the command we were using to ascertain our app container.

                          $              docker run              -dp              3000:3000              \              -w              /app              -five              "              $(              pwd              )              :/app"              \              --network              todo-app              \              -due east              MYSQL_HOST              =mysql              \              -east              MYSQL_USER              =root              \              -e              MYSQL_PASSWORD              =undercover              \              -e              MYSQL_DB              =todos              \              node:12-alpine              \              sh              -c              "yarn install && yarn run dev"                      

If you are using PowerShell then use this control:

                          PS              >              docker run -dp 3000:3000              `              -w /app -v              "              $(              pwd              )              :/app"              `              --network todo-app              `              -e              MYSQL_HOST              =mysql              `              -east              MYSQL_USER              =root              `              -e              MYSQL_PASSWORD              =secret              `              -e              MYSQL_DB              =todos              `              node:12-alpine              `              sh -c              "yarn install && yarn run dev"                      
  1. Get-go, permit's define the service entry and the paradigm for the container. We can pick any name for the service. The proper name will automatically go a network allonym, which will be useful when defining our MySQL service.

                                      version                  :                  "                  3.7"                  services                  :                  app                  :                  image                  :                  node:12-alpine                              
  2. Typically, you lot will come across the command close to the image definition, although there is no requirement on ordering. So, let'due south go ahead and move that into our file.

                                      version                  :                  "                  three.7"                  services                  :                  app                  :                  image                  :                  node:12-alpine                  command                  :                  sh -c "yarn install && yarn run dev"                              
  3. Let'southward migrate the -p 3000:3000 role of the command by defining the ports for the service. We will use the brusk syntax here, simply there is besides a more verbose long syntax bachelor every bit well.

                                      version                  :                  "                  3.7"                  services                  :                  app                  :                  image                  :                  node:12-alpine                  command                  :                  sh -c "yarn install && yarn run dev"                  ports                  :                  -                  3000:3000                              
  4. Next, we'll drift both the working directory (-w /app) and the volume mapping (-five "$(pwd):/app") by using the working_dir and volumes definitions. Volumes also has a short and long syntax.

    One advantage of Docker Compose volume definitions is we can employ relative paths from the current directory.

                                      version                  :                  "                  3.vii"                  services                  :                  app                  :                  image                  :                  node:12-alpine                  command                  :                  sh -c "yarn install && yarn run dev"                  ports                  :                  -                  3000:3000                  working_dir                  :                  /app                  volumes                  :                  -                  ./:/app                              
  5. Finally, we need to migrate the environs variable definitions using the surround cardinal.

                                      version                  :                  "                  three.7"                  services                  :                  app                  :                  image                  :                  node:12-tall                  control                  :                  sh -c "yarn install && yarn run dev"                  ports                  :                  -                  3000:3000                  working_dir                  :                  /app                  volumes                  :                  -                  ./:/app                  environment                  :                  MYSQL_HOST                  :                  mysql                  MYSQL_USER                  :                  root                  MYSQL_PASSWORD                  :                  secret                  MYSQL_DB                  :                  todos                              

Ascertain the MySQL service

Now, it's time to ascertain the MySQL service. The control that nosotros used for that container was the post-obit:

                          $              docker run              -d              \              --network              todo-app              --network-allonym              mysql              \              -five              todo-mysql-data:/var/lib/mysql              \              -e              MYSQL_ROOT_PASSWORD              =clandestine              \              -e              MYSQL_DATABASE              =todos              \              mysql:5.7                      

If you are using PowerShell and then use this command:

                          PS              >              docker run -d              `              --network todo-app --network-alias mysql              `              -v todo-mysql-data:/var/lib/mysql              `              -due east              MYSQL_ROOT_PASSWORD              =secret              `              -e              MYSQL_DATABASE              =todos              `              mysql:5.7                      
  1. We will offset define the new service and name it mysql then it automatically gets the network alias. We'll go ahead and specify the paradigm to use as well.

                                      version                  :                  "                  3.7"                  services                  :                  app                  :                  # The app service definition                  mysql                  :                  prototype                  :                  mysql:v.7                              
  2. Next, nosotros'll define the volume mapping. When we ran the container with docker run, the named volume was created automatically. However, that doesn't happen when running with Etch. Nosotros need to define the volume in the top-level volumes: department and and so specify the mountpoint in the service config. Past just providing only the volume name, the default options are used. There are many more options bachelor though.

                                      version                  :                  "                  iii.7"                  services                  :                  app                  :                  # The app service definition                  mysql                  :                  image                  :                  mysql:5.vii                  volumes                  :                  -                  todo-mysql-data:/var/lib/mysql                  volumes                  :                  todo-mysql-data                  :                              
  3. Finally, nosotros only need to specify the environment variables.

                                      version                  :                  "                  3.7"                  services                  :                  app                  :                  # The app service definition                  mysql                  :                  image                  :                  mysql:five.7                  volumes                  :                  -                  todo-mysql-data:/var/lib/mysql                  environment                  :                  MYSQL_ROOT_PASSWORD                  :                  undercover                  MYSQL_DATABASE                  :                  todos                  volumes                  :                  todo-mysql-data                  :                              

At this signal, our complete docker-compose.yml should look similar this:

                          version              :              "              3.vii"              services              :              app              :              image              :              node:12-alpine              command              :              sh -c "yarn install && yarn run dev"              ports              :              -              3000:3000              working_dir              :              /app              volumes              :              -              ./:/app              environment              :              MYSQL_HOST              :              mysql              MYSQL_USER              :              root              MYSQL_PASSWORD              :              secret              MYSQL_DB              :              todos              mysql              :              image              :              mysql:5.7              volumes              :              -              todo-mysql-data:/var/lib/mysql              environment              :              MYSQL_ROOT_PASSWORD              :              secret              MYSQL_DATABASE              :              todos              volumes              :              todo-mysql-data              :                      

Run the application stack

Now that nosotros accept our docker-compose.yml file, we can start information technology upwardly!

  1. Make sure no other copies of the app/db are running get-go (docker ps and docker rm -f <ids>).

  2. First up the awarding stack using the docker-etch upwards control. We'll add together the -d flag to run everything in the background.

    When we run this, we should see output like this:

                                      Creating network "app_default" with the default driver  Creating volume "app_todo-mysql-data" with default driver  Creating app_app_1   ... done  Creating app_mysql_1 ... done                              

    Y'all'll notice that the volume was created as well as a network! By default, Docker Compose automatically creates a network specifically for the application stack (which is why we didn't define i in the compose file).

  3. Let's look at the logs using the docker-etch logs -f command. Yous'll see the logs from each of the services interleaved into a single stream. This is incredibly useful when y'all want to picket for timing-related problems. The -f flag "follows" the log, so volition give yous live output as it's generated.

    If you lot have run the command already, you'll see output that looks like this:

                                      mysql_1  | 2022-10-03T03:07:sixteen.083639Z 0 [Note] mysqld: gear up for connections.  mysql_1  | Version: '5.7.27'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)  app_1    | Connected to mysql db at host mysql  app_1    | Listening on port 3000                              

    The service proper noun is displayed at the beginning of the line (ofttimes colored) to help distinguish messages. If you want to view the logs for a specific service, you tin can add the service name to the end of the logs control (for instance, docker-compose logs -f app).

    Tip: Waiting for the DB earlier starting the app

    When the app is starting upwards, it really sits and waits for MySQL to be upwards and ready before trying to connect to information technology. Docker doesn't have whatever built-in support to look for another container to be fully upward, running, and ready before starting another container. For Node-based projects, you lot tin utilise the wait-port dependency. Similar projects exist for other languages/frameworks.

  4. At this signal, y'all should be able to open your app and see it running. And hey! Nosotros're downward to a unmarried command!

See the app stack in Docker Dashboard

If we look at the Docker Dashboard, nosotros'll see that there is a group named app. This is the "project name" from Docker Compose and used to grouping the containers together. By default, the project proper name is simply the name of the directory that the docker-compose.yml was located in.

Docker Dashboard with app project

If you lot twirl downwardly the app, you will meet the two containers we divers in the compose file. The names are also a fiddling more descriptive, equally they follow the pattern of <project-name>_<service-name>_<replica-number>. And so, it's very easy to rapidly see what container is our app and which container is the mysql database.

Docker Dashboard with app project expanded

Tear it all downwardly

When you're ready to tear it all downward, only run docker-compose down or hit the trash can on the Docker Dashboard for the entire app. The containers volition stop and the network will be removed.

Alarm

Removing Volumes

By default, named volumes in your compose file are Non removed when running docker-compose down. If you want to remove the volumes, you will need to add the --volumes flag.

The Docker Dashboard does not remove volumes when y'all delete the app stack.

Once torn down, you lot tin can switch to another project, run docker-compose up and be ready to contribute to that project! It really doesn't get much simpler than that!

Recap

In this department, nosotros learned about Docker Compose and how it helps us dramatically simplify the defining and sharing of multi-service applications. We created a Compose file past translating the commands we were using into the appropriate compose format.

At this bespeak, nosotros're starting to wrap upwards the tutorial. Still, at that place are a few all-time practices about paradigm building we want to encompass, as in that location is a big event with the Dockerfile nosotros've been using. So, let'due south take a look!

become started, setup, orientation, quickstart, intro, concepts, containers, docker desktop

Source: https://docs.docker.com/get-started/08_using_compose/

Posted by: bakerpludenis.blogspot.com

0 Response to "How To Install Docker Compose On Windows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel