Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Dockerfile vs docker-compose.yml

I have a Dockerfile as the following:

FROM docker/whalesay:latest
RUN apt-get update && apt-get install -y fortunes
CMD while true; do /usr/games/fortune -a | cowsay; sleep 2; done

I have built the above Dockerfile as image: docker-whale

I want to write a docker-compose.yml for the above image. My understanding is that you can run multiple containers with docker-compose.

So if i want to run 5 images of docker-whale, how does docker-compose.yml looks like?

like image 809
ealeon Avatar asked Jul 21 '16 14:07

ealeon


2 Answers

You could put this docker-compose.yaml next to your Dockerfile:

version: '2'
services:
  docker-whale:
    image: docker-whale
    build: .

And then execute the following commands:

# build docker image
docker-compose build

# bring up one docker container
docker-compose up -d

# scale up to three containers
docker-compose scale docker-whale=3

# overview about these containers
docker-compose ps

# view combined logs of all containers
# use <ctrl-c> to stop viewing
docker-compose logs --follow

# take down all containers
docker-compose down
like image 149
webwurst Avatar answered Sep 25 '22 12:09

webwurst


version:"3" 
services: docker-whale: 
   image:docker-whale 
   deploy: 
     replicas:5
     resources:
      limits: 
        cpus:"0.1"
        memory: 50M
     restart_policy: 
        condition: on-failure
  ports: "80:80" 

...

Above is how your docker-compose.yml should look like. This docker-compose.yml tells Dockers to do the following:

  1. pull the image docker-wale that you build in previous step.
  2. Run five instances of that image as a service called docker-whale, limiting each one to use, at most,10% of the CPU (across all cores), and 50MB of RAM.
  3. Immediately restart containers if one fails
  4. Map port 80 on the host to docker-whale's port 80...

Ref:https://docs.docker.com/get-started/part3/#docker-composeyml

Hope it helps

like image 27
Hung Vu Avatar answered Sep 25 '22 12:09

Hung Vu