Sourcing /root/.zshrc in Docker using current user - docker

I am trying to build my container for a root user and then run it with my user and source the /root/.zshrc from inside.
Here is a minimal example:
FROM ubuntu:20.04
RUN apt update
RUN apt install --assume-yes --fix-broken \
curl \
wget \
zsh
RUN echo EDITOR=vim >> /root/.zshrc
RUN chmod a+rx /root
CMD [ "source /root/.zshrc", "zsh"]
The invocation is as follows:
docker run --rm -it -v "$HOME/.ssh:$HOME/.ssh:ro" -v "$HOME/.netrc:$HOME/.netrc:ro" -v /etc/passwd:/etc/passwd:ro -v /etc/shadow:/etc/shadow:ro -v /etc/group:/etc/group:ro --user $UID:$UID foo_minimal
The error that I'm getting is:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"source /root/.zshrc\": stat source /root/.zshrc: no such file or directory": unknown.
What could be the problem here?

Related

Docker image doesn't find the path to make work a cmd bash.sh file

I have built a Docker image which calls a bash file and processes some files in a specific folder, but I can't manage to make it run/work. I have tried different approaches but cannot find where the issue is. Building an image with
docker build -t user/mycontainer .
works, but the bash script doesn't run when I
docker run mycontainer `pwd`:/app
Instead it produces an error:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/path/c/to/where/i/run/teh/docker:/app": stat /path/c/to/where/i/run/teh/docker:/app: no such file or directory: unknown.
When I run docker ps there aren't any containers, but if I docker ps -a the container just built appears.
I try running
docker exec -it <container id build> bash
and I get a different error response from daemon:
Container <container id build> is not running
The docker image seems to build all the dependencies and the bash code works when run it separately in my local within the folder.
My dockerfile looks:
FROM alpine:latest
# Create directory in container image for app code
RUN mkdir -p /app
# Copy app code (.) to /app in container image
COPY . /app
# Set working directory context
ARG TIPPECANOE_RELEASE="1.36.0"
RUN apk add --no-cache sudo git g++ make libgcc libstdc++ sqlite-libs sqlite-dev zlib-dev bash \
&& addgroup sudo && adduser -G sudo -D -H tippecanoe && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& cd /root \
&& git clone https://github.com/mapbox/tippecanoe.git tippecanoe \
&& cd tippecanoe \
&& git checkout tags/$TIPPECANOE_RELEASE \
&& cd /root/tippecanoe \
&& make \
&& make install \
&& cd /root \
&& rm -rf /root/tippecanoe \
&& apk del git g++ make sqlite-dev
RUN chmod +x ./app/bash.sh
USER tippecanoe
WORKDIR /app
CMD ["./bash.sh"]

Running firefox in a ubuntu docker container on localhost: Unable to init server: Broadway display type not supported:

I am experimenting with running X11 GUI programs from a docker container on localhost:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y firefox
ARG home=/root
WORKDIR $home
COPY entrypoint.sh .
ENTRYPOINT ["./entrypoint.sh"]
where the entrypont.sh file is:
#! /bin/bash
firefox &
exec bash
Building the image with:
docker build -t firefox-ubuntu-2004 .
And running the container (localhost: Ubuntu 20.04):
XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
DISPLAY="localhost:0"
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
docker run -it --rm \
-e DISPLAY=$DISPLAY \
-v $XAUTH:$XAUTH \
-v $XSOCK:$XSOCK \
-e XAUTHORITY=$XAUTH \
firefox-ubuntu-2004
The output error message from running the last command is:
Unable to init server: Broadway display type not supported: localhost:0
Error: cannot open display: localhost:0
The --net=host in the docker command should do the job
docker run --name myContainer -it --net=host fromMyimage:latest
along with using host.docker.internal instead of localhost for connecting to the docker's host on OSX.

Permissions in Docker volume

I am struggling with permissions on docker volume, I get access denied for writing.
This is a small part of my docker file
FROM ubuntu:18.04
RUN apt-get update && \
apt-get install -y \
apt-transport-https \
build-essential \
ca-certificates \
curl \
vim && \............
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && apt-get install -y nodejs
# Add non-root user
ARG USER=user01
RUN useradd -Um -d /home/$USER -s /bin/bash $USER && \
apt install -y python3-pip && \
pip3 install qrcode[pil]
#Copy that startup.sh into the scripts folder
COPY /scripts/startup.sh /scripts/startup.sh
#Making the startup.sh executable
RUN chmod -v +x /scripts/startup.sh
#Copy node API files
COPY --chown=user1 /node_api/* /home/user1/
USER $USER
WORKDIR /home/$USER
# Expose needed ports
EXPOSE 3000
VOLUME /data_storage
ENTRYPOINT [ "/scripts/startup.sh" ]
Also a small part of my startup.sh
#!/bin/bash
/usr/share/lib/provision.py --enterprise-seed $ENTERPRISE_SEED > config.json
Then my docker builds command:
sudo docker build -t mycontainer .
And the docker run command:
sudo docker run -v data_storage:/home/user01/.client -p 3008:3000 -itd mycontainer
The problem I have is that the Python script will create the folder: /home/user01/.client and it will copy some files in there. That always worked fine. But now I want those files, which are data files, in a volume for backup porpuses. And as I am mapping with my volume I get permissions denied, so the python script is not able to write anymore.
So at the end of my dockerfile this instructions combined with the mapping in the docker run command give me the permission denied:
VOLUME /data_storage
Any suggestions on how to resolve this? some more permissions needed for the "user01"?
Thanks
I was able to resolve my issue by removing the "volume" command from the dockerfile and just doing the mapping at the moment of executing the docker run:
sudo docker run -v data_storage:/home/user01/.client -p 3008:3000 -itd mycontainer

Unknown Instruction : Sudo , when i try to build the docker image

When I try to build the below docker file , i get the error "Error response from daemon: Dockerfile parse error line 12: unknown instruction: SUDO"
FROM jenkins
USER root
RUN apt-get -qqy update; apt-get install -qqy sudo
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
RUN wget http://get.docker.com/builds/Linux/x86_64/docker-latest.tgz
RUN tar -xvzf docker-latest.tgz
RUN mv docker/* /usr/bin/
USER jenkins
RUN /usr/local/bin/install-plugins.sh junit git git-client ssh-slaves greenballs chucknorris ws-cleanup
sudo mkdir -p /var/jenkins_home
cd /var/jenkins_home
sudo chown -R 1000 /var/jenkins_home
Below commands doesn't belong to Dockerfile syntax
sudo mkdir -p /var/jenkins_home
cd /var/jenkins_home
sudo chown -R 1000 /var/jenkins_home
Add the RUN infront of them if you wants to run them. But the good practice is to mount folder from local to container. If you are tying to map the jenkins home folder, then create /var/jenkins_home folder on local system & then mount to docker container with -v option.
You can follow given link for using docker in dockerized jenkins: https://medium.com/#manav503/how-to-build-docker-images-inside-a-jenkins-container-d59944102f30

Docker. Exec npm command in different folder

I want to run npm install command in container.
But simple: docker exec container npm install is not the right thing for me.
I want to run this command in /home/client but my working directory in container is /home
Is that possible?
I don't want to enter container and I don't want to change working environment.
Edit 1
Dockerfile:
FROM ubuntu:16.04
COPY . /home
WORKDIR /home
RUN apt-get update && apt-get install -y \
python-pip \
postgresql \
rabbitmq-server \
libpq-dev \
python-dev \
npm \
mongodb
RUN pip install -r requirements.txt
Docker run command:
docker run \
-tid \
-p 8000:8000 \
-v $(PWD):/home \
--name container \
-e DB_NAME \
-e DB_USER \
-e DB_USER_PASSWORD \
-e DB_HOST \
-e DB_PORT \
container
Two commands in order to prove there is a directory /home/client:
docker exec container pwd
Gives: /home
docker exec container ls client
Gives:
node_modules
package.json
src
webpack.config.js
That's node modules from my host.
Edit 2
When run:
docker exec container cd /home/client
It produces the following error:
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"cd\": executable file not found in $PATH"
That is possible with:
docker exec {container} sh -c "cd /home/client && npm install"
Thanks to Matt
Yeah, it's possible. You can do it one of two ways.
Method 1
Do it in a single command like this:
$ docker exec container sh -c "cd /home/client && npm install"
Or like this (as an arg to npm install):
$ docker exec container npm install --prefix /home/client
Method 2
Use an interactive terminal:
$ docker exec -it container /bin/bash
# cd /home/client
# npm install

Resources