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.

Docker Compose vs. Dockerfile - which is better?, 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� Remember, docker-compose.yml files are used for defining and running multi-container Docker applications, whereas Dockerfiles are simple text files that contain the commands to assemble an image

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.

Difference between Docker Compose Vs Dockerfile, Docker Compose define the services that make up your app in docker-compose. yml so they can be run together in an isolated environment. It get an app running � Build Time Labels vs Run Time Labels At the Label-Schema.org community so far we’re promoting conventions for the use of build time labels. Build time labels are specified in your Dockerfile as

It's fundamentally about a separation of concerns and a simple way to conceive this is by thinking in terms of what vrs how.

Dockerfile defines what goes on within a container.

Docker-compose.yml defines how that container is run within a host. This is why they say in docs - "services are just containers in production".

The container doesn't care how or where it is run, its just concerned with what it is running. The host doesn't care about what it is running only how it is run .e.g. multiple instances, this much RAM, that much CPU, make sure you start this service before that one, etc.

So we create two separate configuration docs for each of these purposes.

Get started with Docker Compose, Define your app's environment with a Dockerfile so it can be reproduced anywhere. Define the services that make up your app in docker-compose.yml so they� Adding memory parameters to docker-compose.yml. This solution is desired over Dockerfile. In this way, you have a single control point for your memory configuration on all containers that compose your application. Add the JAVA_OPTS into environment section.

Overview of Docker Compose, specified in the Dockerfile, such as CMD , EXPOSE , VOLUME , ENV , are respected by default - you don't need to specify them again in docker-compose. yml . As with docker run, options specified in the Dockerfile, such as CMD, EXPOSE, VOLUME, ENV, are respected by default - you don’t need to specify them again in docker-compose.yml. You can use environment variables in configuration values with a Bash-like ${VARIABLE} syntax - see variable substitution for full details.

Compose file version 3 reference, What is the difference between Dockerfile and docker-compose.yml files? 7,435 views7.4K Duration: 3:17 Posted: Oct 28, 2019 Importante: Todo Dockerfile debe terminar en un comando CMD o en un ENTRYPOINT, pero en este caso, no lo utilizamos, ya que lanzaremos un comando directamente desde la receta de Docker Compose. Es decir, este Dockerfile se utiliza solamente para construir el contenedor y configurarlo. No es autoejecutable. Paso 2: Creamos un docker-compose.yml

What is the difference between Dockerfile and docker-compose.yml , In Node.js packages and Python projects, you have the Dockerfile , .dockerignore , docker-compose*.yml files all in the� 6. The next step is to build a Docker image from the newly made Dockerfile. Since we are still in the MyDockerImage directory, you don’t need to specify the location of the Dockerfile, just build the image by running: sudo docker build . 7. The output will tell you the name of the container.

Comments
  • And what are portable options?
  • Portable options are options that behave the same way on all hosts. For exposing setting an environment variable using ENV. A non portable option would be something like a host bind mount, where the directory needs to exist on the host for it to work.
  • Thanks for this. I've been trying to understand, if I want to run, for example, an nginx container with specific options (port mappings and volumes), do I specify that using a Dockerfile, docker-compose.yml, or something else (like a bash script). If I understand this answer correctly, I can rule out a Dockerfile for such a task.
  • So the Dockerfile is not needed at all? How do I define the whole stack described in my question by just using docker-compose.yml? Is Dockerfile an older way of Docker configuration or a way suitable for creating low level images, like distro clones?
  • you need a Dockerfile. Dockerfile defines how to create certain docker image (at low level). Docker compose defines how to run, link,configure containers (from images) together
  • Could you please provide complete example of both files, regarding the setup from my question?
  • Thanks for the update, but I still don't see where is php and Apache attached. Why FROM apache:php5.6?
  • In this example image my_app contains src code, php and apache, because base image is apache:php5.6