Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile vs docker-compose.yml

What's the relation between Dockerfile and docker-compose.yml files?

Looks like I can build any Linux setup using Dockerfile using FROM, RUN (apt-get etc. and CMD commands. But it seems, this is not much reusable (I can reuse the whole image, but the services are hardcdoded and not reusable by other projects).

Shall I use both of the files on new projects?

Let's say I want to have regular LAMP stack:

  • Linux OS (debian:wheezy)
  • Apache web server (httpd:2.2)
  • MySQL (mariadb)
  • PHP (php:5.6)

running together as on one, regular machine.

And in a dir on my host system:

  • volume for app source files
  • vhost config file
  • apache logs
  • persistent data in db

I prefer using official, base repos and images, not the pre-configured all-in-ones.

How the config files are supposed to look like in this case?

I'm using docker-compose v.1.8.1 and docker v. 1.12.3 on Ubuntu.

like image 455
Sfisioza Avatar asked Oct 29 '16 19:10

Sfisioza


People also ask

How do I use Docker Compose with dockerfile?

Docker compose uses the Dockerfile if you add the build command to your project’s docker-compose.yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

Which file is responsible for building the dockerfile?

Here the Dockerfile is passed through the compose file, and from the first look, it can seem like it's the compose file that's responsible for building the image, but it's not. For a docker beginner, terms like docker start, docker run and docker create could be confusing.

How do I build a docker image from a dockerfile?

A Dockerfile can be pointed to through the Compose file, and then you can use docker-compose to build the image. Now you can run docker-compose build to build the image. Or you can also run docker-compose up --build to build and run the container at once.

What does docker-compose actually do?

So docker-compose makes it easier for working with multiple containers. The next time you need to start this group of containers in the background, you can do docker-compose up -d; and to stop them, you can do docker-compose down. @Chris It's almost as if there is 3 years worth of knowledge between this and the accepted answer.


2 Answers

Dockerfile:

  • is a recipe for a Docker Image
  • only supports portable options (others options have to be specified at container run time)

docker-compose.yaml:

  • is a recipe for a group of running services
  • supports overriding portable options that were defined in the Dockerfile
  • supports non-portable options
  • supports creating and configuring networks and volumes
  • can also configure the build of an Image by using build:

It is common to use both together.

A Dockerfile is almost always used to create a custom image. Some images might be used to run services (long running processes), but some images might be used to run short-lived interactive processes (like running unit tests).

docker-compose.yaml is useful when you want to run one or more services.

like image 98
dnephin Avatar answered Sep 21 '22 17:09

dnephin


Docker creates isolated machine (container). Each container contains only one process (Apache or Mysql or another); And Dockerfile defines how to build a image.

Docker compose allows run, links and configure the bunch of containers together.

In your case apache needs to know "where" a mysql. And mysql needs to be waked up before you run apache container.

Dockerfile defines how to create app image. App image contains you application and web-browser.

FROM apache:php5.6

ADD /src /var/www/awesome_project #add a project src code
ADD /config/apache/awesome_project.conf /etc/apache2/sites-available/ # add a configuration
# make any things

Then you need to build image docker build my_app:latest .

At this point you have created image, and you need to run app and links it to db

you have 2 ways to do this:

1) Native docker approach. you run db container

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

and after you need to run app container (image was created before)

docker run --name my_app --link some-mysql:mysql -P -d my_app

at this point we have worked application. Bit this simple thing cause us make 2 long command. If you need copy application to another machine you need to repeat this command exactly.

2) docker-compose way allows create a configuration for running the containers. It described how exactly run containers.

Simple docker-compose.yml config illustrate this approach

db:
   image: mysql
   environment:
    - MYSQL_USER=root
    - MYSQL_PASSWORD=root

app:
   image: my_app:latest
   ports:
    - 80:80
   depends_on:
    - db
   environment:
    # Database
    - DB_USER_NAME=root
    - DB_USER_PASSWORD=root

This config allows you run 2 container together, links and configure them.

This is very easy example. and pros of using docker compose not apparent, but if you have 5+ containers it is too hard to run them together without compose.

like image 25
Bukharov Sergey Avatar answered Sep 22 '22 17:09

Bukharov Sergey