Followers

Docker Compose

 Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.

  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

  3. Run docker-compose up and Compose starts and runs your entire app.


Install Docker compose file


  • DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
  • mkdir -p $DOCKER_CONFIG/cli-plugins
  • curl -SL https://github.com/docker/compose/releases/download/v2.19.0/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
  • chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
  • docker compose version

Example

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}

docker-compose up -d
docker-compose down
Example -2
version: '3.3'

services:
   db:
     image: ramansharma95/mysql
   webapp:
     image: ramansharma95/webapp
     ports:
       - "84:80"


  Run the following command in mysql container (docker exec -it <<containerid>> bash)
     mysql -uroot -pwhizlabs
create database company;
use company;
create table employee ( name varchar(30), mobile varchar(30));
select * from employee;


Docker Compose Python Example
Docker Compose multi Docker file Example
Docker Compose with Custom Network
Docker Compose with Predefined network
Docker Compose Examples

COMMENTS

BLOGGER: 6
  1. Hi sir, i got error "Connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known".
    I am using ramansharma95/mysql && ramansharma95/webapp in docker swarm.
    web service in master node & db in work1 node both instance are getting ping to each other.
    i use same image to docker compose it's work but not in docker swarm.
    Kindly suggest me the same ...


    Thank you,
    Akshay Sawant.

    ReplyDelete
  2. docker swarm you need to use the concept of service
    Please follow how to create 2 services one is web and other is db and create a overlay network and run these services on that network and try

    ReplyDelete
  3. Hi Raman sir
    How are you?
    Gadilinga here

    1)Can we include a docker file in compose file, please provide me the example if you have.
    2)In postgresql compose how to execute scripts.

    ReplyDelete
    Replies
    1. Check this https://docs.docker.com/compose/gettingstarted/

      Delete
  4. Hi Raman Sir,
    I followed the steps of example 1. And Tried accessing below URL: http://65.1.110.150:8000
    but getting the below error:
    "Gateway Timeout
    Server error - server 65.1.110.150 is unreachable at this moment.

    Please retry the request or contact your administrator."

    ReplyDelete

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Docker Compose
Docker Compose
DevOpsWorld
https://www.devopsworld.co.in/p/docker-compose.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/docker-compose.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content