What is docker compose? Basic knowledge of Docker compose

Tram Ho

I. What is docker compose?

Docker compose is a tool used to define and run multi-containers for the Docker application. With compose you use the YAML file to configure services for your application. Then use the command to create and run from those configs. Using is also quite simple with just three steps:

  • Declare app's environment in Dockerfile.
  • Declare the services needed to run the application in the docker-compose.yml file.
  • Run docker-compose up to start and run the app.

II. Characteristics

Unlike Dockerfile (build images). Docker compose is used to build and run containers. The operation of docker-compose is similar to the command: docker run.

Docker compose allows creating many similar services (containers) with the command:

III. Demo

Web setup:

npm

or yarn

in the demo is React js web

Change the App.js file

App.css

Server part:

main.go

Config in Dockerfile. If you do not know about the commands to dockerfile config. Don't worry, review part 1: here

Proceed to build Dockerfile

Config services need to start and run in the docker-compose.yml file

  • version : indicates the version of docker-compose used.
  • services : set up the services (containers) you want to install and run.
  • image : indicates the image used during the creation of the container.
  • build : used to create containers.
  • ports : set ports to run at the host machine and in the container.
  • restart : automatically starts when the container is turned off.

There are also a number of other config commands:

environment : set environment variables (often used while configuring the db parameters).

depends_on : indicate dependency. That is, services that must be installed and run first, then the service is configured there to run.

volumes : used to mount two directories on the host and container together.

Run command as below:

After the run is complete we see docker-compose has started and run the two services we have configured in the docker-compose.yml file above.

Now is the time to admire the results 😄

Web : Run at: http: // localhost: 3000

Server : Run at: http: // localhost: 8080

Tip : For those of you who use Visual Studio Code like yourself, you can quickly create profiles for Dockerfile and docker-compose.yml with just a few small steps . In Visual Studio Code, you install the Docker extension

After installation, press F1 => type docker: add => select available temp + config port now Visual Studio Code will automatically generate filesDockerfile, docker-compose.yml, docker-compose.debug.yml, and.dockerignore for you.

In addition it also suggests commands for us as well as warnings while we manually configure the docker's files very convenient.

Share the news now

Source : Viblo