Building and pushing an application

We can also use the docker-compose build command to just build the images of an application defined in the underlying compose file. But to make this work, we'll have to add the build information to the docker-compose file. In the folder, we have a file, docker-compose.dev.yml, which has those instructions already added:

version: "3.5"
services:
web:
build: web
image: fundamentalsofdocker/ch08-web:1.0
ports:
- 3000:3000
db:
build: database
image: fundamentalsofdocker/ch08-db:1.0
volumes:
- pets-data:/var/lib/postgresql/data

volumes:
pets-data:

Please note the build key for each service. The value of that key indicates the context or folder where Docker is expecting to find the Dockerfile to build the corresponding image.

Let's use that file now:

$ docker-compose -f docker-compose.dev.yml build

The -f parameter will tell the Docker Compose application which compose file to use.

To push all images to Docker Hub, we can use docker-compose push. We need to be logged in to Docker Hub so that this succeeds, otherwise we get an authentication error while pushing. Thus, in my case, I do the following:

$ docker login -u fundamentalsofdocker -p <password>

Assuming the login succeeds, I can then push the following code:

$ docker-compose -f docker-compose.dev.yml push

The preceding command pushes the two images to the account fundamentalsofdocker on Docker Hub. You can find these two images at the URL: https://hub.docker.com/u/fundamentalsofdocker/.