how to run sqlplus from my laptop and connect to dockerized oracle launched with docker-compose? - oracle

I am trying to set up automated integration tests against an Oracle database and am
planning on using https://www.testcontainers.org/ to
launch the containers in my docker-compose.yml.
I have succeeded in getting Dockerized Oracle running on my laptop
using these instructions:
https://github.com/oracle/docker-images/tree/master/OracleWebLogic/samples/12212-oradb-wlsstore
The steps are:
# see https://docs.oracle.com/cd/E37670_01/E75728/html/oracle-registry-server.html
docker login container-registry.oracle.com # supply your oracle credentials as described above
docker network create -d bridge SampleNET
cat > env.txt <<EOF
DB_SID=InfraDB
DB_PDB=InfraPDB1
DB_DOMAIN=us.oracle.com
DB_BUNDLE=basic
EOF
docker run -d --name InfraDB --network=SampleNET -p 1521:1521 -p 5500:5500 --env-file env.txt -it --shm-size="8g" store/oracle/database-enterprise:12.2.0.1
After a minute when I issue the command "sudo docker ps -a" I see the DB is up and healthy.
CONTAINER ID IMAGE COMMAND
6a276a311b2e store/oracle/database-enterprise:12.2.0.1 "/bin/sh -c '/bin/ba…"
CREATED STATUS PORTS
2 minutes ago Up 2 minutes (healthy) 0.0.0.0:1521->1521/tcp, 0.0.0.0:5500->5500/tcp
NAMES
InfraDB
And I can now run sqlplus (from a shell prompt on my laptop, not a Docker container):
sqlplus sys/Oradoc_db1#localhost:1521/InfraDB.us.oracle.com AS SYSDBA
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
My goal now is to create a Docker compose file that allows me to
the same thing as what was accomplished with the command line invocation of Dockerized
Oracle. Here is the docker-compose.yml file I tried:
version: '2'
services:
database:
image: store/oracle/database-enterprise:12.2.0.1
ports:
- 1521:1521
- 8080:8080
- 5500:5500
environment:
- DB_SID:InfraDB
- DB_PDB:InfraPDB1
- DB_DOMAIN:us.oracle.com
- DB_BUNDLE:basic
networks:
default:
external:
name: SampleNET
So, with this docker compose file I can launch a shell in the
container running my Oracle DB and run sqlplus:
container=`sudo docker ps -a | grep oracle | sed -e's/\s.*//'`
sudo docker exec -t -i $container bash
# SESSION INSIDE DOCKER CONTAINER:
[oracle#8f26f224db03 /]$ sqlplus sys/Oradoc_db1 AS SYSDBA
SQL*Plus: Release 12.2.0.1.0 Production on Tue Mar 3 02:44:03 2020
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Now, the million dollar question:
What is the syntax of the sqlplus command I would specify
to connect FROM MY LAPTOP (i.e., not launching a shell in the
container running the DB server)?
Here are some of the many variants I tried (note: I tried with localhost, 127.0.0.1, and
the actual IP of my laptop):
sqlplus sys/Oradoc_db1#'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=InfraDB)))'
sqlplus sys/Oradoc_db1#'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=InfraDB.us.oracle.com)))'
Diagnostic Note:
I am running Docker on Mac.
After I launch Docker-ized Oracle I do see listeners on the ports that the docker compose configuration is forwarding, as shown below:
sudo lsof -i -P | grep -i "listen" | egrep "1521|8080|5500"
com.docke 6295 christopher.bedford 33u IPv6 0xfffffffffffffff 0t0 TCP *:1521 (LISTEN)
com.docke 6295 christopher.bedford 34u IPv6 0xfffffffffffffff 0t0 TCP *:5500 (LISTEN)
com.docke 6295 christopher.bedford 38u IPv6 0xfffffffffffffff 0t0 TCP *:8080 (LISTEN)
Problem Solved
Here is a summary of what I did to solve the problem thanks to the suggestion of my friend Matt (Changos Muertos below).
1). creation of the bridge network 'SampleNET' is not required.
2). seems like specifying environment variables inline like this (below) does not work
environment:
- DB_SID:InfraDB
- DB_PDB:InfraPDB1
- DB_DOMAIN:us.oracle.com
- DB_BUNDLE:basic
3). Instead it is better to define those environment settings in a file that is referenced by the 'env_file' configuration option as shown below. This file resides on /tmp/docker-compose.yml
version: '2'
services:
database:
image: store/oracle/database-enterprise:12.2.0.1
ports:
- 1521:1521
- 8080:8080
- 5500:5500
env_file:
- e2.txt
And the env file contents live in the same directory ( /tmp/e2.txt ), and has the content below:
DB_SID=InfraDB
DB_PDB=InfraPDB1
DB_DOMAIN=us.oracle.com
DB_BUNDLE=basic
To run i do
docker-compose -f /tmp/docker-compose.yml up
and then i can connect (from my laptop as was the original goal) via:
sqlplus sys/Oradoc_db1#0.0.0.0:1521/InfraDB.us.oracle.com as SYSDBA

This depends on a few things, like network type, and if the port is forwarded properly. Then verify this on the host (in linux 'netstat -tlpn' ), this will show you if it is listening on the port, and if so what interface and protocol. The tool may differ depending on the host OS.
Once you see it listening via tcp (not tcp6 only), and the firewall is open on the host, you can combine that info to make your connection string.

Related

Oracle 12c in Docker

For a while I'm searching for a good way to run Oracle in Docker. It was always ending up in huge images, and slow starting containers.
But today I saw the link: https://store.docker.com/images/oracle-database-enterprise-edition
Is this just an official Oracle12c we can use (I was able to pull and start it) or is this only for enterprise users?
Can I customize this image with my own .sql scripts? (With some entrypoint)?
You can follow the following steps:
docker login
docker pull store/oracle/database-enterprise:12.2.0.1
docker run -d -it --name oracledb12c -P store/oracle/database-enterprise:12.2.0.1
docker port containerID 1521/tcp (you will can connect to the datbase outside of the container using this port)
$ docker ps #check while the container status is not healthy)
Connect with sqlplus
docker exec -it containerID bash -c "source /home/oracle/.bashrc; sqlplus /nolog"
Connect with jdbc:
USER: sys PASSWD: Oradoc_db1
jdbc:oracle:thin:#(description=(address=(host=127.0.0.1)(protocol=tcp)(port=portNumner))
(connect_data=(service_name=ORCLPDB1.localdomain)))

Docker port mapping is failing for host network mode

Mac running Docker Version 17.12.0-ce-mac55 (23011) here.
I have a very bizarre situation with Docker that I absolutely cannot explain!
I have a Dockerized web service that runs perfectly fine outside of Docker, running off of port 9200 (so: http://localhost:9200)
I can also run several other images locally (nginx, Oracle DB) and I can access them via localhost:80 and localhost:1521 respectively
When I run the container for my Dockerized service, I see (via docker logs <containerId>) the service startup without any errors whatsoever
Despite the fact that the container is running without any errors, I absolutely cannot connect to it from my Mac host via localhost:9200
The exact steps to reproduce are:
Clone this repo
Build the image via ./gradlew clean build && docker build -t locationservice .
Run the container via docker run -it -p 9200:9200 -d --net="host" --name locationservice locationservice
If you use docker ps to obtain the <containerId>, then you can keep hitting docker logs <containerId> until you see it has started up without errors
On my machine, when I try to curl against localhost:9200, I get "connection refused" errors (see below)
curl error is:
curl -X GET http://localhost:9200/bupo
curl: (7) Failed to connect to localhost port 9200: Connection refused
Some things I have ruled out:
localhost is absolutely resolveable from the host because we're running in host network mode and I have no problem connecting to nginx (port 80) and Oracle (port 1521) containers
The app is starting up and if you look at the logs you'll see it is starting up listening on 9200
Any ideas what the problem could be?!
Docker for Mac runs in a VM. --net=host refers to the Linux VM hosts network stack not OSX. There is no direct network path from OSX to the Docker VM other than mapped ports.
Mapped ports (docker run -p Y:N) in Docker for Mac are a little special, in addition to the user space proxy that runs on the Docker host normally, Docker for Mac also launches a user space proxy on OSX to listen on the same port and forward connections into the VM. The OSX process isn't started when using --net=host (and the Linux one isn't either of course).
→ docker run --name nc --rm --net=host -dp 9200:9200 busybox nc -lk -p 9201 -e echo hey
→ docker inspect nc --format '{{ json .NetworkSettings.Ports }}'
{}
→ sudo lsof -Pni | grep 9200
→
Then without --net=host
→ docker run --name nc --rm -dp 9200:9200 busybox nc -lk -p 9201 -e echo hey
→ docker inspect nc --format '{{ json .NetworkSettings.Ports }}'
{"9200/tcp":[{"HostIp":"0.0.0.0","HostPort":"9200"}]}
→ sudo lsof -Pni | grep 9200
vpnkit 42658 matt 28u IPv4 0x57f79853269b81bf 0t0 TCP *:9200 (LISTEN)
vpnkit 42658 matt 29u IPv6 0x57f798532765ca9f 0t0 TCP [::1]:9200 (LISTEN)
If your app requires --net=host then I would use Vagrant/Virtualbox to spin up a VM with a "Host Only" adapter. This means there is a direct network path that you can access from OSX on the VM. Here's the Vagrantfile I use.
Docker for Mac does not support host network mode very well: https://github.com/docker/for-mac/issues/1031
So at this moment the solution is to use default bridge mode.

Docker need to access hosts's Postgres database

I have a Flask app that's running inside a Docker container, and I can run it using the following command.
docker run -e DB_HOST=<...> -e DB_PORT=<...> -e DB_NAME=<...> -e DB_USER=<...> -e DB_PASSWORD=<...> -p 8080:8080 <tag name>
Before the database was on AWS, now the database is running on a MAC laptop. So how can the Flask app from the docker container to connect to the hosts Postgres' database? What should be my DB_HOST?
For security reasons a Postgres server make it hard to connect to it. By default you can connect to the server from localhost and 127.0.0.1.
This does not work from within Docker since localhost refers to the running docker container not your laptop.
You have to connect from Docker to the IP address of your laptop.
You can find your laptop's IP on a Mac in System Preference > Network
Or you can use ifconfig | grep inet.
You need to modify 2 Postgres config file:
postgresql.conf and pg_hba.conf
For file: /usr/local/var/postgres/postgresql.conf
add this line to listen to everything:
listen_addresses = '*' # what IP address(es) to listen on;
Or add your specific laptop IP.
For file /usr/local/var/postgres/pg_hba.conf add a line like this with your IP:
host all all 192.168.1.3/32 md5
Restart the Postgres server when you are done.

How to connect to SSHD inside a Docker container from Windows?

I have a Ruby on Rails environment, and I'm converting it to run in Docker. This is largely because the development machine is a Windows laptop and the server is not. I have the Docker container mainly up and running, and now I want to connect the RubyMine debugger. To accomplish this the recommendation is to setup an SSH server in the container.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/207649545-Use-RubyMine-and-Docker-for-development-run-and-debug-before-deployment-for-testing-
I successfully added SSHD to the container using the dockerfile lines from https://docs.docker.com/engine/examples/running_ssh_service/#build-an-egsshd-image minus the EXPOSE 22 (because it wasn't working with the port mapping in the docker-compose.yml). But the port is not accessible on the local machine
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6652389d248c civilservice_web "bundle exec rails..." 16 minutes ago Up 16 minutes 0.0.0.0:3000->3000/tcp, 0.0.0.0:3022->22/tcp civilservice_web_1
When I try to point PUTTY at localhost and 3022, it says that the server unexpectedly closed the connection.
What am I missing here?
This is my dockerfile
FROM ruby:2.2
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
nodejs \
openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's#session\s*required\s*pam_loginuid.so#session optional pam_loginuid.so#g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
CMD ["/usr/sbin/sshd", "-D"]
RUN mkdir /MyApp
WORKDIR /MyApp
ADD Gemfile /MyApp/Gemfile
ADD Gemfile.lock /MyApp/Gemfile.lock
RUN bundle install
ADD . /MyApp
and this is my docker-compose.yml
version: '2'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/CivilService
ports:
- "3000:3000"
- "3022:22"
DOCKER_HOST doesn't appear to be an environment variable
docker version outputs the following
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 60ccb22
Built: Thu Feb 23 10:40:59 2017
OS/Arch: windows/amd64
Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 07:52:04 2017
OS/Arch: linux/amd64
Experimental: true
docker run -it --rm --net container:civilservice_web_1 busybox netstat -lnt outputs
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.11:35455 0.0.0.0:* LISTEN
SSHD is now running along side the Rails app, but the recipe that I was working from for setting up the service is not correct for the flavor of Linux that came with my base image https://docs.docker.com/engine/examples/running_ssh_service/#build-an-egsshd-image
The image I'm using is based on Debian 8. Could someone point me at where the example breaks down?
Your sshd process isn't running. That's visible in the netstat output:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.11:35455 0.0.0.0:* LISTEN
But as user2105103 points out, I should have realized that if I compared your docker-compose.yml with the Dockerfile. You define the sshd command in the image with a Dockerfile line:
CMD ["/usr/sbin/sshd", "-D"]
But then you override your image setting when running the container with the docker-compose command:
command: bundle exec rails s -p 3000 -b '0.0.0.0'
So, the only thing run, as you can see in the netstat, is the rails app listening on 3000. If you need multiple commands to run, then you can docker exec to kick off the second command (not recommended for a second service like this), use a command that launches sshd in the background and rails in the foreground (fairly ugly), or you can consider something like supervisord.
Personally, I'd skip sshd and just use docker exec -it civilservice_web_1 /bin/bash to get a prompt inside the container when you need it.

Docker & Postgres: Failed to bind tcp 0.0.0.0:5432 address already in use

Problem
I'm trying to start postgres in a docker container on my Mac, but I keep getting the following error message
docker: Error response from daemon: driver failed programming external connectivity on endpoint postgres (8392b9e5cfaa28f480fe1009dee461f97e82499726f4afc4e916358dd2d2f61e): Error starting userland proxy: Failed to bind tcp 0.0.0.0:5432 address already in use.
I have postgres installed locally, but I stopped it and running
pg_ctl status
returns
pg_ctl: no server running
I've ran the following to check what's running on 5432
lsof -i tcp:5432
&
netstat -anp tcp | grep 5432
and nothing is running on the port.
Versions
Mac - OS X El Capitan Version 10.11.2
PostgreSQL - 9.5
Docker - Docker version 1.12.0-rc2, build 906eacd, experimental
If lsof -i :5432 doesn't show you any output, you can use sudo ss -lptn 'sport = :5432' to see what process is bound to the port.
Proceed further with kill <pid>
If you execute lsof -i :5432 on the host you can see what process is bound to the port.
Some instance of Postgres is running. You can execute kill <pid> to kill it if you want. You can also use 5432 instead of 5432:5432 in your docker command or docker-compose file and let docker choose the host port automatically.
The first thing you should do is stop PostgreSQL service.
In most cases it fixed the issue.
sudo service postgresql stop
If above doesn't work. then add the following line to /etc/postgresql/12/main/postgresql.conf
sudo vim /etc/postgresql/12/main/postgresql.conf
## good if you add under CONNECTION AND AUTHENTICATION comments
listen_addresses = "*"
macOS Monterey
None of the above commands worked for me - need to do few changes. So, adding the complete working solution:
Identify what is running in port 5432: sudo lsof -i :5432
Kill all the processes that are running under this port: sudo kill -9 <pid>
Run the command again to verify no process is running now: sudo lsof -i :5432
it is worked for me, probably you should stop postgres :
sudo systemctl stop postgresql
In some cases it is critical to perform a more in-depth debugging to the problem before stopping or killing the container/process.
Consider following the checklist below:
1) Check you current docker compose environment
Run docker-compose ps. If port is in use by another container, stop it with docker-compose stop <service-name-in-compose-file> or remove it by replacing stop with rm.
2) Check the containers running outside your current workspace
Run docker ps to see list of all containers running under your host.
If you find the port is in use by another container, you can stop it with docker stop <container-id>.
(*) Because you're not under the scope of the origin compose environment - it is a good practice first to use docker inspect to gather more information about the container that you're about to stop.
3) Check if port is used by other processes running on the host
For example if the port is 6379 run:
$ sudo netstat -ltnp | grep ':6379'
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 915/redis-server 12
tcp6 0 0 ::1:6379 :::* LISTEN 915/redis-server 12
(*) You can also use the lsof command which is mainly used to retrieve information about files that are opened by various processes (I suggest running netstat before that).
So, In case of the output above the PID is 915. Now you can run:
$ ps j 915
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
1 915 915 915 ? -1 Ssl 123 0:11 /usr/bin/redis-server 127.0.0.1:6379
And see the ID of the parent process (PPID) and the execution command.
You can also run: $ pstree -s <PID> to a visual display of the process and its related processes (install with: brew install pstree).
In our case we can see that the process probably is a daemon (PPID is 1) - In that case consider running: A) $ cat /proc/<PID>/status in order to get a more in-depth information about the process like the number of threads spawned by the process, its capabilities, etc'.
B) $ systemctl status <PID> in order to see the systemd unit that caused the creation of a specific process. If the service is not critical - you can stop and disable the service.
4) Restart Docker service
Run sudo service docker restart.
5) You reached this point and..
Only if its not placing your system at risk - consider restarting the server.
None of these other answers worked for me. (For example, lsof and netstat just returned empty lines.) The following worked, though:
sudo -u postgres pg_ctl -D /Library/PostgreSQL/13/data stop
In case of mac,
if you are OK with uninstalling the POSTGRES for the time being:
brew uninstall postgres
Then check if the process still exists
sudo lsof -nP -i4TCP:5432 | grep LISTEN
If it exists, then kill it
kill -9 <pid>
Check again if the 5432 is being listened at, this time it should not be.
Go to project and click on docker-compose.yml
version: '2'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
links:
- db
- mail-server
db:
image: "postgres"
environment:
POSTGRES_PASSWORD: hunter2
ports:
- "5432:9432"
mail-server:
image: "mailhog/mailhog"
expose:
- 1025
ports:
- "8026:8026"
"
change the ports to 8026:8026 because there is already running another container on this port number only change the port number"
This command line is very simple and easy to remember, using third party javascript packages. npx comes built in with Node.js:
npx kill-port 3000
For a more powerful tool with search:
npx fkill-cli
I tried to sudo kill -9 <PID> to disable postgres process, but it spawns again and again with a different PID. After that, I found that it stores a process under LaunchDemos and it runs on every startup:
cd /Library/LaunchDemos/
sudo rm com.edb.launchd.postgresql-13.plist
Restart your PC to apply changes.

Resources