What is a docker – compose

Docker-compose, as its name suggests, is a command line tool that aggregates your docker container services together. For example, if you have a MySQL instance and a Node.js instance deployed in two containers, docker-compose can do all the steps for you from packaging to running with just one docker-comemage. yml file. And if you’re still struggling to execute the various Docker run [] commands manually, this guide might help.

The installation

If you are using Docker-compose in a native development environment, you do not need to install any dependencies. Docker for Windows and Docker for Mac already have docker-compose packages built in

Install on a Linux based server:

  • Running in terminalSudo curl -l https://github.com/docker/compose/releases/download/1.16.1/docker-compose- ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-composeDownload the docker-compose installation script
  • Add execute permission to the scriptsudo chmod +x /usr/local/bin/docker-compose
  • Execute the scriptsh /usr/local/bin/docker-compose

The container arrangement

Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose:

You can clone and run the examples below on Github

services:
  mysql:
    image: mysql:latest
    ports:
      - '3306:3306'
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: '123456'

  webapp:
    build: .
    container_name: webapp
    restart: always
    depends_on:
      - 'mysql'
    environment:
      NODE_ENV: 'production'
    ports:
      - '80:8080'
Copy the code

It is normal to be confused. Let’s break down each configuration item one by one

  • Services represents all the services contained in the collection. In this case, we have mysql and WebApp

  • Image means to build our container from the officially maintained mysql image

  • Port means to map port 3306 of docker host to port 3306 of Docker container, so that we can connect to our mysql database through localhost:3306 on the host

  • webapp

    • build: indicates the folder from which docker builds the image
    • container_name: Container name
    • restart: indicates that any non-zero status returned by CMD in Dockerfile will restart the container
    • depends_on: Container dependencies. In this case, the WebApp container is guaranteed to start after the mysql container.
    • environment: The Unix environment variable to be injected for use by the container.

Note: All services under services share the same Docker network, which means you can access mysql services from the WebApp container through mysql:3306. Access our Express server from the mysql container via WebApp :8080. See Networking in Compose for a more detailed network configuration

Depends_on only guarantees the order of execution of the container and does not guarantee that your application is already in the available state (whatever the available state means for your application). Docker was designed with careful consideration: to keep your application robust, you should define your own retry/restart mechanism in case of a service connection error between containers.

  1. Next, you just need to run it in the project directorydocker-compose upYou can see docker start building the image and starting the service
  2. Use it if you want to start in background daemon modedocker-compose up -d
  3. withdocker-compose downTo stop the running container
  4. docker-compose buildRebuild the container

After a successful run, visit http://localhost to see the Hello World example of the Express server and http://localhost/mysql to check the mysql connection status

Other examples from Docker:

  • WordPress
  • Rails
  • Django

For complete configuration items, go to docker official website

The original link