Docker Run yields different result than docker-compose - docker

I have a docker compose file with an image that runs an npm install.
services:
test:
image: company.com/myImage:1.0.2
environment:
- HTTP_PROXY=http://proxy.com:8080
- HTTPS_PROXY=http://proxy.com:8080
Running docker-compose -f ./docker/docker.build.yaml up fails during the install with some type of dns issue
npm verb stack FetchError: request to https://company.com/artifactory/api/npm/npm-remote/lodash.merge failed, reason: getaddrinfo EAI_AGAIN company.com
however,
Running docker run company.com/myImage:1.0.2 works
npm http fetch GET 200 https://company.com/artifactory/api/npm/npm-remote/lodash.merge/-/lodash.merge-4.6.2.tgz 95ms
My company uses a proxy to connect to the internet so my local environment variables contain some proxy env vars. I tried hardcoding those env vars into the docker compose file but the result stayed the same.
What am I missing?
edit: added env vars I tested with to compose file

docker cli by default uses the docker host network whereas docker-compose was creating it's own docker_default network. My solution lied in specifying the docker network in the compose file
services:
test:
image: whatever
network_mode: "host"

Related

'Cypress could not verify that this server is running' error when using Docker

I am running Cypress version 10.9 from inside Docker in a Mac OS. I set my base URL as localhost:80. As a simple example, I am running an Apache server on localhost:80 which if I go to a web browser, I get the 'It works!' page, so it is indeed up. I also can ping localhost:80 from the same terminal I am executing my Docker Cypress container.
But I get this error every time when attempting to run my Cypress container:
Cypress could not verify that this server is running:
> http://localhost
We are verifying this server because it has been configured as your baseUrl.
I do see there are some stackoverflow posts(ie, [https://stackoverflow.com/questions/53959995/cypress-could-not-verify-that-the-server-set-as-your-baseurl-is-running][1]) that talk about this error. However, the application under test in these posts are inside another Docker container. The Apache page is not under a container.
This is my docker-compose.yml:
version: '3'
services:
# Docker entry point for the whole repo
e2e:
build:
context: .
dockerfile: Dockerfile
environment:
CYPRESS_BASE_URL: $CYPRESS_BASE_URL
CYPRESS_USERNAME: $CYPRESS_USERNAME
CYPRESS_PASSWORD: $CYPRESS_PASSWORD
volumes:
- ./:/e2e
I pass 'http://localhost' from my environment CYPRESS_BASE_URL setting.
This is the docker command I use to build my image:
docker compose up --build
And then to run the Cypress container:
docker compose run --rm e2e cypress run
Some other posts suggest running the docker run command with --network to make sure my Cypress container runs on the same network as the compose network(ref: Why Cypress is unable to determine if server is running?) but I am executing 'docker compose run' which does not have a --network argument.
I also verified that my /etc/hosts has an entry of 127.0.0.1 localhost as other posts have suggested. Any suggestions? Thanks.

docker-compose cannot resolve DNS

The problem is that docker compose cannot build image, failing on RUN npm ci. But after hours of debugging, I isolated the problem and pinned it in this minimal setup:
My docker-compose.yml
version: '3.8'
services:
myapp:
build:
dockerfile: Dockerfile
context: .
target: development
command: sleep Infinity
My Dockerfile
FROM node:18-alpine AS development
RUN ping google.com
When I run docker compose -f docker-compose.yml up -d --build
I'm getting error:
What I tried so far
In Dockerfile replace ping google.com to ping <real-ip>. ✅ And it works, so I assume it's DNS problem.
Add dns into docker-compose.yml: dns: 8.8.8.8. ❌ No luck
Run under super user sudo docker compose …. ❌ No luck
I tried to build image from Dockerfile without compose, using just docker build command. ✅ And it works, so the problem with docker compose.
Commented RUN ping … command, so it does not fail and runs sleep Infinity form the compose config. Then I connected into the container via docker exec -it <container> sh and was able to ping google and run npm ci. So when container is running it has access to DNS. The problem happens only in docker compose on the build stage from Dockerfile.
Environment
It's a VPS on hetzner. I ssh under a user with sudo and docker group.

When running docker-compose remotely, an error occurs with mounting volumes

I am trying to run a project on docker-compose via a remote server. Everything works, but as soon as I add the item about mounting the volume, it gives an error:
Error response from daemon: invalid mount config for type "bind": invalid mount path: 'C:/Users/user/Projects/my-raspberry-test' mount path must be absolute
To run I use tools from PhpStorm.
The docker-compose.yml file itself looks like this:
version: "3"
services:
php:
image: php:cli
volumes:
- ./:/var/www/html/
working_dir: /var/www/html/
ports:
- 80:80
command: php -S 0.0.0.0:80
I checked by ssh:
Daemon is running,
Docker works (on a similar Dockerfile with the same tasks),
Docker-compose works (on the same file).
Also checked docker remote run using phpstorm and file:
FROM php:cli
COPY . /var/www/html/
WORKDIR /var/www/html/
CMD php -S 0.0.0.0:80
It didn’t give an error and it worked.
OS on devices:
PC: Windows 10
Server: Fedora Server
Without mounting the volume in docker-compose, everything starts. Maybe someone faced a similar problem?
php for an example.
The path must be absolute for the remote host and the project data itself must be loaded there. That is, you need to upload the project to a remote host.
I corrected everything like this:
volumes:
- /home/peter-alexeev/my-test:/var/www/html/

Links to container in networking with docker-compose

My aim is to access a container via URL from another container using docker-compose.
So, suppose i have the following docker-compose.yml file
version: "3.8"
services:
web:
build: web
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres
ports:
- "8001:5432"
and a Dockerfile Dockerfile in the folder web
FROM alpine:3.7
RUN ping postgres://db:5432
Running docker-compose build returns
db uses an image, skipping
Building web
Step 1/2 : FROM alpine:3.7
---> 6d1ef012b567
Step 2/2 : RUN ping postgres://db:5432
---> Running in afbfcd27b340
ping: bad address 'postgres://db:5432'
Service 'web' failed to build : The command '/bin/sh -c ping postgres://db:5432' returned a non-zero code: 1
The docs for networking in docker compose (
https://docs.docker.com/compose/networking/#links) states:
Each container can now look up the hostname web or db and get back the appropriate container’s IP address. For example, web’s application code could connect to the URL postgres://db:5432 and start using the Postgres database.
What is the correct URL the connect to the container obtained from service db?
During the web image build, your db container does not exist, so using RUN is incorrect here.
One option would to include the CMD command in the Dockerfile which will instruct the web container to run the ping command every time the container is started up.
Also, I've adjusted the argument being passed to the ping command.
So, the web Dockerfile would be:
FROM alpine:3.7
CMD ["ping", "db:5432"]
Now, after docker-compose build and docker-compose up, you will see that the web container pings the db container on part 5432 and receives a response.
docker-compose starts a bridge network and adds all of the containers to this network so they can communicate with each other. Each container's hostname is the same as their service name in the docker-compose file. The hostnames are resoved by an internal DNS service.

Using proxy on docker-compose in server

When i run sudo docker-compose build i get
Building web
Step 1/8 : FROM python:3.7-alpine
ERROR: Service 'web' failed to build: error parsing HTTP 403 response body: invalid character '<' looking for beginning of value: "<html><body><h1>403 Forbidden</h1>\nSince Docker is a US company, we must comply with US export control regulations. In an effort to comply with these, we now block all IP addresses that are located in Cuba, Iran, North Korea, Republic of Crimea, Sudan, and Syria. If you are not in one of these cities, countries, or regions and are blocked, please reach out to https://support.docker.com\n</body></html>\n\n"
I need to set proxy for docker-compose for build
things i have tried:
looking at https://docs.docker.com/network/proxy/#configure-the-docker-client
i have tried setting ~/.docker/config.json
{
"proxies":
{
"default":
{
"httpProxy": "http://127.0.0.1:9278"
}
}
}
tried with --env argument
tried setting proxy variables on the server with no result
i also have tried this link
services:
myservice:
build:
context: .
args:
- http_proxy
- https_proxy
- no_proxy
but i get this on version: '3.6'
Unsupported config option for services.web: 'args'
these settings seem to be set on docker and not docker-compose
i also don't need to set any proxy on my local device (i don't want to loose portability if possible)
docker-compose version 1.23.1, build b02f1306
Docker version 18.06.1-ce, build e68fc7a
You must be from restricted countries which are banned by docker (from 403 status code). only way is to use proxies in your docker service.
[Service]
...
Environment="HTTP_PROXY=http://proxy.example.com:80/
HTTPS_PROXY=http://proxy.example.com:80/"
...
after that you should issue:
$ systemctl daemon-reload
$ systemctl restart docker
Include proxy details for each service in docker-compose.yml file, the sample configuration looks as below mentioned. Restart the docker and then run "docker-compose build" again. You might also run "docker-compose ps" to see if all the services mentioned in the compose file running successfully.
services:
<service_name>:
image:
hostname:
container_name:
ports:
environment:
HTTP_PROXY: 'http://host:port'
HTTPS_PROXY: 'http://host:port'
NO_PROXY: 'localhost, *.test.lan'
1: edit resolve.conf in linux, add ip to the top of line in resolv.conf
nameserver {type ip}
2: use a poxy and create an account in docker hub (https://hub.docker.com/)
3:login into docker
sudo docker login
user:
password:
4: if you have problem try step 3 again
You need to make an env file which you put proxy settings in
/usr/local/etc/myproxy.env
HTTP_PROXY=http://proxy.mydomain.net:3128
HTTPS_PROXY=http://proxy.mydomain.net:3128
Then run docker-compose with something like:
docker-compose -f /opt/docker-compose.yml --env-file /usr/local/etc/myproxy.env up

Resources