docker-compose: How can I pass ARG as variable to docker-compose? - docker

I have this docker-compose file
version: "3.7"
services:
develop:
build:
args:
user: sineverba
uid: 1000
context: ./
container_name: tpaw-develop
image: tpaw-develop:0.1.0
volumes:
- ./:/home/sineverba/app
networks:
- develop-tpaw
networks:
develop-tpaw:
driver: bridge
I can launch it with docker-compose run --rm develop.
I want pass that args as variable from docker-compose command and not hard-coding it.
Is it possible?
I did try with
version: "3.7"
services:
develop:
build:
args:
user: ${CURRENT_USER}
uid: 1000
context: ./
And launch
CURRENT_USER=$(whoami) docker-compose build app
But Docker created a container with user $user inside
PS this is the main Dockerfile
FROM php:7.4-cli
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Setup PHPXDebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
#WORKDIR /var/www
WORKDIR /home/$user/app
USER $user
ENTRYPOINT /bin/bash

Related

how to make a dockerfile with only one container?

I have a yii1 application. And I have a dockerfile. And I had a docker-compose file.
But for the momemnt I only have one application. Because I have a remote database. So the database is not in a container.
So I have this dockerfile:
FROM php:7.3-apache
#COPY BaltimoreCyberTrustRoot.crt.pem /usr/local/share/ca-certificates/AzureDB.crt
# Copy virtual host into container
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
# Enable rewrite mode
RUN a2enmod rewrite
# Install necessary packages
RUN apt-get update && \
apt-get install \
libzip-dev \
wget \
git \
unzip \
-y --no-install-recommends
# Install PHP Extensions
RUN docker-php-ext-install zip pdo_mysql
# RUN pecl install -o -f xdebug-3.1.3 \
# && rm -rf /tmp/pear
# Copy composer installable
COPY ./install-composer.sh ./
# Copy php.ini
COPY ./php.ini /usr/local/etc/php/
#COPY BaltimoreCyberTrustRoot.crt.pem /var/www/html/
EXPOSE 80
# Cleanup packages and install composer
RUN apt-get purge -y g++ \
&& apt-get autoremove -y \
&& rm -r /var/lib/apt/lists/* \
&& rm -rf /tmp/* \
&& sh ./install-composer.sh \
&& rm ./install-composer.sh
# Change the current working directory
WORKDIR /var/www/html
# Change the owner of the container document root
RUN chown -R www-data:www-data /var/www
# Start Apache in foreground
CMD ["apache2-foreground"]
And I had this docker-compose file:
version: '3'
services:
web:
build: ./docker
container_name: dockeryiidisc
ports:
- 80:80
- 443:443
volumes:
- C:\xampp\htdocs\webScraper/docker:/etc/apache2/sites-enabled/
- C:\xampp\htdocs\webScraper:/var/www/html/
and that worked.
But so now I only want to use the dockerfile.
So I tried this:
docker build -t docker_webcrawler .
and this command:
docker run -d -p 80:80 --name cntr-apache docker_webcrawler
But if I then go to: http://localhost:80
I only see a empty directory:
Index of /
[ICO] Name Last modified Size Description
So what I have to change? That I only have to use the dockerfile?
Thank you
It looks like you're missing the volume mappings that you have in your docker-compose file. Try this
docker run -d -p 80:80 --name cntr-apache -v C:\xampp\htdocs\webScraper/docker:/etc/apache2/sites-enabled/ -v C:\xampp\htdocs\webScraper:/var/www/html/ docker_webcrawler

How to deploy dockerized laravel app with elastic beanstalk?

I'm new to Docker. Trying to deploy dockerized laravel app using elastic beanstalk. Current Docker files -
docker-compose.yml -
version: '3'
services:
#PHP Service
app:
build:
context: ./
dockerfile: Dockerfile
image: admin
container_name: admin-app
restart: unless-stopped
working_dir: /usr/share/nginx/app/
volumes:
- ./:/usr/share/nginx/app/
networks:
- app-network
nginx:
image: nginx:stable-alpine
container_name: admin-nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./:/usr/share/nginx/app/
- ./nginx/conf.d/:/etc/nginx/conf.d
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
and Dockerfile
FROM php:7.4-fpm
ARG uid=1000
ARG user=sammy
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
libcurl4-openssl-dev pkg-config libssl-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
RUN pecl install mongodb && docker-php-ext-enable mongodb && \
pecl install xdebug && docker-php-ext-enable xdebug
RUN pecl config-set php_ini /etc/php.ini
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Add user for laravel application
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Copy existing application directory contents
COPY . /usr/share/nginx/app
WORKDIR /usr/share/nginx/app
RUN chown -R $user:$user .
USER $user
RUN chown -R $user:$user storage bootstrap/cache
RUN chmod -R 775 storage bootstrap/cache
RUN composer install
RUN php artisan cache:clear
RUN php artisan view:clear
RUN php artisan config:clear
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
It works fine at local machine when I run docker compose up -d only if I have already run composer install otherwise throws following error
It is ok for development purpose I have to run composer install once, but for production, I think it is not right way to manually do composer install every time new version is deployed. Doesn't the command RUN composer install at Dockerfile install the required dependencies? I can see progress bar of dependencies being installed but no vendor folder is generated if I ssh into container. Again it works fine if I ssh into instance and manually install dependencies.
I have deployed a nodejs app also successfully using elastic beanstalk. There the dependencies were installed properly using command RUN npm install at Dockerfile. I don't see any difference in the process. Do I have to include vendor folder also in the zip file?. Please suggest correct way to deploy.

When I want to rebuild docker image it generates an error

I made docker images and built them using docker-compose.yml, everything works fine for the first time but when I add/update some configurations on the Dockerfiles and rebuild the images an error regarding the group or the user I am creating inside the image generates:
> [ 2/11] RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel:
#5 0.396 addgroup: group 'laravel' in use
Here I am adding laravel as user and group.
The only work around I make is to delete all the built images: docker system prune -a
Below are Docker files and docker-compose.yml file configuration:
.env file:
PROJECT_NAME=myproject
DEV_DOMAIN=docker.local
USERNAME=laravel
UID=1000
PHP_VERSION=7.4-fpm-alpine
MYSQL_VERSION=latest
MYSQL_DATABASE=local_db
MYSQL_USER=homestead
MYSQL_PASSWORD=secret
XDEBUG_REMOTE_HOST=172.31.33.213
XDEBUG_IDE_KEY=VSCODE
XDEBUG_PORT=7765
docker-compose.yml:
version: "3.5"
services:
nginx:
build:
args:
user: ${USERNAME}
uid: ${UID}
context: .
dockerfile: ./docker/nginx/Dockerfile
image: "nginx:stable-alpine"
container_name: ${PROJECT_NAME}-nginx
ports:
- 80:80
volumes:
- "./code:/var/www/html"
- "./docker/nginx/vhost:/etc/nginx/conf.d/default.conf"
- "./docker/nginx/nginx.conf:/etc/nginx/nginx.conf"
depends_on:
- php
- mysql
networks:
- devproject
php:
build:
args:
PHP_VERSION: ${PHP_VERSION}
user: ${USERNAME}
uid: ${UID}
XDEBUG_REMOTE_HOST: ${XDEBUG_REMOTE_HOST}
XDEBUG_IDE_KEY: ${XDEBUG_IDE_KEY}
XDEBUG_PORT: ${XDEBUG_PORT}
context: .
dockerfile: ./docker/php/Dockerfile
image: php:${PHP_VERSION}
container_name: ${PROJECT_NAME}-php
volumes:
- "./code:/var/www/html"
- "./docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf"
ports:
- 9000:9000
networks:
- devproject
mysql:
image: mysql:${MYSQL_VERSION}
container_name: ${PROJECT_NAME}-mysql
restart: unless-stopped
tty: true
ports:
- 3306:3306
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- devprojectmysql:/var/lib/mysql
networks:
- devproject
networks:
devproject:
driver: bridge
volumes:
devprojectmysql:
driver: local
docker/php/Dockerfile:
ARG PHP_VERSION
FROM php:$PHP_VERSION
ARG XDEBUG_REMOTE_HOST
ARG XDEBUG_IDE_KEY
ARG XDEBUG_PORT
ARG user
ARG uid
ENV XDEBUG_REMOTE_HOST $XDEBUG_REMOTE_HOST
ENV XDEBUG_IDE_KEY $XDEBUG_IDE_KEY
ENV XDEBUG_PORT $XDEBUG_PORT
RUN addgroup -g $uid $user && adduser -G $user -g $user -s /bin/sh -D $user
RUN mkdir -p /var/www/html
RUN chown $user:$user /var/www/html
WORKDIR /var/www/html
RUN apk add --no-cache --update icu-dev gettext gettext-dev git curl libpng-dev libzip-dev \
libmcrypt-dev libpng-dev libjpeg-turbo-dev libxml2-dev icu-dev postgresql-dev curl-dev \
libmemcached-dev build-base autoconf zip unzip oniguruma-dev &&\
apk add --update --virtual build-dependencies build-base gcc wget autoconf
RUN pecl install xdebug-3.0.4
RUN docker-php-ext-install gd ctype fileinfo mysqli pdo pdo_mysql mbstring exif pcntl bcmath xml curl json intl gettext tokenizer zip simplexml
RUN docker-php-ext-configure intl &&\
docker-php-ext-configure gettext &&\
docker-php-ext-enable xdebug
RUN apk del build-dependencies &&\
rm -rf /var/cache/apk/* &&\
rm -rf /var/lib/apt/lists/*
RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_log=/tmp/xdebug_remote.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_port=$XDEBUG_PORT" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.idekey=$XDEBUG_IDE_KEY" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_host=$XDEBUG_REMOTE_HOST" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
docker/nginx/Dockerfile:
FROM nginx:stable-alpine
# Arguments defined in docker-compose.yml
ARG user
ARG uid
RUN mkdir -p /var/www/html
RUN addgroup -g $uid $user && adduser -G $user -g $user -s /bin/sh -D $user
RUN chown $user:$user /var/www/html
The image field in the compose file, e.g.
image: php:${PHP_VERSION}
Is the image you are creating. You have named your created images the same as your base images in the Dockerfile, overwriting the upstream names. This is a bug in your compose file, and confusing to others since running an image with that name does different things depending on whether you pull the upstream image or run your locally built image.
Instead your compose file should name images with something in your own registry namespace to avoid collisions. In Hub that involves prepending your Hub username:
image: <hubuser>/<appname>:<tag>

Docker not pulling updated php version

I am trying to update php version on the Docker
This is how my Dockerfile looks like
FROM php:7.2-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -d /home/ubuntu ubuntu
RUN mkdir -p /home/ubuntu/.composer && \
chown -R ubuntu:ubuntu /home/ubuntu
# Set working directory
WORKDIR /var/www
USER ubuntu
I have changed the php version to 7.3, and I tried to delete all docker containers and recreate it docker rm -vf $(docker ps -a -q). And then I built my docker containers using docker-compose build --nocache --pull.
docker-compose.yaml file looks like this:
version: "3.7"
services:
app:
build:
context: ./
dockerfile: ./docker/Dockerfile
image: myapp
container_name: myapp-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- myapp
But still the php version is stated as 7.2.
Any advice?
To remove all containers/images/networks/.. run:
docker system prune -a
Then try to build the image.
If that don't works: can you give the logs, where the wrong version will pulled?

Docker - No such file or directory. Copy does not copy all files

Dockerfile does not completely copy all files from the local directory.
I don’t understand why he copies the folder from the backend, but it doesn’t have all the files.
Thanks for help..
structure:
docker/
django/
/Dockerfile
/backend/
requirements.txt
src/
angular4/
/Dockerfile
/client
docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres:9.6
hostname: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: alinta
ports:
- "5432:5432"
backend:
build: ./django
image: alinta
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/var/www/alinta/
ports:
- "8000:8000"
depends_on:
- db
migration:
image: alinta
command: python3 manage.py migrate --noinput
volumes:
- .:/var/www/alinta/
depends_on:
- db
frontend:
build: ./angular4
volumes:
- .:/var/www/alinta
ports:
- "4200:4200"
Dockerfile (django):
FROM postgres:9.6
RUN apt-get update && apt-get install -q -y postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6 postgresql-client-common postgresql-common
#RUN echo postgres:postgres | chpasswd
#RUN pg_createcluster 9.6 main --start
#RUN /etc/init.d/postgresql start
FROM python:3.7
MAINTAINER Nikita Alekseev <nik_alekseev#outlook.com>
# Alinta
# Version: 1.0
# Install Python and Package Libraries
RUN apt-get update && apt-get upgrade -y && apt-get autoremove && apt-get autoclean
RUN apt-get install -y \
libffi-dev \
libssl-dev \
libxml2-dev \
libxslt-dev \
libjpeg-dev \
libfreetype6-dev \
zlib1g-dev \
net-tools \
vim
RUN apt-get install -y \
python3-pip \
python3-dev \
python3-virtualenv \
libpq-dev \
postgresql \
postgresql-contrib \
nginx \
curl
RUN pip3 install virtualenv
# Project Files and Settings
ARG PROJECT=alinta
ARG PROJECT_DIR=/var/www/${PROJECT}
RUN mkdir -p $PROJECT_DIR/backend/src
RUN mkdir -p $PROJECT_DIR/backend/src/static
RUN mkdir -p $PROJECT_DIR/backend/src/media
#WORKDIR $PROJECT_DIR
COPY ./backend /var/www/alinta/
WORKDIR $PROJECT_DIR/backend
RUN virtualenv -p python3.7 --no-site-packages env
RUN /bin/bash -c "source env/bin/activate"
RUN pip3 install -r requirements.txt
WORKDIR $PROJECT_DIR/backend/src
EXPOSE 8000
docker-compose build
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
For your Dockerfile your requirements.txt is in backend directory, and not on the same level, so adding or copying it will make it visible:
so add:
ADD ./backend/requirements.txt requirements.txt
or
COPY ./backend/requirements.txt requirements.txt
before you run
RUN pip3 install -r requirements.txt
Just copying backend directory as:
COPY ./backend /var/www/alinta/
will not allow Docker to know the context of your requrements.txt. Using ADD or COPY will instruct Docker how to locate file.

Resources