Docker Compose II

Docker Compose II

I. Define the Project

  • Create a directory for your project, in this directory, we will create project-specific docker-compose.yml, Dockerfiles, and setup files.

II. Create a docker-compose.yml file

  • Create a?docker-compose.yml?file within a newly-created directory. You can use either a .yml or .yaml extension for this file. Create YAML file with consistent formatting.

III. Structure of docker-compose.yml

version: '3'
services:
  web:
    # /path/to/dockerfile/
    build:?.

    volumes:
??????-?"/your_code/:/project_code"

    links:
??????-?"db:backenddb"

  db:
    image: mysql

    ports:
      - "3306:3306"

    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password

    volumes:
??????-?"/db/init.sql:/init.sql"        
    


??        

  • version: version defines docker-compose version. There are several versions of the Compose file format – 1, 2, 2.x, and 3.x. Check for more details Compose and Docker compatibility matrix.
  • services: services define docker container configuration. In the above example, we have added two services, `web and db`.
  • web: Docker compose will create docker container. web is the name of our service.
  • build: build refers to a path to our dockerfile. `.` represent a directory where docker-compose file is loacted. If we are providing our customize dockerfile we can add build in our docker-compose.yml file. For pre-built docker images `image` is an alternative for this.
  • volumes: For mounting local disk to docker we can use volumes. In above example, we are mounting your_code directory to container's /project_code directory.
  • links: To link one service to another service, we can use links option.
  • images: If we have pre-built docker images for starting our service, we can add image option and provide docker image name with image loaction.
  • ports: To map the container’s ports to the host, we can use ports option.
  • environment: Setting up an environment variable in the container. In the above example, we have added MYSQL_ROOT_PASSWORD, MYSQL_PASSWORD and MYSQL_USER environment variable

IV. Build and run your app with Compose

Run the below commands from your project directory. You can start up your application using `docker-compose up` commands. If our compose file has a different name than the default one (docker-compose.yml), we can add -f to specify an alternate filename.


docker-compose up        < Build and run your application >

docker-compose up -d     < Build and run your application in detached mode >

docker-compose -f compose-file.yml up  < For custom file name >
        

V. Docker Compose commands

Refer Docker Compose articles for different docker compose commands.

For more sample docker compose application check official: https://docs.docker.com/samples/#sample-applications

PS: Please don't copy-paste this article. If you find it useful please feel free to share it, and yes don't forget to give credits.

要查看或添加评论,请登录

Snehal Rajmane的更多文章

  • Docker Compose I

    Docker Compose I

    Docker Compose is a tool for running multi-container applications. All containers work together.

  • Advanced Dockerfiles: Multistage Build

    Advanced Dockerfiles: Multistage Build

    Docker images usually built with dockerfile with instructions, dependencies, and build steps. Dockerfile instructions…

    2 条评论
  • Dockerfile

    Dockerfile

    Overview: Dockerfile is a document with a set of commands that you need to build docker image. The creation of Docker…

    4 条评论
  • Part II: Docker Commands

    Part II: Docker Commands

    1. Copy files/folders: Yes, we can copy files/folders from local machine to container and vice versa.

    3 条评论
  • Introduction to Docker

    Introduction to Docker

    What is Docker? Docker is a container orchestration tool, which is used for developing, shipping, and running…

    6 条评论

社区洞察

其他会员也浏览了