My Dockerfile
FROM centos
RUN useradd me
CMD su -c "ping localhost" me
My test commands:
$ docker build -t test .
$ docker run --rm -it test
ping: icmp open socket: Operation not permitted
$ docker run --rm -it test /bin/bash
[root#153c87b53b53 /]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.126 ms
My temp solution is https://www.centos.org/forums/viewtopic.php?t=39341
chmod 4755 /usr/bin/ping
That is not a "temp solution" but the actual solution to allow user level pings - basically ping needs root level access to open up a socket in raw mode. So when it attempts to do this, but is not run as root, then you get the error above.
So in order for this to work, ping must be setuid root, which is what you are doing when you chmod 4755 /bin/ping - this means that when you run ping as a normal user, you elevate the privilege to root, but ping is smart enough to drop you back down to your user directly after opening the socket.
So your Dockerfile could look like this:
FROM centos
RUN chmod 4755 /bin/ping
RUN useradd me
CMD su -c "ping localhost" me
Related
Solution
The problem was that my VPN wasn't allowing any internal networking connections, I didn't realise that was possible.
What I'm trying to do
On my Ubuntu system run postgres within a docker container, then from the Ubuntu system connect to the container using psql. Note - I don't want to enter the container then run psql, I want to be able to connect to the running container from the OS using psql.
What I've tried
When I run the following command:
docker run --rm -d \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=example \
-e POSTGRES_USER=user \
-p 5432:5432 postgres:14.6-bullseye
I have the output:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd655ef0830a postgres:14.6-bullseye "docker-entrypoint.s…" 3 seconds ago Up 1 second 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp tender_ardinghelli
When I try connecting to this using the following psql command (this is run from my OS, not from within the container):
psql -h localhost --port 5432 --dbname example -U user
I get the error:
psql: error: connection to server at "localhost" (::1), port 5432 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I don't understand why this is happening - as I have explicitly stated which port I want to connect to, and I have set the ports (i think) correctly in the docker run command.
Updates
Try using a different port in docker run
"If you have a postgres running on the host OS, try to avoid issues by forwarding and connecting to a different port than 5432"
I tried altering the docker run command to:
docker run --name example \
--rm -d \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=db -e POSTGRES_USER=user \
-p 5499:5432 \
postgres:14.6-bullseye
Where I've changed the host port from 5432 to 5499 in case there's a conflict with postgres already running on the host system.
After running the docker run command above I have docker ps output of:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4172d2d12bb8 postgres:14.6-bullseye "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:5499->5432/tcp, :::5499->5432/tcp example
Trying to connect from the host using psql i have:
$ psql -h localhost --port 5499 --dbname db -U user
psql: error: connection to server at "localhost" (::1), port 5499 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Which is the same error :S
Try changing listen_addresses in postgres.conf
I have updated /etc/postgresql/14/main/postgresql.conf to have the following line:
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
And restarted postgres using:
sudo systemctl restart postgresql
But I have the same error as above.
system info
Ubuntu system:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy
Docker version:
Docker version 20.10.22, build 3a2c30b
host psql version
$ psql --version
psql (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
Run from the host: systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Sat 2022-12-31 02:18:33 GMT; 1min 0s ago
Process: 17727 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 17727 (code=exited, status=0/SUCCESS)
CPU: 1ms
What directories contain post, run from /etc:
$ /etc> find . -maxdepth 1 -type d | sort | grep post
./postgresql
./postgresql-common
when I start docker container like this:
sudo docker run -p5432:5432 -d -e POSTGRES_PASSWORD=test_pass -e POSTGRES_USER=test_user -e POSTGRES_DB=test_db --name postgres postgres:12
I can see it's started by command sudo docker ps. But when I try to connect to the container from host using
psql -Utest_user -p5432 -h localhost -d test_db
it just hangs for several minutes and then reports that wasn't able to connect.
But when I add --net host option like this:
sudo docker run --net host -p5432:5432 -d -e POSTGRES_PASSWORD=test_pass -e POSTGRES_USER=test_user -e POSTGRES_DB=test_db --name postgres postgres:12
everything starts working as expected, I can connect to the postgresql the same psql command.
The same happens to other containers which I run, not only created from postgres:12 image.
I can only make requests to them when I set --net host option.
But I need to expose different ports like for example 2000:5432 to run, for example, several postgres containers simultaneously.
What should I do to make it work? My machine is Ubuntu:20, in case if it matters, and docker is fresh new one installed by instruction from the official site yesterday.
You can't connect to database container because by default it only allows connections from the localhost ( local machines in the same network ).
When you start docker container it makes it's own network ( usually in 172.0.0.0/something ip range).
When you set the flag -net host, docker takes your host's ip address for it's own, and that's why you are able to connect to the database ( because then you are both on the same network ).
The solution is either use the -net host flag, or to edit the config file for the database container to allow external connections which is not recommended.
I am running a postgres docker container by using the commands below: (reference: https://docs.docker.com/engine/examples/postgresql_service/)
docker build -t eg_postgresql .
docker run --rm -P --name pg_test eg_postgresql
This works but the port number is dynamic. I can connect to the database by giving the port number. (the port I see in docker ps command)
I would like to connect to this docker database from Python so I need a static port number.
I tried the parameters below:
-p 127.0.0.1:5432:5432
-p 5432:5432
In that case, the docker container's port number was set as 5432. However, I could not connect to the database. I get docker user does not exist error message.
What is your advice?
I took the Dockerfile from the link you posted. After building the container with
docker build -t eg_postgresql .
I started the container with
docker run --rm -p 5432:5432 --name pg_test eg_postgresql (which binds localhost port 5432 to the container port 5432)
and then I tried to connect with
psql -h localhost -p 5432 -d docker -U docker --password
It works like a charm. If you get a message that docker user does not exist please double check that all steps from the Dockerfile are executed succesfully during the docker build command as the creation of the docker user is done in the command RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
createdb -O docker docker. Make also sure that you have no PostgreSQL server running on your localhost so that you can be sure that you are trying to connect to PostgreSQL inside the container.
I have successfully built a postgres-based Docker image that enables PostGIS:
The I run it:
docker run -d -t -p 5432:5432 -v ./data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm bash
However, when I try to connect to the database via psql:
psql -h localhost -p 5432 postgres
I get an error:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I am a beginner with the port forwarding, but it looks like a port-related issue to me.
Any ideas?
To access an application from within your container you need to first "attach" to that container.
You can do so by running the command:
docker exec -it container_name sh
What this command does is it runs the command sh inside the container container_name
It will prompt a shell terminal where you can now run your psql command like this:
psql -U postgres
Where here you're running psql with the user postgres (default authorized user for psql)
try this
docker run -d -t -p 5432:5432 -v $PWD/data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm
and then
psql -h localhost -p 5432 postgres
You've got:
psql: could not connect to server: Connection refused
Is the server running on host "192.168.99.101" and accepting
TCP/IP connections on port 5432?
So apply [Configure PostgreSQL to accept TCP/IP connections][https://www.mozmorris.com/2011/11/15/configure-postgresql-to-accept-tcpip-connections.html], but not in production, for tests purpose only !
And override your Dockerfile with this configuration
If I inspect the official mongo docker image, I can see that it exposes port 27017
$ docker inspect mongo
...
"ExposedPorts": {
"27017/tcp": {}
},
...
I have run the image, binding the internal port to the same on my host:
$ docker run -p 27017:27017 -d --name db mongo
I now run my own image in interactive mode, launching bash
$ docker run -i -t --link db:db_1 cd9b5953b633 /bin/bash
In my dockerized container, if I try to show open ports, nothing is listening.
$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
What am I doing wrong here? How can I connect from my dockerized container to the mongo container?
If it is of some use, here is my Dockerfile:
# https://registry.hub.docker.com/u/dockerfile/nodejs/ (builds on ubuntu:14.04)
FROM dockerfile/nodejs
MAINTAINER My Name, me#email.com
ENV HOME /home/web
WORKDIR /home/web/site
RUN useradd web -d /home/web -s /bin/bash -m
RUN npm install -g grunt-cli
RUN npm install -g bower
RUN chown -R web:web /home/web
USER web
RUN git clone https://github.com/repo/site /home/web/site
RUN npm install
RUN bower install --config.interactive=false --allow-root
ENV NODE_ENV development
# Port 9000 for server
# Port 35729 for livereload
EXPOSE 9000 35729
CMD ["grunt"]
Docker create a Network namespace, so within your container, you will not see the exposed port of the host.
In your usecase, you do not need to run mongo with -p if you just need to access it from an other container. The --link will simply "inject" the linked container info as environement variable.
From your new container, you can do env to see the list, and you will have something like DB_1_PORT_27027_TCP_ADDR with the private IP of the mongo container where you can connect.