4

I have a project that is running in local development using docker-compose

As part of the organisation requirements for gitlab and deployment, the application needs to be built into a single Docker image.

I understand this can be done by adding a Dockerfile to the project.

However, I'm wondering if any advice or suggestions on how to include a project running multiple services via docker-compose with the application codebase into a single image with Dockerfile for deployment, thanks.

EDIT: From what I can gather, the approach would be to build a new image , with Dockerfile, from the application codebase, then include that in the docker-compose.yml file for the deployment environment?

EDIT 2: Apologies for the confusion. I'm new at Docker and there's a bit of learning curve! In this case it seems one can build the application image in the gitlab registry and then include that image in the docker-compose for deployment, will try it.

David Thomas
  • 4,027
  • 3
  • 28
  • 22
  • Without seeing your `docker-compose.yml` file it's going to be very difficult to answer this question. There are some things that simply don't translate. Moving from docker-compose to a single Dockerfile will probably increase the complexity of your project. – larsks Dec 13 '17 at 00:07
  • @larsks thanks for your response. I've added more details on the `docker-compose.yml` file. Please check if that helps on how to convert it to a Dockerfile for the project – David Thomas Dec 13 '17 at 00:13

1 Answers1

9

TL;DR

A single Dockerfile is usually not enough to replace a whole containers orchestration made with docker-compose and is not necessarly a good choice.

About converting a docker-compose.yml file to a Dockerfile :

You can pass some informations from your docker-compose.yml file to your Dockefile (the command to run for instance) but that wouldn't be equivalent and you can't do that with all the docker-compose.yml file content.

You can replace your docker-compose.yml file with commands lines though (as docker-compose is precisely to replace it).


BUT

Keep in mind that Dockerfiles and docker-compose serve two whole different purposes.

  • Dockerfile are meant for image building, to define the steps to build your images.

  • docker-compose is a tool to start and orchestrate containers to build your applications (you can add some informations like the build context path or the name for the images you'd need, but not the Dockerfile content itself).

So asking to "convert a docker-compose.yml file into a Dockerfile" isn't really relevant.

That's more about converting a docker-compose.yml file into one (or several) command line(s) to start containers by hand.

The purpose of docker-compose is precisely to get rid of these command lines to make things simpler (it automates it).

Multiple processes in a single container :

From the docker documentation :

It’s ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application

So you can if your entrypoint permits you to launch several processes, or if you use a supervisor, but maybe that's not necessarly the best idea.

EDIT

Since I'm not sure it's clear for you either, here is the difference between a container and an image.

You really should check this out and try to understand this before working with Docker since it's a very necessary thing to know.

vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • Thanks for your response. Yes, I understand docker-compose and the dockerfile serve different purposes. In this case it is an org requirement to flatten the project to a single image for gitlab CD deployment. So, to build the project image from the Dockerfile, I would need to take the commands from the docker-compose file and convert into Dockerfile syntax? – David Thomas Dec 13 '17 at 00:09
  • @DavidThomas No, you didn't get the difference between Dockerfiles and docker-compose.yml files. You should really look it up. The answer I linked is also great to explain the difference between both. The steps you provide in the `Dockerfile` are meant for the images building process. The informations you provide in the `docker-compose.yml` file are meant to tell compose how to start these containers. – vmonteco Dec 13 '17 at 00:19
  • OK thanks, yes I've read the docs and I'm not sure that's any clearer. Perhaps I can find a way to just use the `docker-compose.yml` file only. I'm not sure why the `Dockerfile` is needed when the project is already running with `docker-compose` however the aim is to use `Dockerfile` to _build an image_ of the whole project for deployment – David Thomas Dec 13 '17 at 00:20
  • How do you build your images if you don't have a base image that fits your need nor a Dockerfile to build your image from a base image then? – vmonteco Dec 13 '17 at 00:22
  • Well, that's the question, how can I build the project image from the docker-compose services + application code base? Trying to work out what's required for the Dockerfile there. – David Thomas Dec 13 '17 at 00:23
  • Image building -> Dockerfile. Containers orchestration -> compose. I added a link to a SO answer explaining the difference between container and image. If that's not already the case, you **really** should try to look how those concepts are wholy different or you won't be able to go further with docker without encountering difficulties. – vmonteco Dec 13 '17 at 00:29
  • Thanks, yes I understand "images" are the static components, built from the `Dockerfile` whereas "containers" are more like a running instance of the image? Not any clearer on what's required for the project Dockerfile though (to use the services/containers defined in the docker-compose file) – David Thomas Dec 13 '17 at 00:30
  • @DavidThomas That's it. To make a culinary comparison, the image is the mold, the container is the cake you make from it. You can't really pass building steps for an image to a `docker-compose.yml` file or the orchestration instructions to a `Dockerfile` since that those aren't meant for these purposes. – vmonteco Dec 13 '17 at 00:36
  • ok, thanks @vmonteco for the advice, much appreciated. It sounds like in this case I need to somehow build a new image of the application codebase, with the `Dockerfile` then include that image in the existing docker-compose for the deployment. – David Thomas Dec 13 '17 at 00:38