Cannot copy files from docker to tmp of host using docker-compose - docker

docker -v:
Docker version 1.12.1, build 23cf638
docker-compose.yml:
version: "2"
services:
test-docker:
build: ./test-docker
volumes:
- /tmp:/tmp
command: /bin/bash -c "mkdir -p /my && mkdir -p /tmp/my \
&& echo 'tmp:' && ls /tmp && echo 'code:' && ls /my \
&& cp -r /my/nLWjfTg9 /tmp/my/nLWjfTg9 \
&& cp -r /my/WzzrKGqe /tmp/my/WzzrKGqe"
Dockerfile:
FROM ubuntu:16.04 ENV TERM xterm
ENV DEBIAN_FRONTEND noninteractive
ADD http://pastebin.com/raw/nLWjfTg9 /my
ADD http://pastebin.com/WzzrKGqe /my
docker-compose up:
test-docker_1 | mkdir: cannot create directory '/my': File exists
tmp on host not create.

The ADD instruction in your Dockerfile creates the /my directory in the image.
In your command you run mkdir -p /my - which already exists, so you get your error and the command ends before copying any files.

Related

Building a Dockerfile from inside Docker Compose

So I'm trying to follow these instructions:
https://github.com/open-forest/sendy
I'm using Portainer and trying to run a Sendy container (newsletter software). Instead of running a MySQL image with it, I'm just using my external managed database instead.
On my server I keep project data at: /var/docker/project-name. I use this structure for bind mounting if I need to bring data into the containers from the start.
So for this project in the project-name folder I have sendy-6.0.2.zip and this Dockerfile: (This file was provide via the instructions on the above link)
#
# Docker with Sendy Email Campaign Marketing
#
# Build:
# $ docker build -t sendy:latest --target sendy -f ./Dockerfile .
#
# Build w/ XDEBUG installed
# $ docker build -t sendy:debug-latest --target debug -f ./Dockerfile .
#
# Run:
# $ docker run --rm -d --env-file sendy.env sendy:latest
FROM php:7.4.8-apache as sendy
ARG SENDY_VER=6.0.2
ARG ARTIFACT_DIR=6.0.2
ENV SENDY_VERSION ${SENDY_VER}
RUN apt -qq update && apt -qq upgrade -y \
# Install unzip cron
&& apt -qq install -y unzip cron \
# Install php extension gettext
# Install php extension mysqli
&& docker-php-ext-install calendar gettext mysqli \
# Remove unused packages
&& apt autoremove -y
# Copy artifacts
COPY ./artifacts/${ARTIFACT_DIR}/ /tmp
# Install Sendy
RUN unzip /tmp/sendy-${SENDY_VER}.zip -d /tmp \
&& cp -r /tmp/includes/* /tmp/sendy/includes \
&& mkdir -p /tmp/sendy/uploads/csvs \
&& chmod -R 777 /tmp/sendy/uploads \
&& rm -rf /var/www/html \
&& mv /tmp/sendy /var/www/html \
&& chown -R www-data:www-data /var/www \
&& mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
&& rm -rf /tmp/* \
&& echo "\nServerName \${SENDY_FQDN}" > /etc/apache2/conf-available/serverName.conf \
# Ensure X-Powered-By is always removed regardless of php.ini or other settings.
&& printf "\n\n# Ensure X-Powered-By is always removed regardless of php.ini or other settings.\n\
Header always unset \"X-Powered-By\"\n\
Header unset \"X-Powered-By\"\n" >> /var/www/html/.htaccess \
&& printf "[PHP]\nerror_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED\n" > /usr/local/etc/php/conf.d/error_reporting.ini
# Apache config
RUN a2enconf serverName
# Apache modules
RUN a2enmod rewrite headers
# Copy hello-cron file to the cron.d directory
COPY cron /etc/cron.d/cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron \
# Apply cron job
&& crontab /etc/cron.d/cron \
# Create the log file to be able to run tail
&& touch /var/log/cron.log
COPY artifacts/docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["apache2-foreground"]
#######################
# XDEBUG Installation
#######################
FROM sendy as debug
# Install xdebug extension
RUN pecl channel-update pecl.php.net \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& rm -rf /tmp/pear
Here is my Docker Compose file:
version: '3.7'
services:
project-sendy:
container_name: project-sendy
image: sendy:6.0.2
build:
dockerfile: var/docker/project-sendy/Dockerfile
restart: unless-stopped
networks:
- proxy
- default
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.project-secure.entrypoints=websecure"
- "traefik.http.routers.project-secure.rule=Host(`project.com`)"
environment:
SENDY_PROTOCOL: https
SENDY_FQDN: project.com
MYSQL_HOST: db-host-name-here
MYSQL_DATABASE: db-name-here
MYSQL_USER: db-user-name-here
MYSQL_PASSWORD: db-password-here
SENDY_DB_PORT: db-port-here
networks:
proxy:
external: true
When I try to deploy I get:
failed to deploy a stack: project-sendy Pulling project-sendy
Error could not find /data/compose/126/var/docker/project-sendy:
stat /data/compose/126/var/docker/project-sendy: no such file or directory
So here's what I've done.
I have the cron and artifacts folder on the same directory as the Dockerfile.
In the Dockerfile look for this line:
COPY artifacts/docker-entrypoint.sh /usr/local/bin/
Right below it put this line:
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
Otherwise you will get this error:
Starting Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/usr/local/bin/docker-entrypoint.sh": permission denied: unknown
Then build it with:
docker build -t sendy:6.0.2 .
Then your image will show up in portainer.
You can then remove the build section in your docker compose file and hit deploy. It now works for me.

Permission Denied on "docker compose exec --user alice app /bin/bash"

In my Dockerfile, I create a user, "alice" (generic name that isn't industry-specific) with a home directory. Alice has /home/.bashrc and root has /root/.bashrc (which all users can read - chmod a+r /root/bashrc).
I can run docker compose exec app /bin/bash and access the app container's command line as the root user. I can then su alice and have full access to the container as Alice.
However, if I run docker compose exec --user alice app /bin/bash, I get "bash: /root/.bashrc: Permission denied" followed by the "alice#sha:working/directory" cli prompt. ls ~/ gives the error: "ls: cannot open directory '/root/': Permission denied".
My docker-compose.yml file (abridged):
services:
app:
build:
context: ./docker/app
dockerfile: Dockerfile
args:
- HOST_GID=${HOST_GID}
- HOST_UID=${HOST_UID}
volumes:
${full_source_path}:/var/www/html
...
env_file: .env
My Dockerfile (abridged):
FROM --platform=$BUILDPLATFORM php:7.1-apache
# Set up Apache
RUN a2enmod rewrite
# UID & GID are passed in to use the same UID/GID as the host user's user account
ARG HOST_UID
ARG HOST_GID
RUN echo "Creating alice" && \
groupadd \
--force \
--gid ${HOST_GID} \
alice && \
sync && \
useradd \
--no-log-init \
--uid ${HOST_UID} \
--gid ${HOST_GID} \
--create-home \
--shell /bin/bash \
alice \
&& \
sync && \
echo "DONE"
# copy externally created files, including .bashrc, into /home/alice/
...
RUN chmod a+r /root/.bashrc
RUN chmod a+r /home/alice/.bashrc
RUN echo "Finalizing" && \
chown -R alice:alice /home/alice/ && \
echo "DONE"
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Persist cached writes
RUN sync
WORKDIR /var/www/html
ENTRYPOINT ["/entrypoint.sh"]
My entrypoint.sh file:
#!/usr/bin/env bash
set -e
echo "Starting Apache"
exec apache2-foreground
echo "Container Ready"
sleep infinity
My host:
MacOS 12.4
Docker Desktop 4.10.1

How to access docker compose mounted volume from inside the container

I am mounting a local file inside a docker container through the docker-compose.yml file:
version: '3'
services:
myapp:
build:
context: ./dockerfiles
dockerfile: myapp.Dockerfile
args:
- UID=1000
- GID=1000
network_mode: host
volumes:
- ./volumes/logs:/opt/myapp/logs
The mounted folder belongs to my user(uid: 1000, gid:1000) and these are the ids that the docker user gets, but the docker user cannot write to the mounted folder (permission denied).
Dockerfile:
FROM centos:7
ARG UID=1000
ARG GID=1000
RUN yum -y update && \
yum -y install epel-release && \
yum -y install passwd curl jq supervisor iputils openssl-devel
RUN yum -y clean all
RUN useradd -m -s /bin/sh user && \
passwd -d user && \
usermod -o -u ${UID} user && \
groupmod -o -g ${GID} user
VOLUME ["/opt/myapp/logs"]
ADD myapp /opt/myapp/app
ADD supervisor/services.ini /etc/supervisord.d/services.ini
ADD start.sh /
RUN chown -R user:user /opt/myapp
RUN chown user:user /start.sh
USER user
CMD ["/start.sh"]
start.sh
exec supervisord -c /etc/supervisord.conf -n
service.ini
[program:myapp]
user = user
autorestart = true
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes = 0
command = /opt/myapp/app
My app is run with user and it cannot write it's logs in the mounted folder.
My goal is to access the logs from outside the docker container.
Even if I run with the root user inside the container, I still cannot access the mounted folder!

Dockerfile VOLUME not visibile on host

In the WordPress Dockerfile, there's a VOLUME /var/www/html statement. If I understand correctly, this means that the WordPress files (in /var/www/html) should be mapped to the directory on my host containing the docker-compose.yml BUT this is not happening. Do you know why?
I created my own WordPress Dockerfile that extends the original WordPress Dockerfile where you'll find said VOLUME /var/www/html statement on line 44 (https://github.com/docker-library/wordpress/blob/b3739870faafe1886544ddda7d2f2a88882eeb31/php7.2/apache/Dockerfile).
I even tried to add the VOLUME /var/www/html statement at the bottom of my Dockerfile as you can see in my Dockerfile below. I added it just in case but I don't think anything is going wrong in there.
FROM wordpress:4.9.8-php7.2-apache
##########
# XDebug #
##########
# Install
RUN pecl install xdebug-2.6.1; \
docker-php-ext-enable xdebug
# Configure
RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \
echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \
echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \
echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \
echo "xdebug.remote_port=9000" >> /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_host=docker.for.win.localhost" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ##
###########
# PHPUnit #
###########
RUN apt-get update; \
apt-get install wget
RUN wget https://phar.phpunit.de/phpunit-7.4.phar; \
chmod +x phpunit-7.4.phar; \
mv phpunit-7.4.phar /usr/local/bin/phpunit
RUN phpunit --version
###################
# PHP Codesniffer #
###################
RUN curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar; \
mv phpcs.phar /usr/local/bin/phpcs; \
chmod +x /usr/local/bin/phpcs
############
# Composer #
############
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"; \
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"; \
php composer-setup.php; \
php -r "unlink('composer-setup.php');"; \
mv composer.phar /usr/local/bin/composer
##################
# Install Nodejs #
##################
RUN apt-get install -y gnupg2; \
curl -sL https://deb.nodesource.com/setup_11.x | bash -; \
apt-get install -y nodejs
##################
# Install Grunt #
##################
RUN npm install -g grunt-cli
#####################
# BASH customization#
#####################
RUN echo "alias ll='ls --color=auto -lA'" >> ~/.bashrc
VOLUME /var/www/html
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- ./docker-mysql/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: progonkpa/wordpress:1.0
restart: always
ports:
- "80:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
restart: always
volumes:
db_data:
The volume is being created, it just isn't being created in the execution context where you docker-compose.yml file lives. I assume you are running the ls -lah command and expecting something to be created in the directory where your docker-compose.yml file is. That is why you say, "BUT this is not happening"
The VOLUME command in the Dockerfile is limited. The host is unknown when you build an image from the Dockerfile. It is not until the docker run is executed using your built image that the Docker host is known.
And so, when using the VOLUME command in a Dockerfile and then using docker run with that image, the volume is created in a location configured by the Docker installation. To confirm that a volume has indeed been created for your container use this command:
docker inspect -f '{{ .Mounts }}' [container_name]
To have better control and specify where you VOLUME is created on your Docker host, you need to use the -v option with docker run or configure in your docker-compose.yml file, like is being done for your MySQL persistence container.
You can remove VOLUME /var/www/html from your Dockerfile, and you should. Because your FROM wordpress image creates the VOLUME, as you already know.

UNKNOWN: access denied: channel [] creator org [Org1MSP] - Hyperledger in docker-compose

My docker-compose is the same as the default one, exepting that it has this additional code:
hyperledger:
build:
context: .
ports:
- 8081:8081
My Dockerfile:
FROM hyperledger/composer-cli:0.19.0
WORKDIR /app
COPY . /app
USER root
RUN chown -R composer:composer .
USER composer
RUN ls -la
# RUN composer archive create -t dir -n .
RUN rm -rf .stage && mkdir ./.stage
RUN echo '{"name": "hyper-discover-network","x-type": "hlfv1","x-commitTimeout": 300,"version": "1.0.0","client": {"organization": "Org1","connection": {"timeout": {"peer": {"endorser": "300","eventHub": "300","eventReg": "300"},"orderer": "300"}}},"channels": {"composerchannel": {"orderers": ["orderer.example.com"],"peers": {"peer0.org1.example.com": {}}}},"organizations": {"Org1": {"mspid": "Org1MSP","peers": ["peer0.org1.example.com"],"certificateAuthorities": ["ca.org1.example.com"]}},"orderers": {"orderer.example.com": {"url": "grpc://orderer.example.com:7050"}},"peers": {"peer0.org1.example.com": {"url": "grpc://peer0.org1.example.com:7051","eventUrl": "grpc://peer0.org1.example.com:7053"}},"certificateAuthorities": {"ca.org1.example.com": {"url": "http://ca.org1.example.com:7054","caName": "ca.org1.example.com"}}}' > connection.json
EXPOSE 8081
USER root
RUN chmod 777 docker-entrypoint.sh
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT [ "sh", "/app/docker-entrypoint.sh" ]
CMD [ "composer" ]
And my docker-entrypoint.sh:
#!/bin/bash
# set -e
if [ "$1" = 'composer' ]; then
composer archive create -t dir -n .
echo '{...}' > connection.json
composer card create -p ./connection.json -u PeerAdmin -r PeerAdmin -r ChannelAdmin -f .stage/PeerAdmin.card \
-c crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem \
-k crypto-config/peerOrganizations/org1.example.com/ca/19ab65abbb04807dad12e4c0a9aaa6649e70868e3abd0217a322d89e47e1a6ae_sk
# composer-playground
composer card import -f .stage/PeerAdmin.card && composer card list && ls -la && pwd
echo 'som shit'
composer network install -a hyper-discover-network#0.0.1.bna -c PeerAdmin#hyper-discover-network
fi
The error appears at composer network install ... line.
I am building this environment by executing: ./startFabric.sh && docker-compose up hyperledger

Resources