How does docker-compose up work?

I’m confused about docker-compose up

Before we activate the remote machine using eval $(docker-machine env vidly), does docker-compose-up build the images locally and start the containers locally on our machine?

After we activate the remote machine, Mosh says that the command is send to the remote machine ,does docker-compose -f up -d still build the images locally but send images to remote machine and start containers?

If our command is send to the remote machine and execute, how docker-compose up could build the images locally since there is no files?

On a second thought, I think my question is more like where the image was built.

docker-compose up can be used to build images, but oftentimes it is used to start containers from already-built images, for example images built with docker build and hosted on Docker Hub.

If you take a look at your Compose yml file you might be able to tell if it’s building the image or not. If you see a build section, then it might be. If you see only image sections without any build sections, then it’s probably using prebuilt images.

1 Like

Thanks for explaining. I think I should be more explicit on my question.

In the lesson Deploying Applications > 9- Deploying the Application, we connect to the remote machine and run docker images for the first time, at the moment and from then on the command is executed on the remote machine and there was no images, at the end of the lesson we executed docker-compose -f up -d --build, then docker images will show the images on the remote machine.

So my question is how the images were created? Are they created on the local machine on our side and sent to the remote machine or they were built directly on the remote machine?

I think the images were built locally on our side because the files are only available here, but how do we build the images when the command is executed on the remote machine? I’m just guessing there is some magic behind the scene so the images were still built locally but sent to the remote machine. Or maybe it’s me simply missed some concept and misunderstood something?

I’m not 100% on this, but I think that the image is built on the remote machine, using files on the local machine as the build context. So for example in the Compose file, if we have this…

    build: ./frontend

Then the ./frontend directory is used as the build context i.e. the context from which files will be copied. This is a directory on the local machine, because we are running the docker-compose command on the local machine. However, because the remote machine is set as the DOCKER_HOST, via the eval $(docker-machine env vidly) command that you mentioned, the docker commands are executed by the remote machine’s docker engine.

So if the Dockerfile has the following instructions…

COPY package.json ./
RUN npm install

These instructions are run on the remote machine. First, the package.json file is copied from frontend directory on the local machine (the build context) to the destination directory on the remote machine, and then npm install is run on the remote machine.

1 Like