Search code examples
imagedockerbuildcd

Docker Build Image -- cant cd into directory and run commands


Docker Version: 17.09.1-ce I am beginner in docker and I am trying to build docker image on centos. The below is the snippet of docker file i am having

FROM centos
RUN yum -y install samba-common && \
yum -y install gcc perl mingw-binutils-generic mingw-filesystem-base mingw32-binutils mingw32-cpp mingw32-crt mingw32-filesystem mingw32-gcc mingw32-headers mingw64-binutils mingw64-cpp mingw64-crt mingw64-filesystem mingw64-gcc mingw64-headers libcom_err-devel popt-devel zlib-devel zlib-static glibc-devel glibc-static python-devel && \
yum -y install git gnutls-devel libacl1-dev libacl-devel libldap2-dev openldap-devel && \
yum -y remove libbsd-devel && \
WORKDIR /usr/src && \
git clone git://xxxxxxxx/p/winexe/winexe-waf winexe-winexe-wafgit && \
WORKDIR /usr/src/samba && \
WORKDIR /usr/src/winexe-winexe-wafgit/source && \
head -n -3 wscript_build > tmp.txt && cp -f tmp.txt wscript_build && \
echo -e '\t'"stlib='smb_static bsd z resolv rt'", >> wscript_build && \
echo -e '\t'"lib='dl gnutls'", >> wscript_build && \
echo -e '\t'")" >> wscript_build && \
rm -rf tmp.txt && \
./waf --samba-dir=../../samba configure build

I tried with the normal cd which not work. WORKDIR does not work. How I can set working directory in Dockerfile?

I am getting an error like below using the above Dockerfile

/bin/sh: WORKDIR: command not found
The command '/bin/sh -c yum -y install samba-common &&     yum -y install gcc perl mingw-binutils-generic mingw-filesystem-base mingw32-binutils mingw32-cpp mingw32-crt mingw32-filesystem mingw32-gcc mingw32-headers mingw64-binutils mingw64-cpp mingw64-crt mingw64-filesystem mingw64-gcc mingw64-headers libcom_err-devel popt-devel zlib-devel zlib-static glibc-devel glibc-static python-devel &&     yum -y install git gnutls-devel libacl1-dev libacl-devel libldap2-dev openldap-devel &&     yum -y remove libbsd-devel && WORKDIR /usr/src &&     git clone git://xxxxxxxx/p/winexe/winexe-waf winexe-winexe-wafgit &&     WORKDIR /usr/src/samba &&     git reset --hard a6bda1f2bc85779feb9680bc74821da5ccd401c5 && WORKDIR /usr/src/winexe-winexe-wafgit/source &&     head -n -3 wscript_build > tmp.txt && cp -f tmp.txt wscript_build &&     echo -e '\t'"stlib='smb_static bsd z resolv rt'", >> wscript_build &&     echo -e '\t'"lib='dl gnutls'", >> wscript_build &&     echo -e '\t'")" >> wscript_build &&     rm -rf tmp.txt &&     ./waf --samba-dir=../../samba configure build' returned a non-zero code: 127

When I tried with normal cd instead work WORKDIR then I got below error /bin/sh: line 0: cd: /usr/src/samba: No such file or directory but with sudo i can go into it. Then I tried to include sudo cd directory in docker file then it said no sudo found

UPDATE 1: This is how I started build sudo docker build -t abwinexeimage -f ./abwinexeimage . The build got successfully but unfortunately when i list images i dont see any image with tag namme of abwinexeimage.

enter image description here

I dont understand what is that first entry with tag name as none. what it represents ? it shows size of 1.23 GB, Do I really need this image or can i safely delete ?

When I started build first line showed that Sending build context to Docker daemon 303.9MB that means in that image list repository named centos with tag name latest is one the right image which I built ? I assuming so as the size says 202 MB ?

Then I issued docker ps, but no container running, then issued docker ps -a to see stopped containers enter image description here

Then I tried to run image as container..

enter image description here

Now tried to issue docker ps to check whether container is running

enter image description here

Now i can tell you why i am so concerned about multiple containers present. Actually I wanted to manually CD into cd /usr/src/samba this inside docker container to verify if changes done via docker file got updated correctly or not. Now since i have multiple containers, really not sure which container I need to look into. In that stunt, i tried to start all containers, then manually issue docker exec -it CONTAINER_NAME [bash | sh] to verify if i am able to find that file system there. This is the reason why I asked whether I can have single container so that i can easily find the file system there,my understanding is since multiple RUN statements created different layers, then its difficult for me to find in which container my file system resides, so that I can CD into it.. sorry for big explanation... I am trying to understand concepts better. Your comments please..


Solution

  • You need to use WORKDIR as a Dockerfile instruction, instead of using it together with run instruction.

    RUN has 2 forms:

    • RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows) RUN

    • ["executable", "param1", "param2"] (exec form)

    WORKDIR

    WORKDIR /path/to/workdir The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile

    FROM centos
    RUN yum -y install samba-common && \
    yum -y install gcc perl mingw-binutils-generic mingw-filesystem-base mingw32-binutils mingw32-cpp mingw32-crt mingw32-filesystem mingw32-gcc mingw32-headers mingw64-binutils mingw64-cpp mingw64-crt mingw64-filesystem mingw64-gcc mingw64-headers libcom_err-devel popt-devel zlib-devel zlib-static glibc-devel glibc-static python-devel && \
    yum -y install git gnutls-devel libacl1-dev libacl-devel libldap2-dev openldap-devel && \
    yum -y remove libbsd-devel
    
    WORKDIR /usr/src 
    
    #use git clone with RUN not with WORKDIR
    RUN git clone git://xxxxxxxx/p/winexe/winexe-waf winexe-winexe-wafgit
    
    #So start it with new line
    
    WORKDIR /usr/src/samba
    WORKDIR /usr/src/winexe-winexe-wafgit/source 
    
    #start RUN with new line 
    
    RUN head -n -3 wscript_build > tmp.txt && cp -f tmp.txt wscript_build && \
    echo -e '\t'"stlib='smb_static bsd z resolv rt'", >> wscript_build && \
    echo -e '\t'"lib='dl gnutls'", >> wscript_build && \
    echo -e '\t'")" >> wscript_build && \
    rm -rf tmp.txt && \
    ./waf --samba-dir=../../samba configure build