skip to Main Content

I’ve been fighting with my docker for 2 days to have a stable instance of my laravel, but how do I run it inside my AWS ec2 instance, and I already have some apache services running on that machine, and now I need to put this docker on that same machine, I did some tests and I’ll share the results with you, to see if you can help me:

Dockerfile

# Base image as Ubuntu 22.04
FROM ubuntu:22.04

# Avoid prompts from apt.
ENV DEBIAN_FRONTEND=noninteractive

# Install curl and iputils-ping to check internet connectivity
RUN apt-get update && apt-get install -y curl iputils-ping
RUN curl -s https://api.ipify.org || exit 1

# Install required packages
RUN apt-get install -y software-properties-common && 
    add-apt-repository ppa:ondrej/php && 
    apt-get update && apt-get install -y apache2 
    php8.1 php8.1-fpm php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl 
    mariadb-server mariadb-client composer

# Set your timezone
RUN ln -fs /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata

# Enable Apache mods
RUN a2enmod rewrite ssl

# Copy application source
COPY . /var/www/html

# Set the working directory
WORKDIR /var/www/html

# Remove composer.lock if present and install/update Composer dependencies
RUN if [ -f composer.lock ]; then rm composer.lock; fi && 
    composer install --no-interaction && 
    composer update --no-interaction

# Expose ports for web traffic
EXPOSE 80 443

# Start Apache in the foreground
CMD ["apache2ctl", "-D", "FOREGROUND"]

docker-composer.yml

version: '3.8'
services:
  web:
    build: .
    ports:
      - "8080:80"
      - "8443:443"
    volumes:
      - /var/www/backend/api:/var/www/html
      - /etc/apache2/sites-available/api.com.br.conf:/etc/apache2/sites-available/000-default.conf
      - /etc/apache2/sites-available/api.com.br-le-ssl.conf:/etc/apache2/sites-available/000-default-le-ssl.conf
    command: >
      bash -c "composer update && apache2ctl -D FOREGROUND"
    environment:
      - APACHE_DOCUMENT_ROOT=/var/www/html/public
    dns:
      - 8.8.8.8
      - 8.8.4.4

The build command generates my image and I give the up -d command without any problems, but when I access this machine and ping it, it does not recognize the internet.

Then test giving a command:

sudo docker run --dns 8.8.8.8 --dns 8.8.4.4 -it api_web:latest /bin/bash

and then it pings the internet correctly.

The container builds and runs without errors but fails to connect to the internet. Using docker run –dns configures DNS successfully and allows internet access. Attempts to set DNS in daemon.json and use host network mode have failed due to port conflicts with the existing Apache server.

How can I configure DNS within docker-compose.yml to ensure my Docker container has internet access without interfering with the host’s Apache setup?

UPDATE

So the question is how do you configure DNS via docker-composer? That doesn’t give an error but when I enter and ping it it doesn’t work or appear in etc, but via command it works. Considering these 3 things together, because I have to run it on a different port because there is already something running on the host which is an instance of ec2.

1

Answers


  1. I’ve created a minimal docker-compose.yml:

    services:
      web:
        image: ubuntu:22.04
        command: >
          bash -c "yes"
        dns:
          - 8.8.8.8
          - 8.8.4.4
    

    After docker-compose up:

    [gmt@arch dns-test]$ docker compose exec web cat /etc/resolv.conf
    

    Result:

    # Generated by Docker Engine.
    # This file can be edited; Docker Engine will not make further changes once it
    # has been modified.
    
    nameserver 127.0.0.11
    options ndots:0
    
    # Based on host file: '/etc/resolv.conf' (internal resolver)
    # ExtServers: [8.8.8.8 8.8.4.4]
    # Overrides: [nameservers]
    # Option ndots from: internal
    

    So DNS config in docker-compose.yml overrided at runtime. So I created a resolv.conf file in the directory of my docker-compose.yml:

    nameserver 8.8.8.8
    nameserver 8.8.4.4
    

    and changed the docker-compose.yml to:

    services:
      web:
        image: ubuntu:22.04
        command: >
          bash -c "yes"
        volumes:
          - ./resolv.conf:/etc/resolv.conf:ro
    

    and running:

    [gmt@arch dns-test]$ docker compose exec web cat /etc/resolv.conf
    

    Result:

    nameserver 8.8.8.8
    nameserver 8.8.4.4
    

    I hope it will help you. It is workaround and not an answer I like.

    I think another DNS server exists in your docker network, so in case of problems, by default docker prioritizes its own internal DNS mechanisms. This ensures essential functionalities are available even if user-specified DNS settings haven’t been fully applied yet.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search