> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Installing Oracle Database using Docker
- URL: https://linuxhandbook.com/oracle-db-docker/
- Published: 2024-11-21T03:45:55.000Z
- Updated: 2024-11-21T03:45:55.000Z
- Description: The tutorial walks through a much easier, hassle-free approach of installing oracle database, as a docker container.
- Author: Pranav Krishna
- Tags: Tutorial

Oracle databases are very popular in production environments, after Postgres and mysql, because of its robustness. 

It's also popular for being notoriously difficult to install in non-production environments, like the ones students and small scale applications use. 

The traditional installation process can be tiresome, often having to choose compatibility modes. Uninstalling the database once it's not needed is even more difficult. 

That's why I decided to share my experience of installing Oracle DB using Docker. As a student, I find this method the most comfortable in getting the database up and running sio that I could focus on the development rather than configuration. 

## Why use Docker for Oracle DB setup?

Docker can be used to deploy lightweight containers of the oracle database. These are disposable too, hence removing them will not be a hassle. 

Also, docker is available for all operating systems, hence these installation instructions can even be followed even on Windows!

Before getting into the tutorial, make sure you completely set up docker for your distribution. Ubuntu users can start here:

[How to Install Docker on Ubuntu Linux \[Beginner Tutorial\]In the first of Docker tutorial series, you’ll learn to install the latest version of Docker Engine Community Edition on Ubuntu Linux.![](https://linuxhandbook.com/content/images/icon/Linux-Handbook-New-Logo-10.png)Linux HandbookAvimanyu Bandyopadhyay![](https://linuxhandbook.com/content/images/thumbnail/Install_Docker_on_Ubuntu-1-1.png)](https://linuxhandbook.com/install-docker-ubuntu/)

## Obtaining Oracle Database image

Once you have installed docker, it's time to fetch the relevant image for your setup. For this tutorial, I am making use of the [latest express edition database](https://github.com/oracle/docker-images/tree/main/OracleDatabase?ref=linuxhandbook.com) (it's 21c during the time of writing). 

Start with pulling the docker image, using the following command:

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

![Download the required docker image using docker pull](https://linuxhandbook.com/content/images/2024/11/docker-pull-container.png)

Download the required docker image

💡

If you are unable to run docker commands normally, try running them as sudo, or [add your user to the docker group](https://linuxhandbook.com/usermod-command/). 

Once it has finished downloading, you can check for the downloaded images with the command

```bash
docker image ls
```

![output of docker image ls](https://linuxhandbook.com/content/images/2024/11/docker-image-ls.png)

List of all downloaded images

Your downloaded image should show up there. 

## Setting up the Docker container

[Docker compose](https://linuxhandbook.com/docker-compose-quick-start/) scripts are simple [YAML files](https://linuxhandbook.com/yaml-basics/) used to write the configuration of the docker container. This config file comes in very handy, since it makes it far easier to change settings. 

The setup also involves creating a docker volume for the persistent storage of data. 

### Creating a volume: persistent storage

Docker containers are temporary. Data stored in them maybe lost during a container restart. A [docker volume is a persistent storage](https://linuxhandbook.com/courses/docker/docker-volumes/) that exists outside the container, in the host storage. This is used to prevent loss of data even if the container is removed or restarted. 

The database requires one such volume, which I'll be creating as:

```bash
docker volume create oracledb-volume
```

![creating a docker volume](https://linuxhandbook.com/content/images/2024/11/docker-volume-2.png)

Create a docker volume

You can use the 'inspect' command to check the configuration in JSON format. Note that the [volume is located by default](https://linuxhandbook.com/docker-image-container-location/) at `/var/lib/docker/volumes/`. 

### Configuring docker compose

I have my docker compose scripts within a directory in my system. You can follow a similar setup:

```bash
mkdir oracle_database_express
cd oracle_databae_express
```

Within a file named `docker-compose.yml`, add the following lines:

```yaml
# oracle database: express edition container configuration
services:
  oracle_database_express:
    image: container-registry.oracle.com/database/express:latest
    container_name: oracle_database_express
    env_file:
      - ./.env
    volumes:
      - oracledb-volume:/opt/oracle/oradata:rw
    hostname: oracledbhost
    restart: always
    init: true
    tty: true
    ports:
      - 1521:1521

volumes:
  oracledb-volume:
    external: true

```

Let me explain what each part means:

- `image` refers to the docker image that I pulled already
- `container_name` will be the name/alias of the container, which I can refer in any operation (rather than the hash)
- `env_file` locates a file for the environment variables of the database instance
- `volumes: <volume-name>:/internal/path/in/container` maps the volume storage to some internal path, which should be stored permanently (hence a volume is used)
- `init: true`, `tty: true` is the equivalent of running `docker run **-it** <image>`
- `restart: always` will restart the container at all times, even during errors or a system restart
- `ports: *host_port:internal_port*` allocates a port in the host to the internal port; the database running at port 1521 has to be exposed outside of the container.

### Setting up environment variables

You can configure various environment variables relevant to oracle database in a `.env` file like this:

```env
# environment variables (without spaces)

ORACLE_PWD=mysecurepassword
```

For instance, the variable `ORACLE_PWD` sets the password for inbuilt accounts like `sys`, `system` etc of the database.

### Starting up the database

Once all configurations are complete, you can start the container with the following command (`-d` starts it in the background):

```bash
docker compose up -d
```

![Start the docker container](https://linuxhandbook.com/content/images/2024/11/oracledb-docker.svg)

Start the docker container

Running a `docker ps` gives you the status of the container. If the status is *healthy*, then the container is ready to be used.

![Status of the container](https://linuxhandbook.com/content/images/2024/11/Screenshot_20241118_011432.png)

Status of the container

To access your database via the *sqlplus* command line tool (that's included with the container), you can run a command inside the container with:

```bash
docker exec -it <container_name> <command>
```

![Access the database by running commands inside container](https://linuxhandbook.com/content/images/2024/11/oracledb-docker-accessing.svg)

Access the database by running commands inside container

### Stopping the database

To stop the container from execution, run 

```bash
docker compose stop
```

which temporarily stops the container. To start again, you can run 

```bash
docker compose start
```

Finally, to completely remove the container (during updating configurations and stuff), you can run 

```bash
docker compose down
```

## Changing the configuration

One of the main advantages of using docker compose is that the configuration is editable whenever possible. 

If the database is still running, terminate it by running 

```bash
docker compose down
```

Then, make any changes to the `docker-compose.yml` file. Here, I am changing the exposed port of the host for demonstration purposes. 

![Change configuration of database (docker compose)](https://linuxhandbook.com/content/images/2024/11/oracledb-change-config.svg)

Change configuration of database (docker compose)

Once the changes are done, you can once again run 

```bash
docker compose up -d
```

## Real use case: Using DB via an IDE

Undeniably, VSCode has been a popular text editor. It also provides an [Oracle SQL Developer Extension](https://marketplace.visualstudio.com/items?itemName=Oracle.sql-developer&ref=linuxhandbook.com) to work with oracle databases. 

You can connect to this database by creating a new connection, like this:

![Create a new database connection in VSCode](https://linuxhandbook.com/content/images/2024/11/database-connection-1.png)

Create a new connection in VSCode

![Test the configuration and save](https://linuxhandbook.com/content/images/2024/11/database-connection-configured.png)

Test the configuration and save

You should see a success message as a notification, like below, and then you can save the connection for later usage. 

![Success message from connection test](https://linuxhandbook.com/content/images/2024/11/database-connection-configured-successful.png)

Success message from connection test

You can now use this database to run SQL scripts. 

0:00 

/0:16 

1× 

Run SQL Scripts via an IDE

## Removing the entire database

Uninstalling this instance is as easy as removing one container. 

To remove your current database instance, you should remove the container as well as the persistent volume associated with it. 

```bash
docker compose down
docker container rm <container_name>
docker volume rm <volume_name>
```

Finally, since you don't need the DB anymore, you can also [remove the Docker image](https://linuxhandbook.com/remove-docker-images/) by referencing either the name or the hash.

```bash
docker image rm container-registry.oracle.com/database/express:latest
```

## Conclusion

This journey was fueled by looking for a quick installation of oracle database, that does not directly affect the host system. 

This tutorial takes you through the final revision of my attempt, that also enables changes in the configuration and the ability to dispose the container once the work is done.

Please let me know in the comments if you face any difficulties with the installation process.