Sub topic 3
Running Oracle Database 26ai Free in a Container
This chapter assumes that you know basic SQL, can use a terminal, and understand that a database server accepts connections from client programs. It also assumes that Docker or Podman is installed and running. You will use Oracle Database 26ai Free locally, so your database files and resource usage are controlled by your computer.
How the container works
A container packages the Oracle database software and its runtime environment. The container runs a database server as an isolated process. Your SQL client runs outside the container and connects through a published network port.
Oracle Database uses a multitenant model:
- A container database, or CDB, is the main database instance.
- A pluggable database, or PDB, is a separate logical database inside the CDB.
FREEPDB1is the default PDB created by the Oracle Database Free image.- Port
1521is the listener port. The listener accepts client connections and directs them to a database service.
The host port and container port are both 1521 in the examples below.
Start the database container
Oracle's container image is hosted in the Oracle Container Registry. The following command downloads the image if necessary, creates a container named oracle-free, and starts it in the background.
docker run -d --name oracle-free \
-p 1521:1521 \
-e ORACLE_PWD='ChangeMe_123' \
container-registry.oracle.com/database/free:latestORACLE_PWD sets the initial password for administrative accounts such as SYS, SYSTEM, and PDBADMIN. Use a stronger password for anything beyond local practice. Do not put a real production password in a shell history or tutorial script.
The first startup can take several minutes. Oracle must create database files before it can accept connections. Follow the container log:
docker logs -f oracle-free
# Wait for the line: DATABASE IS READY TO USE!Press Ctrl+C to stop following the log. This does not stop the container. Check its state with:
docker psThe container should appear with a status such as Up. The -p 1521:1521 option maps port 1521 on your computer to port 1521 inside the container. Without this mapping, a client running on the host cannot reach the listener.
Connect to the CDB and PDB
An Easy Connect string identifies a database with a host, port, and service name:
host:port/service_nameUse FREEPDB1 as the service name when connecting to the default pluggable database. A SQL*Plus client must be installed on your computer for this command:
sqlplus sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdbaSYSDBA is an administrative connection privilege. It lets you perform tasks such as creating users. It is not appropriate for ordinary application work.
After connecting, check the current container:
SELECT sys_context('USERENV', 'CON_NAME') AS container_name,
sys_context('USERENV', 'SESSION_USER') AS session_user
FROM dual;A connection to FREEPDB1 should report FREEPDB1 as the container. If you connect without a service name or use the root service, you may be in the CDB root instead. User tables for practice should normally be created in the PDB, not in the root.
| Connection target | Typical purpose |
|---|---|
| CDB root | Database-wide administration |
| `FREEPDB1` | Application users, tables, and SQL practice |
| `localhost:1521` | The host and listener endpoint |
Create a development user
Working as SYS gives more privileges than a practice application needs. Create a separate user while connected to FREEPDB1:
CREATE USER app_user IDENTIFIED BY "ChangeMe_123" QUOTA UNLIMITED ON users;
GRANT DB_DEVELOPER_ROLE TO app_user;The QUOTA clause allows the user to allocate space in the USERS tablespace. DB_DEVELOPER_ROLE is an Oracle role that bundles common development privileges. In a real application, grant only the specific privileges required.
Reconnect as the new user:
sqlplus app_user/ChangeMe_123@localhost:1521/FREEPDB1Then run a complete DDL, DML, and query cycle:
CREATE TABLE practice_note (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
body VARCHAR2(200) NOT NULL
);
INSERT INTO practice_note (body)
VALUES ('Hello from my practice database');
COMMIT;
SELECT id, body
FROM practice_note;DDL means data definition language, such as CREATE TABLE. DML means data manipulation language, such as INSERT. COMMIT makes the inserted row permanent in the current transaction.
Common problems
ORA-12541: TNS:no listener: The container is not ready, is stopped, or port 1521 is already used by another process. Checkdocker psand the startup log.ORA-01017: invalid username/password: Check the password, username, service name, and whether the account is locked.- The connection reaches the wrong container: Include
/FREEPDB1in the Easy Connect string and rerun theSYS_CONTEXTquery. - The container name already exists: A previous container uses
oracle-free. Start it withdocker start oracle-free, or choose another name when creating a new container. - The image cannot be pulled: Sign in to the Oracle Container Registry if the registry requests authentication, then run the command again.
Exercise
Why should an application user connect to FREEPDB1 instead of using SYS in the CDB root?
Show answer
FREEPDB1 is the application-oriented pluggable database, while SYS in the CDB root has broad administrative privileges and is the wrong security boundary for ordinary application work.