Let’s say I have a docker-compose.yml like further below.
When I fire up docker-compose up it works.
I am wondering of what use the Dockerfile could possibly be.
The reason why I am asking is this section of the documenation:
https://docs.docker.com/compose/
Using Compose is basically a three-step process:
- 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 can be run together in an isolated environment.- Run docker-compose up and Compose starts and runs your entire app.
I my case I haven’t created a Dockerfile, but still I can reproduce that container everywhere.
So, what is the purpose of the Dockerfile in conjunction or as opposed to docker-compose.yml?
And then I’d also like to ask this intertwined follow up question?
If there is useful application of a Dockerfile despite having already provide a docker-compose.yml, Would I have to use two Dockerfiles since I have two applications in one container?
I am confused.
version: "3.8"
services:
mynodeapp:
image: node:15.8.0
container_name: myappname
working_dir: /app
volumes:
- .:/app
- ~/.ssh:/root/.ssh
expose:
- 3000
restart: always
ports:
- "3000:3000"
networks:
- backend
environment:
HOST: 0.0.0.0
NODE_ENV: $NODE_ENV
redis:
container_name: myredis
networks:
- backend
image: redis
ports:
- "6379:6379"
volumes:
- $PWD/redis-data:/var/lib/redis
- $PWD/redis.conf:/usr/local/etc/redis/redis.conf
networks:
backend:
driver: ${DOCKER_NETWORK_DRIVER}
2
Answers
In your example, you are using already built containers (
node:15.8.0
/redis
), that’s whydocker-compose
doesn’t need anyDockerfile
.The Dockerfile is needed when you want to build your own image.
In that case,
docker-compose
will need instructions on how to build your image.So just to summarize – if you want to use some existing container "as is" – you don’t need
Docekrfile
.docker-compose
will pull the image from docker registry and it’ll be ready to use without any modifications. On the other hand, if you want to build your own container (e.g. building an application), you’ll needDockerfile
.You can do more complex setup in a dockerfile. In your case there exists an image and you have no modifications to make to the image. So don’t provide one.
Just so you know, common practice is to
COPY
the app files in production as opposed to mapping a volume from local machine. This would be done in a dockerfile. For each service, you can specify thebuild
property indocker-compose.yaml
with the location of the dockerfile should you wish to. Usually that is unique to the service/container.Also, slight terminology point of order: you have more than one container. Each service will spin up a separate container.
docker ps
will show you this. You have one compose file that manages several containers.