Video Lesson: Build, release, and run containers with Docker Compose

Build, release, and run containers with Docker Compose

Learn how to build, configure, release, and run versioned applications with Docker Compose.


Lesson Content

Once you have a Dockerfile, build an executable image of your application with docker build. The build installs application dependencies and compiles binaries and assets into an image that you can later run as a tagged release.

Use the repository name, followed by a tag or version number, with the -t option. A repository name commonly uses the vendor name followed by the application name, although Docker accepts any value. Build from the current directory with .:

docker build -t foo/bar:1.0 .

If you omit the tag, Docker uses latest, but a production release should use a specific version so that each deployment is uniquely identifiable. The build may take a few moments depending on the base image size, dependency installation time, asset size, and number of files copied into the image.

After building the image, create a docker-compose.yml file to describe the release configuration. A simple configuration for a Node.js application looks like this:

version: '3'
services:
  app:
    image: foo/bar:1.0
    env_file:
      - .env
    ports:
      - 8080:8080

The important part is the image property. It references the exact tag you built, so the Compose configuration and image together define the release. You can also configure other release details here, including environment variables, ports, volumes, and dependent services.

Before committing the release, make sure both .dockerignore and .gitignore exclude .env and node_modules. The environment file often contains credentials for third-party services and should never be committed to version control.

Add the release files to Git and commit them:

git add .gitignore .dockerignore *
git commit -m "Initial release"

Tag the commit with an incrementing release version:

git tag v1.0.0

When you deploy the release to production, check out the tag first:

git checkout v1.0.0

Create or update .env on the production machine with the values required by the application. Keep this file untracked because it commonly contains security credentials.

Start the production release in detached mode:

docker-compose up -d app

Compose downloads any images that are not already available locally, applies the configuration, and starts the containers in the background.

Confirm that the container is running with docker ps:

docker ps

Follow the application logs with docker logs -f and the name of the running container:

docker logs -f foo_app_1

Open http://localhost:8080 in a browser to access the application. You should see Herman, the application’s cat.

You can further automate the build, checkout, and deployment process with custom Bash scripts. This removes repetitive commands from the release process while keeping each deployment tied to a specific image and Git tag.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!