DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Create Couchbase Docker Images on the Fly With TestContainers

Create Couchbase Docker Images on the Fly With TestContainers

Want to create Docker Images for Couchbase on the fly? Take a look at this tutorial, which uses TestContainers to do just that.

Laurent Doguin user avatar by
Laurent Doguin
·
Jul. 25, 16 · Integration Zone · Tutorial
Like (3)
Save
Tweet
3.68K Views

Join the DZone community and get the full member experience.

Join For Free

Yesterday, I wrote about how to do unit and integration tests with Couchbase and TestContainers. One of the prerequisites for those tests was to have an image already built. Turns out you don't have too. You can create your own images just before running your tests, and it is super easy. Thanks to Sergei Egorov for showing me the way!

Creating Images on the Fly

In the previous example, an image would be instanciated with the following code:

@ClassRule
public static GenericContainer couchbase =
    new GenericContainer("mycouchbase:latest")
        .withExposedPorts(8091, 8092, 8093, 8094, 11207, 11210, 11211, 18091, 18092, 18093)
        .waitingFor(new CouchbaseWaitStrategy());


The GenericContainer constructor would just take a string as parameter — that string being the name of the container you want to test. But the GenericContainer constructor also accepts a future, which happens to be what the ImageFromDockerfile class is. The Docker image will be created asynchronously with the parameters you will give.

Here I have copied all I needed into the resources folder of my project, which makes them all accessible form the classpath. This is why I use the withFileFromClasspath method. You can also get a file from a string, an absolute path, or a file. You'll find more informations about this on TestContainers documentation.

@ClassRule
public static GenericContainer couchbase =
    new GenericContainer(
        new ImageFromDockerfile().withFileFromClasspath("Dockerfile", "Dockerfile")
            .withFileFromClasspath("scripts/dummy.sh","scripts/dummy.sh")
            .withFileFromClasspath("scripts/entrypoint.sh","scripts/entrypoint.sh")
            .withFileFromClasspath("scripts/run","scripts/run")
        )
        .withExposedPorts(8091, 8092, 8093, 8094, 11207, 11210, 11211, 18091, 18092, 18093)
        .waitingFor(new CouchbaseWaitStrategy());


And with that, your Docker image will be built automatically before running your tests. By default, the images are deleted on exit but you can pass a flag to keep the images and avoid rebuilding them all the time. It really depends on your testing strategy.

Troubleshooting

While writing this, I encountered a minor issue. All the resources that were used for the image creation lost their permissions, so I had to add a RUN chmod +x on all the resources I copy in the Dockerfile. This is now a known issue and the lovely and reactive people behind TestContainer are working on this. You can have a chat with them on their Slack channel, just like you can have a chat with Couchbase folks on our community channel.

Docker (software)

Published at DZone with permission of Laurent Doguin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Removing JavaScript: How To Use HTML With Htmx and Reduce the Amount of Code
  • Top 10 Criteria to Select the Best Angular Development Company
  • Creating Microservices in Nest.js
  • Salesforce and Snowflake Native Data Integration Options

Comments

Integration Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo