Reputation: 3153
I have a docker-compose
file that creates two instances, one for webserver
and another for database
.
When I run docker ps
I get this kind of output:
7cb1fb603a98 apache "/usr/bin/dumb-init …" 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:8080->8080/tcp compose_webserver_1
74f3bbe506eb postgres:13 "docker-entrypoint.s…" 6 minutes ago Up 16 minutes (healthy) 5432/tcp compose_postgres_1
If I run docker images
:
REPOSITORY TAG IMAGE ID CREATED SIZE
webserver latest 17dc61fa0139 2 minutes ago 2.05GB
database latest 17dc61fa0139 2 minutes ago 2.05GB
I see that both services share image_id
. In my docker-compose.yml
I have setup:
build:
context: .
dockerfile: Dockerfile-test
To run some ADD
before creating it. Then, I am creating my own image using another image as a base in the FROM
statement.
Once I have my new image with the main content, but also with my added requirement.txt
, and other stuff I push it to ECR
repository.
Then instead of working with my first two docker containers I work with a single container that has been created using these new image in my ECR repository.
It have both services in a single container, despite in my original docker-compose up --build
it raised two containers. Why this is happening?
Is this correct, or should I force with the command statement to create two container instances from the new pulled imaged created by me? For example having always two container instances can allow me to then exchange the postgres container to one mysql for example.
Upvotes: 1
Views: 672
Reputation: 1152
Why docker containers share image id?
Because they can. Images are just "filesystems templates" to be used by containers.
About the image duplication. It is not duplication, it is a single image 17dc61fa0139
with two names webserver:latest
and database:latest
Upvotes: 0
Reputation: 25344
In the answer below, I'll use the terms 'image' and 'container' and it's important to be aware of the difference. The image is what you build and what is stored in the registry. A container is the process that is running when the image is started.
When images have the same id, they're identical and they only take up the space of one copy of the image in your registry. Your docker-compose --build
creates two images because you have 2 services defined in your compose file and it creates an image for each.
As for your setup the common approach is to have each service in it's own image and run them as separate containers. There's nothing wrong with having an image that can do multiple things based on the command you pass it, except that the image is larger than needed. On the other hand, I can't really see any advantages, so the best way is to have a database image and a webserver image that only do what they need.
You can have multiple processes running in a single container, but that's not something you see that often. In your case with a database and a webserver, you might want to scale the number of webservers and still have a single database. You can't do that if they're in the same container.
Upvotes: 1