What is the difference between Docker-Compose and Dockerfile?

Asked

Viewed 2,220 times

10

What is the difference between Docker-Compose and Dockerfile, specifically?

  1. Docker-Compose uses Dockerfile or vice versa?

  2. Can I make every configuration with just one of them? If so, which?

2 answers

6

The purpose of 2 is quite different. dockerfile describes a container IMAGE that can be used later, including in a Docker-Compose.

Docker-Compose can be compared to container orchestrators. It allows you to create a number of services and settings.

Therefore, one does not replace the other. The two are complementary and with distinct objectives.

An example of Dockerfile would be

FROM ubuntu:18.04
COPY . /app
RUN make /app
CMD python /app/app.py

In this case, a base image (Ubuntu) is used, files are copied, the make command is executed and an initial command is set for the FUTURE container. I say future, because the command may be executed when the image generated by this Dockerfile is used to create a container.

An example of Docker-Compose would be

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

In this case 2 services are created: an application (web) and a database (redis). Note that the image was set for one of them and the other not. This is because in the case of the "web" service there is not yet an image created, the Dockerfile will be built present in the same directory where the "Docker-Compose" command is executed.

1

Docker-Compose uses Dockerfile or vice versa?

Dockerfile: describes the steps p/ create an image based on your definition of what the container should run.

Docker Compose: instead of you having to call docker run comando_1 comando_2 comando_3 definir/montar_volume nome_do_container etc , you define a file docker-compose.yml with these settings and calls the Docker Compose behind the command docker-compose up

Can I make every configuration with just one of them? If so, which?

It’s up to you. When it’s simple, I only use Dockerfile, if it’s more complex and depends on more Docker Compose containers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.