Tuesday, June 7, 2022

Running APEX in Docker container


Hello everyone and welcome back to our blog...


In this blog, we are going to show you how to install Oracle XE in a Docker container, then install ORDS and APEX. It is recommended that you have a basic knowledge of Docker and how it works.

First of all, you need to install docker on your machine, you can easily download and install it from the docker homepage.

Then you will need to sign up for a free account on the oracle container registry, from this registry we will pull all the docker images we will need later.

After you have Docker installed and signed up, you can follow throw with us :).

Starting Docker

After you installed Docker, you need to start docker desktop, to be able to use the docker commands. 

Docker Network

We will configure a docker network, to allow communication between the DB and ORDS container. For creating a network, we simply run the following command in the Terminal or Command line:

1
docker network create demo-network
 
Now, to list the networks you have, run the following command:

1
docker network ls

Docker Volume

When we run our DB in a docker container all data changes are saved in the container, meaning we will lose all the data when the container gets deleted. To store the data independent from the container we can attach a volume. In this case, the data is saved on the host machine and will not be affected if we remove the container. We can later spin up a new container and mount the same volume to it.
To create a volume we run the following command:

1
docker volume create db-demo-volume

To list all the volumes, run the following:

1
docker volume ls
 

Download and Build Oracle XE

After installing docker, we need to download the Oracle XE docker-image. Afterward, we can spin up a container from that image, and our DB instance is ready to use. 

In most cases, you will find the Docker image you need on Docker-Hub. In some cases, the image provider hosts the images on its registry, like in our example, the images are hosted on an Oracle registry.

To pull the Oracle XE image, sign in to the Oracle registry using the account you created. You can do that by running the following command:

1
docker login container-registry.oracle.com

After a successful login, you will see the following message


Then we run the following command to pull the latest Oracle XE image (As of today 21c 21.3.0):

1
docker pull container-registry.oracle.com/database/express:latest


To make things easy we will tag this image and give it a short name (oracle-xe-21.3):

1
docker image tag container-registry.oracle.com/database/express:latest oracle-xe-21.3

1
docker rmi container-registry.oracle.com/database/express:latest

You can list the images to see what images you have:

1
docker images


Now we can spin up a container from that image by running the following command:

1
2
3
4
5
6
docker run -d --name db-container\
>          -p 1521:1521\
>          -e ORACLE_PWD=1230123\
>          -v db-demo-volume:/opt/oracle/oradata\
>          --network=demo-network\
>          --hostname database
>          oracle-xe-21.3
  • The first flag -d will run the container in a detached mode.
  • The parameter --name specifies the container name.
  • -p maps the port 1521 on the host machine to the port 1521 in the container, so we can connect to the database.
  • With -e we can pass environment variables. In this case, ORACLE_PWD set the password for the SYY, SYSTEM, and PDB_ADMIN users.
  • -v mounts the volume we created for the oradata files in the container.
  • --network connects the container to the network we created.
  • --hostname give a name to the DB server. 
  • The last parameter is the image we want to use to spin up the container.
After the command finishes, you should see this message:


Now if you go to SQL Developer, you can connect to the Database just like shown below:


The password in our example is 1230123.

Download and build ORDS and APEX

Now we will pull the ORDS image from the Oracle registry, to do that run the following command:

1

docker pull container-registry.oracle.com/database/ords:latest


We will tag the image to make the name shorter:

1
docker image tag container-registry.oracle.com/database/ords:latest ords-21.4

1

docker rmi container-registry.oracle.com/database/ords:latest



For ORDS to connect to the database, it has to know the hostname where the DB is running, the password to use when connecting, and the service name. So, we will create a file on our host machine and save the connection string variable in it. When we spin up an ORDS container, we will mount this file to the container, and ORDS will use the connection string variable to connect to the DB. In this demo, we will create a folder on the desktop and name it ORDS, create a file in the ORDS folder and name it conn_string, in the file, we will store the connection string variable.


1
echo 'CONN_STRING=sys/1230123@database:1521/XEPDB1'>conn_string.txt

Please note that the connection string variable has to be in the following format:
conn_string=username/password@hostname:port/service
The hostname of our DB is database, which we gave it as we initialized the container and the password is 1230123.

For Windows Users, you need to remove the quote signs from the conn_string variable. Create a file and write the connection string variable like this:

CONN_STRING=sys/1230123@database:1521/XEPDB1

Now, we spin up an ORDS container by running the following command (You have to modify the 4th line so you point to where you saved the connection string file):

1
2
3
4
5
docker run -d --name ords 
--network=demo-network 
-p 8181:8181 
-v /Users/mohamadbouchi/Desktop/ords:/opt/oracle/variables 
ords-21.4

If you are using Windows then the command line then the command would be:

1
2
3
4
5
docker run -d --name ords 
--network=demo-network 
-p 8181:8181 
-v C:\Users\mohamadbouchi\Desktop\ords:/opt/oracle/variables 
ords-21.4
  • The first flag -d will run the container in a detached mode.
  • The parameter --name specifies the container name.
  • -p maps the port 8181 on the host machine to the port 8181 in the container, so we can connect to APEX.
  • -v moves the connection string file to /opt/oracle/variables in the container.
  • --network connects the container to the network we created.
  • The last parameter is the image we want to use to spin up the container.
Please notice, that we are attaching our network to the ORDS container, so both the DB container and ORDS container are running in the same network. And also we are mounting the folder where our connection string is stored to the container.
This will take a few minutes because it will install APEX in the DB, you can monitor the installation by running the following command in a new Console/Terminal window:

1
docker exec -it ords tail -f /tmp/install_container.log

When the APEX installation finishes,  you can open APEX in your browser using the following link:
localhost:8181/ords/


Use the following credentials to connect to the Internal Workspace:
  • Workspace: Internal
  • Username: Admin
  • Password: Welcome_1

Removing the Containers

After you finish, you may want to stop or remove the containers to free up system resources.
To stop the container you can simply run the following command

1
docker stop <container name>

At a later point you can start the container again with:

1
docker start <container name>

You can remove the container completely by running

1
docker rm <container name>

Since we are using volumes, our data will remain and we can mount it on different containers, or you can delete the volume, you can do that by running

1
docker volume rm <volume name>

Finally to remove the network

1
docker network rm <network name>


We hope you found this post useful :).

References:

Labels: , ,

5 Comments:

At July 17, 2022 at 9:33 PM , Anonymous Anonymous said...

I followed the tutorial but the ords installation is giving me ERROR:CONN_STRING_FILE has not add, create a file with CONN_STRING variable and added as docker volume . Help please

 
At July 19, 2022 at 6:43 AM , Anonymous Anonymous said...

Thanks i found it

 
At September 1, 2022 at 10:37 PM , Anonymous Anonymous said...

trying to load the conn_stringht.txt file with CONN_STRING variable results in the error : No such file or directorystring.txt: line 1: CONN_STRING=usr/pass@localhost:1521/XEPDB1
INFO : This container will start a service running ORDS 22.2.0 and APEX 22.1.0.
ERROR: CONN_STRING has not found in the container variables file.
user/password@hostname:port/service_name

HELP PLEASE

 
At December 5, 2022 at 4:11 PM , Anonymous Anonymous said...

Add a blank line before the connection string in the file conn_string.txt

 
At January 17, 2023 at 9:16 AM , Anonymous Sarwar Javaid said...

Hi,
I just configure and deploy apex as you describe. Now guide me how can I configure SSL.

 

Post a Comment

Note: Only a member of this blog may post a comment.

Subscribe to Post Comments [Atom]

<< Home