How to use Docker on your Local Machine – For DBAs

Recent posts related to using Docker with Db2:
An Overview of Running Db2 in Containers
Where do Db2 Containers Come From and How are they Used

The focus of this article is to go through a few of the basics of using Docker with Db2. I’m going to focus on using the Db2 Community container, as it’s free and easy.

Installing

I’m not going to walk through how to install Docker, as it’s just like installing any other app for your platform. Docker can be downloaded here: https://www.docker.com/products/docker-desktop. There may be special configuration needed if you require the use of proxies (required in some work environments), but beyond that, once it’s installed, it just works. I work mostly at the command line, because that is where I’m comfortable, so that’s what I’ll be sharing. I use iterm2 on Mac.

Downloading the image

Once you’ve got docker installed on your machine, you should be able to issue basic commands. The image we’re using as an example here is the Db2 Community Edition container. It is free to use up to certain hardware limits, but should not be used for production. It is fairly simple to pull a copy of the image. Simply issue the command docker pull ibmcom/db2. It may take a while, but it will look something like this:

$ docker pull ibmcom/db2
Using default tag: latest
latest: Pulling from ibmcom/db2
93156a512b98: Pull complete
f8c518873786: Pull complete
5d4974261da2: Pull complete
2d3a12d55319: Pull complete
d8d137bd0181: Pull complete
0b0c43213599: Pull complete
650e3bc372c5: Pull complete
8e1c790df7a2: Pull complete
397ac3fddb7e: Pull complete
37df0a98c95f: Pull complete
ebdee5ddf728: Pull complete
a7b63a97ead4: Pull complete
654ebc840f5c: Pull complete
Digest: sha256:54355ddc5d8e5b890141ff863083fb3e37168fed8d66bbc4cdf6b73b704d4389
Status: Downloaded newer image for ibmcom/db2:latest
docker.io/ibmcom/db2:latest
$ docker image list
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ibmcom/db2   latest    a6a5ee354fb1   2 weeks ago   2.95GB

Running the Container

If you go to the link above, there is quite a bit of detail around the Db2 container, options, and ways to customize the container. In it’s simplest form, you can get the container up and running with a command like this:

$ docker run -itd --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=db2rock$ -e DBNAME=testdb -v /Users/ecrooks/db:/database ibmcom/db2
c0691c7705fb46e8893b76a65494822643a80d8e16f4caf031773d507545ace4
$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS         PORTS                                                                               NAMES
c0691c7705fb   ibmcom/db2   "/var/db2_setup/lib/…"   12 seconds ago   Up 7 seconds   22/tcp, 55000/tcp, 60006-60007/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp   mydb2

This shows us that the container is up and running, but a db2 container takes a while to be ready. This is because it is creating the instance and the database. To determine if it is ready or not, you can use:

$ docker logs mydb2 --follow

Look for the string (*) Setup has completed. – this indicates that db2 is ready to use. After it is done with setup, it will output the diagnostic log to this same place. This is standard practice for Docker containers and makes it easier for managing log files in larger implementations.

Entering a Running Container

Ok, so the container is there and running, but how do you use it? There are a couple of ways. If you only want to connect to it from programs or from GUIs like DBeaver, you can now do so (in this case on port 50000). If you want to enter the container and treat it just like sshing into a server, you can use:

$ docker exec -it mydb2 bash -c "su - db2inst1"
Last login: Thu Jul 15 00:23:21 UTC 2021 on pts/0
[db2inst1@c0691c7705fb ~]$

You are now inside the container as the instance owner. Keep in mind that any changes you make are NOT saved to the image. The data is persisted to the location you specified when running the container, but changes that are not at that level will not be persistent. Make sure that if you’re working on scripting, you save the scripts off frequently or save them to a location that is persistent. Test your understanding of persistence before you really need it.
You can exit out of the container simply using exit.

Stopping the Container

When should a container be stopped? Well, when it is up, it is using resources on the host. If you’re following these instructions, you’re probably only using the container for experimentation locally, so there’s no need for the container to be up and running. To stop a container, simply issue:

$ docker stop mydb2
mydb2

Deleting Containers and Images

Having a stopped container doesn’t really take up system resources, except for disk space. If you want to delete a container, use:

$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                                                               NAMES
c0691c7705fb   ibmcom/db2   "/var/db2_setup/lib/…"   14 minutes ago   Up 14 minutes   22/tcp, 55000/tcp, 60006-60007/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp   mydb2
$ docker stop mydb2
mydb2
$ docker rm mydb2
mydb2
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

A container must be stopped before it can be removed. This removes the container, but not the image. Often, you’re likely to use the image again, and there’s no need to delete it. However, if you want to delete an image, first you must stop all containers using it, and then issue:

$ docker image list
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ibmcom/db2   latest    a6a5ee354fb1   2 weeks ago   2.95GB
$ docker image rm ibmcom/db2
Untagged: ibmcom/db2:latest
Untagged: ibmcom/db2@sha256:54355ddc5d8e5b890141ff863083fb3e37168fed8d66bbc4cdf6b73b704d4389
Deleted: sha256:a6a5ee354fb1242a75d508982041cd48883f3fe7c9c9b485be0da6c0ebd44a39
Deleted: sha256:e3e07ecb8c4c516fe093e7852c6ec30bdf444f676478beac297491b0430ee9eb
Deleted: sha256:b2e70f58f0a5dd23dacfdb819dfd5a076f7a96c939263e5cf96308e3c56ae3b5
Deleted: sha256:ff8c7f097580d64005740aa521e0c300981d5c8ce0fb5b16d660d724cb780699
Deleted: sha256:d0ef8c1ba74577f4cb60d6652f70d6cbccfcd8e3143fd684133527fba5d8a440
Deleted: sha256:c62b997486b21e75558cd8380e7cc2bd13f36a293c512d436c5b467e36fa33be
Deleted: sha256:736e1a00f26877afcb601d52856ea621ff0dcebbd5be49ec54a31527151a1bcc
Deleted: sha256:6f5a8e657a58becfe22b591fd483b5239c10cbc7a415bb98bdb9ab03a08eabc2
Deleted: sha256:732f8a7cab28fdc500518dc97c10b1ed9a654a48bec94ca3c4d50a437438ab43
Deleted: sha256:6e5c635c0af7c5d918b4222add1f8f85cbc7301f1f0a4702271fb5ed48e2374d
Deleted: sha256:b338d3ccdc69ff13cba19fb8e4a5ce96c50c7c9bcc5efda99913a72eef07a977
Deleted: sha256:5a8941a031ea234d845063c16f35c968aead796f6232574651ce291bd62c2f10
Deleted: sha256:7d2f2eafd73b78fd3963e4fbd77f42bf17004085765e515019a0c6a8d11f615e
Deleted: sha256:01d2fb86653538b491b461c58ff5c3f5659c808d75efd99ba3ae4f1427db90ab
$ docker image list
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

Summary

Simple utilization of a sandbox image can be powerful for experimentation and learning. If you prefer videos on all of this, I’d recommend the Docker Playlist over at discover.db2.

Ember Crooks
Ember Crooks

Ember is always curious and thrives on change. She has built internationally recognized expertise in IBM Db2, spent a year working with high-volume MySQL, and is now learning Snowflake. Ember shares both posts about her core skill sets and her journey learning Snowflake.

Ember lives in Denver and work from home

Articles: 544

2 Comments

  1. Hi Ember: I’m just giving this a go and working as advertised, which is great. Except where I want to connect to the newly-created DB2 database without using the
    docker exec -it Local_DB2 bash -c “su – db2inst1”
    option. You say you can connect using from programs; does that mean I should be able to catalog it in a local DB2 client and access it? Because I’ve had no luck so far. Let me know if I’m barking up the wrong tree
    Cheers
    Mark

    • Yes, you can both connect from a local client or program, and also from another container. I often run a server container and a client container to test specific out – such as changes to our maintenance container. On Mac, my catalog commands on the client container are:

      $ db2 catalog tcpip node lhost remote host.docker.internal server 50000
      DB20000I  The CATALOG TCPIP NODE command completed successfully.
      DB21056W  Directory changes may not be effective until the directory cache is
      refreshed.
      $ db2 catalog db sample at node lhost
      DB20000I  The CATALOG DATABASE command completed successfully.
      DB21056W  Directory changes may not be effective until the directory cache is
      refreshed.
      $ db2 connect to sample user db2inst1
      Enter current password for db2inst1: 
      
      
         Database Connection Information
      
       Database server        = DB2/LINUXX8664 11.1.4.5
       SQL authorization ID   = DB2INST1
       Local database alias   = SAMPLE
      

      It is also critical to make sure you’ve included the port mapping syntax in the run command – `-p 50000:50000`, and that you select a port you’re not otherwise using on the computer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.