Docker SWARM Not working - docker

I"m following the tutorial from https://docs.docker.com/get-started/part3/ on a Mac. This tutorial is to setup a Docker service using swarm node. However, I'm getting Empty reply from server if I goto http://localhost. I have verified that port 80 is with a Docker process, and the Docker container are running as well.
Mac-Machine: docker stack deploy -c docker-compose.yml getstartedlab
Creating network getstartedlab_webnet
Creating service getstartedlab_web
Mac-Machine:docker user1$ lsof -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 7592 user1 44u IPv4 0xfc021b13bc914389 0t0 TCP *:http (LISTEN)
Mac-Machine:docker user1$ curl http://localhost
curl: (52) Empty reply from server
Mac-Machine:docker user1$ docker service ls
ID NAME MODE REPLICAS IMAGE
w4dghr7jcpca getstartedlab_web replicated 5/5 dockhub-user1/get-started:part1
Mac-Machine:docker user1$ docker service ps w4dghr7jcpca
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
4oykrq8ge8yl getstartedlab_web.1 dockhub-user1/get-started:part1 moby Running Running about a minute ago
ba1n3m1pis2f getstartedlab_web.2 dockhub-user1/get-started:part1 moby Running Running about a minute ago
kmy8n4tm0n44 getstartedlab_web.3 dockhub-user1/get-started:part1 moby Running Running about a minute ago
cyeyozw6u8x7 getstartedlab_web.4 dockhub-user1/get-started:part1 moby Running Running about a minute ago
0evm9skw7p44 getstartedlab_web.5 dockhub-user1/get-started:part1 moby Running Running about a minute ago
Mac-Machine:docker user1$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5223c52b2014 dockhub-user1/get-started#sha256:2d3934a04a4aecc453652678489b2d96ce8d3dc5457aa8afdaeb71dbeff236ff "python app.py" 2 minutes ago Up About a minute 80/tcp getstartedlab_web.1.4oykrq8ge8ylw3ilbufxdp4t0
910b7b7521b3 dockhub-user1/get-started#sha256:2d3934a04a4aecc453652678489b2d96ce8d3dc5457aa8afdaeb71dbeff236ff "python app.py" 2 minutes ago Up About a minute 80/tcp getstartedlab_web.4.cyeyozw6u8x7j1zy1k82dugrn
d3ebd24cfe9a dockhub-user1/get-started#sha256:2d3934a04a4aecc453652678489b2d96ce8d3dc5457aa8afdaeb71dbeff236ff "python app.py" 2 minutes ago Up 2 minutes 80/tcp getstartedlab_web.5.0evm9skw7p44npujg6nbhckmy
ba29ffbdf2ce dockhub-user1/get-started#sha256:2d3934a04a4aecc453652678489b2d96ce8d3dc5457aa8afdaeb71dbeff236ff "python app.py" 2 minutes ago Up About a minute 80/tcp getstartedlab_web.2.ba1n3m1pis2flttytx87nucvb
6d8af1744b75 dockhub-user1/get-started#sha256:2d3934a04a4aecc453652678489b2d96ce8d3dc5457aa8afdaeb71dbeff236ff "python app.py" 2 minutes ago Up About a minute 80/tcp getstartedlab_web.3.kmy8n4tm0n44jgb1tc34qgeww
Here is the docker-compose.yml
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: dockhub-user1/get-started:part1
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
Image dockhub-user1/get-started:part1 was created using the following Docker file.
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Here is the app.py file
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
#app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)

After spending a whole afternoon debugging the issue, I found out that it was caused by Kubernetes. Stopping it from Docker Desktop resolved the issue.

Related

How to configure port mapping for replicated containers in Docker Compose?

The goal is to run two containers of publisher-app. One container should be mapped to port 8080 on the host machine, and the other on 8081. Here is the docker-compose:
publisher_app:
ports:
- "8080-8081:8080"
environment:
server.port: 8080
deploy:
mode: replicated
replicas: 2
Two containers are created, but as I understand, both ports are assigned to the first one, and the second one produces this error: Ports are not available: listen tcp 0.0.0.0:8081: bind: address already in use.
Here is the output of docker ps -a:
6c7067b4ebee spring-boot-rest-kafka_publisher_app "java -jar /app.jar" 33 seconds ago Up 28 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:8081->8080/tcp spring-boot-rest-kafka_publisher_app_2
70828ba8f370 spring-boot-rest-kafka_publisher_app "java -jar /app.jar" 33 seconds ago Created spring-boot-rest-kafka_publisher_app_1
Docker engine version: 20.10.11
Docker compose version: 2.2.1
How to handle this case? Your help will be very appreciated.
Here is the source code: https://github.com/aleksei17/springboot-rest-kafka-mysql/blob/master/docker-compose.yml
tried locally on Windows 10 and failed similarly, both with v2 and with v2 disabled.
It seems like a compose issue
when tried on arch: amd64 fedora based linux distro with package manager installed docker and manually installing docker-compose 1.29.2 (using the official guide for linux) worked:
compose file:
version : "3"
services:
web:
image: "nginx:latest"
ports:
- "8000-8020:80"
docker command:
docker-compose up --scale web=5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b304d397b2cd nginx:latest "/docker-entrypoint.…" 14 seconds ago Up 7 seconds 0.0.0.0:8004->80/tcp, :::8004->80/tcp testdir_web_4
a8c6f177a6e6 nginx:latest "/docker-entrypoint.…" 14 seconds ago Up 7 seconds 0.0.0.0:8003->80/tcp, :::8003->80/tcp testdir_web_3
b1abe53e7d7d nginx:latest "/docker-entrypoint.…" 14 seconds ago Up 8 seconds 0.0.0.0:8002->80/tcp, :::8002->80/tcp testdir_web_2
ead91e9df671 nginx:latest "/docker-entrypoint.…" 14 seconds ago Up 9 seconds 0.0.0.0:8001->80/tcp, :::8001->80/tcp testdir_web_5
65ffd6a87715 nginx:latest "/docker-entrypoint.…" 24 seconds ago Up 21 seconds 0.0.0.0:8000->80/tcp, :::8000->80/tcp testdir_web_1

Connection refused when running my vertx3 http handler in docker swarm

I'm trying to deploy a vertx3 service on docker swarm but when I run
$ curl localhost:4000
curl: (7) Failed to connect to localhost port 4000: Connection refused
and I can't figure out how to make it work, help appreciated. See below the code and more info.
This is my vertx3 application
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
public class MainVerticle extends AbstractVerticle {
#Override
public void start(Future<Void> fut) {
vertx.createHttpServer().requestHandler(r -> {
r.response().end("<h1>Powered by Vert.x3</h1>");
}).listen(8080, result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
});
}
}
this is my dockerfile
FROM vertx/vertx3
ENV VERTICLE_NAME com.shop.services.products.MainVerticle
ENV VERTICLE_FILE target/products-3.5.1.jar
ENV VERTICLE_HOME /usr/verticles
EXPOSE 8080
COPY $VERTICLE_FILE $VERTICLE_HOME/
WORKDIR $VERTICLE_HOME
ENTRYPOINT ["sh", "-c"]
CMD ["exec vertx run $VERTICLE_NAME -cp $VERTICLE_HOME/*"]
and this is my docker-compose.yml
version: "3"
services:
web:
image: sergionava89/products:v1
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:8080"
networks:
- webnet
networks:
webnet:
I try to run a local docker swarm
docker swarm init
then I get this message
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on interface enp0s25 (2a02:c7d:501a:f200:84fc:532e:737a:7a21 and 2a02:c7d:501a:f200:ad08:5748:44d4:6726) - specify one with --advertise-addr
so I run the following
docker swarm init --advertise-addr 2a02:c7d:501a:f200:84fc:532e:737a:7a21
then launch the container
docker stack deploy -c docker-compose.yml shop
to which I get
Creating network shop_webnet
Creating service shop_web
when I try to hit the endpoint I get
$ curl localhost:4000
curl: (7) Failed to connect to localhost port 4000: Connection refused
Output of docker service
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
sj8k1hd26cbk shop_web replicated 5/5 sergionava89/products:v1 *:4000->8080/tcp
docker service ps shop_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
sjsoh7atv24x shop_web.1 sergionava89/products:v1 sergio-ThinkPad-T431s Running Running 9 minutes ago
owakk9xqty69 shop_web.2 sergionava89/products:v1 sergio-ThinkPad-T431s Running Running 9 minutes ago
yvlopiq6vtjh shop_web.3 sergionava89/products:v1 sergio-ThinkPad-T431s Running Running 9 minutes ago
op7yv8vuliuk shop_web.4 sergionava89/products:v1 sergio-ThinkPad-T431s Running Running 9 minutes ago
z8imj3b572tg shop_web.5 sergionava89/products:v1 sergio-ThinkPad-T431s Running Running 9 minutes ago
As suggested in the comments 127.0.0.1:4000 works

Cannot start Portainer which compose

I don't know why but I cannot start portainer.
I downloaded https://github.com/portainer/portainer-compose
I did a docker-compose up
Everything seems fine:
portainer-proxy
docker ps
CONTAINER ID IMAGE COMMAND CREATED
9c01c18dcc23 portainer/portainer:latest "/portainer --temp..." 5 minutes ago
2de6b22cadb0 portainer_proxy "nginx -g 'daemon ..." 10 minutes ago
1c0166b3f870 v2tec/watchtower "/watchtower --cle..." 10 minutes ago
893a507f62e3 portainer/templates "nginx -g 'daemon ..." 10 minutes ago
And I have this in the logs :
portainer-app | 2017/11/12 15:01:54 Warning: the --templates / -t flag is deprecated and will be removed in future versions.
portainer-app | 2017/11/12 15:01:54 Starting Portainer 1.15.1 on :9000
I should be able to access portainer on port 9000, but nothing happens here.
If I try to access localhost then I have 404 from nginx.
Do you have any idea?
In the doc of the partainer compose, the link to access to partainer is: http://localhost/portainer (replace localhost by the ip of your server if necessary). So it uses the 80 port.
If you need to use the 9000 port, replace this line ine the docker-compose.yml:
ports:
- "80:80"
by
ports:
- "9000:80"
And access to it: http://localhost:9000/portainer
HTH

Docker compose yml inheritance

There are two tasks: run app container, run almost the same deploy-app container. The differences for them, for example, that deploy container does not have port sharing.
So, I made configs for this tasks...
./dockerfiles/base.yml:
app:
net: docker_internal_net
environment:
APPLICATION_SERVER: "docker"
./dockerfiles/base.run.yml:
app:
container_name: project-app
# set the build context to the project root
build: ..
volumes:
- /var/log/project/nginx:/var/log/nginx
- /var/log/project/php-fpm:/var/log/php5-fpm
- ..:/var/www/project
./dockerfiles/dev/run.yml:
app:
dockerfile: ./dockerfiles/dev/run-app/Dockerfile
ports:
- "80:80"
- "22:22"
environment:
DEV_SSH_PUBKEY: "$SSH_PUBLIC_KEY"
APPLICATION_PLATFORM: "dev"
./dockerfiles/dev/build.yml:
app:
container_name: project-app-deploy
# set the build context to the project root
build: ../..
dockerfile: ./dockerfiles/dev/build-app/Dockerfile
environment:
APPLICATION_PLATFORM: "dev"
volumes:
- ../..:/var/www/project
So I can tun the app container like this:
$ docker-compose -f ./dockerfiles/base.yml -f ./dockerfiles/base.run.yml -f ./dockerfiles/dev/run.yml up -d app
Creating project-app
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dae45f3f2c42 dockerfiles_app "/sbin/my_init" 2 seconds ago Up 1 seconds 0.0.0.0:2223->22/tcp, 0.0.0.0:8081->80/tcp project-app
Everything okay. But if then I trying to run deploy-app container, I will receive this message:
$ docker-compose -f ./dockerfiles/base.yml -f ./dockerfiles/dev/build.yml up -d app
Recreating project-app
WARNING: Service "app" is using volume "/var/www/project" from the previous container. Host mapping ".." has no effect. Remove the existing containers (with `docker-compose rm app`) to use the host volume mapping.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53059702c09b dockerfiles_app "/sbin/my_init" 6 seconds ago Up 4 seconds 22/tcp, 80/tcp project-app-deploy
This is because both of them are shared one local directory? But why I can run deploy-app container manually without docker-compose?
$ docker run -d --net docker_internal_net -e APPLICATION_SERVER=docker -e APPLICATION_PLATFORM=dev --name project-app-deploy -v ..:/var/www/project mybaseimage
86439874b8df561f529fde0d1e31824d70dc7e2a2377cd529331a2d7fcb00467
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
86439874b8df mybaseimage "/sbin/my_init" 4 seconds ago Up 3 seconds 22/tcp, 80/tcp project-app-deploy
40641f02a09b dockerfiles_app "/sbin/my_init" 2 minutes ago Up 2 minutes 0.0.0.0:2223->22/tcp, 0.0.0.0:8081->80/tcp project-app
I've solved my problem with the extend command in that way:
1) making changes into my ./dockerfiles/dev/build.yml file:
deploy-app:
extends:
file: ../base.yml
service: app
container_name: project-app-deploy
# set the build context to the project root
build: ../..
dockerfile: ./dockerfiles/dev/build-app/Dockerfile
environment:
APPLICATION_PLATFORM: "dev"
volumes:
- ../..:/var/www/project
2) run my deploy app container so:
$ docker-compose -f ./dockerfiles/dev/build.yml up -d deploy-app
Building deploy-app
...
Successfully built 74750fe274c6
Creating lovetime-app-deploy
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bb2af79ffaa dev_deploy-app "/sbin/my_init" 5 seconds ago Up 4 seconds 22/tcp, 80/tcp project-app-deploy
812b8824f1f0 dockerfiles_app "/sbin/my_init" 3 minutes ago Up 3 minutes 0.0.0.0:2223->22/tcp, 0.0.0.0:8081->80/tcp project-app
$ docker inspect -f '{{ .Mounts }}' project-app-deploy
[{ ...... /var/www/project rw true}]
Update:
According to documentation, this command is not supported in newer compose versions:
The extends keyword is supported in earlier Compose file formats up to Compose file version 2.1 (see extends in v1 and extends in v2), but is not supported in Compose version 3.x.

Docker port uncertainty

I am trying to access a running app on a port that I defined using "EXPOSE".
Here is what I get:
docker#boot2docker:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
653d8ee23260 nginx:latest "nginx -g 'daemon of 2 minutes ago Up 2 minutes 80/tcp, 443/tcp
insane_thompson
007cfcd0f539 highlighter:latest "java -jar -Xmx1500m 8 minutes ago Up 8 minutes 7777/tcp
elated_kirch
docker#boot2docker:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
653d8ee23260 nginx:latest "nginx -g 'daemon of 2 minutes ago Up 2 minutes 80/tcp, 443/tcp insane_thompson
007cfcd0f539 highlighter:latest "java -jar -Xmx1500m 8 minutes ago Up 8 minutes 7777/tcp elated_kirch
docker#boot2docker:~$ docker port 007cfcd0f539
docker#boot2docker:~$ docker port 653d8ee23260
docker#boot2docker:~$ docker port 653d8ee23260 80
FATA[0000] Error: No public port '80/tcp' published for 653d8ee23260
docker#boot2docker:~$ docker port 007cfcd0f539 7777
FATA[0000] Error: No public port '7777/tcp' published for 007cfcd0f539
Am I misunderstanding how the "port" command works?
EXPOSE in Dockerfile is not enough.
You need to specifically tell docker to expose port upon docker run with the -P/-p flags.
A much more detailed answer can be found here.

Resources