1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Serving multiple virtual hosts with Dockerfile and docker-compose.yaml

Discussão em 'Outras Linguagens' iniciado por ThurstonLevi, Outubro 17, 2024 às 07:22.

  1. ThurstonLevi

    ThurstonLevi Guest

    The scenario. I am trying to replicate a development box using docker but I'm struggling. So lets say I wish to add 3 virtual hosts to an ubuntu 24.04 server. I would normally create these in there own folders and then i put a symlink in the var/www for each of these and update hosts. I have managed to do this with the following example: Dockerfile

    FROM ubuntu:24.04

    # Set environment variable to avoid interactive prompts
    ENV DEBIAN_FRONTEND=noninteractive

    # Install required packages
    RUN apt-get update && apt-get install -y \
    openssh-server sudo neofetch apache2 apache2-utils\
    php php-mysql php-xml php-mbstring php-tokenizer \
    php-curl php-zip php-bcmath php-json wget unzip tzdata nano \
    mysql-client curl rsync composer bash phpmyadmin \
    lsd curl software-properties-common

    # Set the global ServerName to suppress the warning
    RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf

    # Install Starship
    RUN curl -sS https://starship.rs/install.sh | sh -s -- --yes

    # Create the SSH directory and set permissions
    RUN mkdir /var/run/sshd

    # Set root password (change 'rootpassword' to a secure password)
    RUN echo 'root:rootpassword' | chpasswd

    # Create user 'eclipse' with bash as the default shell and set password
    RUN useradd -m -s /bin/bash eclipse && echo 'eclipse:password' | chpasswd

    # Add 'eclipse' to the sudo group
    RUN usermod -aG sudo eclipse

    # Allow 'eclipse' to login via SSH
    RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
    RUN echo 'AllowUsers eclipse' >> /etc/ssh/sshd_config

    # Add SSH key for passwordless login
    RUN mkdir -p /home/eclipse/.ssh && \
    chmod 700 /home/eclipse/.ssh

    # Copy the SSH key from the private directory
    ADD ./private/authorized_keys /home/eclipse/.ssh/authorized_keys

    RUN chmod 600 /home/eclipse/.ssh/authorized_keys && \
    chown -R eclipse:eclipse /home/eclipse/.ssh

    # Create directories for WordPress and Laravel
    RUN mkdir -p /var/www/html/wordpress-test/htdocs
    RUN mkdir -p /var/www/html/laravel-test/htdocs
    RUN mkdir -p /home/eclipse/live/wordpress-test
    RUN mkdir -p /home/eclipse/live/laravel-test

    # Create symbolic links in /home/eclipse
    RUN ln -s /var/www/html/wordpress-test/htdocs /home/eclipse/live/wordpress-test/htdocs
    RUN ln -s /var/www/html/laravel-test/htdocs /home/eclipse/live/laravel-test/htdocs

    # Copy Apache configuration files
    COPY ./apache-config/wordpress.conf /etc/apache2/sites-available/wordpress.conf
    COPY ./apache-config/laravel.conf /etc/apache2/sites-available/laravel.conf
    COPY ./apache-config/phpmyadmin.conf /etc/apache2/sites-available/phpmyadmin.conf

    # Enable the new sites
    RUN a2ensite wordpress.conf
    RUN a2ensite laravel.conf
    RUN a2ensite phpmyadmin.conf

    # Enable mod_rewrite and mod_headers
    RUN a2enmod rewrite
    RUN a2enmod headers

    # Add alias and enable history in .bashrc
    RUN echo 'alias ls="lsd"' >> /home/eclipse/.bashrc
    RUN echo 'alias ll="ls -lah"' >> /home/eclipse/.bashrc
    RUN echo 'HISTFILE=/home/eclipse/.bash_history' >> /home/eclipse/.bashrc
    RUN echo 'HISTSIZE=1000' >> /home/eclipse/.bashrc
    RUN echo 'HISTFILESIZE=2000' >> /home/eclipse/.bashrc
    RUN echo 'eval "$(starship init bash)"' >> /home/eclipse/.bashrc

    # Source .bashrc to apply changes
    RUN echo 'source /home/eclipse/.bashrc' >> /home/eclipse/.bash_profile

    # Set up browser access
    RUN mkdir -p /opt/novnc/utils/websockify
    RUN ln -s /usr/share/novnc/vnc.html /opt/novnc/index.html

    # Copy and install the DejaVuSansMono font
    COPY ./fonts/DejaVuSansMono.zip /usr/share/fonts/
    RUN unzip /usr/share/fonts/DejaVuSansMono.zip -d /usr/share/fonts/ && \
    fc-cache -f -v

    # Copy custom Starship configuration
    COPY ./config/starship.toml /home/eclipse/.config/starship.toml

    # Expose SSH, Apache, and noVNC ports
    EXPOSE 22 80 8080

    # Start SSH and Apache services
    CMD service ssh start && service apache2 start && tail -f /dev/null


    and the compose file is

    version: '3.8'

    services:
    web:
    build: .
    ports:
    - "8000:80" # Expose Apache on port 8000
    - "2222:22" # Expose SSH port
    - "8080:8080" # Expose noVNC port
    environment:
    WORDPRESS_DB_HOST: db
    WORDPRESS_DB_USER: exampleuser
    WORDPRESS_DB_PASSWORD: examplepass
    WORDPRESS_DB_NAME: wordpressdb
    LARAVEL_DB_HOST: db
    LARAVEL_DB_USER: exampleuser
    LARAVEL_DB_PASSWORD: examplepass
    LARAVEL_DB_NAME: laraveldb
    volumes:
    - ./wordpress-test/htdocs:/var/www/html/wordpress-test/htdocs
    - ./laravel-test/htdocs:/var/www/html/laravel-test/htdocs
    - ./apache-config:/etc/apache2/sites-available
    networks:
    custom_network:
    ipv4_address: 172.30.0.2 # Static IP address

    db:
    image: mysql:5.7
    environment:
    MYSQL_ROOT_PASSWORD: somewordpress
    MYSQL_DATABASE: testdb
    MYSQL_USER: exampleuser
    MYSQL_PASSWORD: examplepass
    volumes:
    - db_data:/var/lib/mysql # Persistent volume for MySQL data
    - ./init.sql:/docker-entrypoint-initdb.d/init.sql # Mount the initialization script
    command: --default-authentication-plugin=mysql_native_password
    networks:
    custom_network:
    ipv4_address: 172.30.0.3 # Static IP address

    networks:
    custom_network:
    driver: bridge
    ipam:
    config:
    - subnet: 172.30.0.0/16

    volumes:
    wordpress_data: {}
    db_data: {}
    laravel_data: {}


    This sort of works, it builds correctly and I can ssh onto the container using ssh eclipse@172.30.0.2 all files are there and everything appears to be correct. However I have 3 failed tests out of the 88 so far created in my backup app (2 are accessing data from the wordpress database which i suspect is not there as not been setup other is to check the url exists). My primary issue at this point is accessing these from the browser to complete setup. I have tried updating my dev box /etc/hosts with 172.30.0.2 test.wordpress.app 172.30.0.2 test.laravel.app etc and also with 127.0.0.1 test.wordpress.app etc but it is either not working at all or all routes go to test.laravel.app whatever the url (I've seen this on my dev box before it routes the first alphabetically). Also it is redirecting from http to https (which isn't even setup and is not in .htaccess or in the database.

    So for the most part what I need this for is working (unit testing a backup app) but i need access to these via a browser (so i can configure them and also check the site exists as some of the tests need) either from the container or from my local browser, either will do but ideally both.

    My hope is that this can be done (i can see no reason why not as i do it on my own ubuntu 24.04 dev box) and i do not need a separate docker project for each one!

    thanks in advance

    Continue reading...

Compartilhe esta Página