Docker/Compose

From Wikiversity
Jump to navigation Jump to search

Docker Compose is a tool for defining and running multi-container Docker applications. It uses YAML files to configure the application's services and performs the creation and start-up process of all the containers with a single command. The docker-compose CLI utility allows users to run commands on multiple containers at once, for example, building images, scaling containers, running containers that were stopped, and more. The docker-compose.yml file is used to define an application's services and includes various configuration options.

docker-compose.yml[edit | edit source]

# Creates a three-container application based on Express, MongoDB, and Redis.
#
# References:
#   https://en.wikiversity.org/wiki/Docker/Express
#   https://en.wikiversity.org/wiki/Docker/MongoDB
#   https://en.wikiversity.org/wiki/Docker/Redis
#   https://en.wikiversity.org/wiki/Server-Side_Scripting

version: "2"
services:
  express-server:
    container_name: express-server
    restart: always
    build: .
    ports:
      - "3000:3000"
  mongo-server:
    container_name: mongo-server
    image: mongo
    ports:
      - "27017:27017"
  redis-server:
    container_name: redis-server
    image: redis
    ports:
      - "6379:6379"

Try It[edit | edit source]

Online Free[edit | edit source]

  1. Use Play with Docker. Create an account and/or log in.
  2. Start an interactive session and add a new instance.
  3. In the terminal window, enter the following commands:
    • touch docker-compose.yml
    • touch Dockerfile
    • touch app.js
  4. Use the Editor button to edit the files and save the contents into each respective file. Use the docker-compose.yml file above and the Dockerfile and app.js files from Docker/Express.
  5. Run the following commands:
    • docker-compose up
  6. In the top window, select the Open Port button and enter 3000 to connect to the running server.

On Your Own System[edit | edit source]

  1. Install Docker Desktop or the Docker Engine.
  2. Save the file above and from Docker/Express into a new Docker Express folder:
    • docker-compose.yml
    • Dockerfile
    • app.js
  3. At a command prompt, change to the Docker Express folder and then run the following command:
    • docker-compose up
  4. Open a web browser to connect to the running server:

See Also[edit | edit source]