Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to change directories while building docker Image using Dockerfile

I am trying to create an Image from the Dockerfile.

# cat Dockerfile 
FROM ubuntu:16.04
COPY $pwd/intel_virtual_gateway_console64_1_9_0.tar /root/
COPY $pwd/login.exp /root/
RUN cd /root
RUN echo $PWD
RUN tar -xvf intel_virtual_gateway_console64_1_9_0.tar 
RUN cd virtualgatewayconsole_package
RUN apt-get update && apt-get install expect \
                      expect-dev

While building the Image the directory is not getting changed to /root/. I thought the issue could be the tar file is missing, in order to confirm that printing the current working directory after the changing it to /root directory.But I have verified in the container that the packages were successfully copied to the /root directory. I have even verified by experimenting with other directories as well, even for those the directory is not getting changed. Due to this issue the consequent steps are failing:

# docker build -t release:1.0 .
Sending build context to Docker daemon  633.2MB
Step 1/8 : FROM ubuntu:16.04
 ---> 6a2f32de169d
Step 2/8 : COPY $pwd/intel_virtual_gateway_console64_1_9_0.tar /root/
 ---> Using cache
 ---> 36e9ea407082
Step 3/8 : COPY $pwd/login.exp /root/
 ---> Using cache
 ---> 578f9f9481d9
Step 4/8 : RUN cd /root
 ---> Running in 07ccfc507888
 ---> ad60f9d31c7e
Removing intermediate container 07ccfc507888
Step 5/8 : RUN echo $PWD
 ---> Running in e0ec2df6a0dc
/
 ---> 979a42368814
Removing intermediate container e0ec2df6a0dc
Step 6/8 : RUN tar -xvf intel_virtual_gateway_console64_1_9_0.tar
 ---> Running in 0701db595e27
tar: intel_virtual_gateway_console64_1_9_0.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
The command '/bin/sh -c tar -xvf intel_virtual_gateway_console64_1_9_0.tar' returned a non-zero code: 2

But able to change the directory within the container.

# docker run -it 979a42368814 /bin/bash
root@100b02ddc98a:/# pwd
/
root@100b02ddc98a:/# cd /root/
root@100b02ddc98a:~# pwd
/root

Please help to find out what is causing the issue.

like image 837
Here_2_learn Avatar asked Apr 20 '17 11:04

Here_2_learn


People also ask

Which Dockerfile command is best to use for changing the directory in a Dockerfile?

Using the cd Command In Linux, the cd command is the standard way to change the directory for most use cases. On the same note, when working with some docker instructions such as RUN, CMD, and ENTRYPOINT, we can use the cd command to change the directory for the current command in context.

How do I create a directory in Dockerfile?

RUN mkdir -p /var/www/html/foo creates the foo directory inside the filesystem of your container. docker-compose. yml ./code:/var/www/html "hides" the content of /var/www/html in the container filesystem behind the contents of ./code on the host filesystem.


2 Answers

use WORKDIR

https://docs.docker.com/engine/reference/builder/#workdir

or do all in one RUN

your cd is "forgotten" when you are in another RUN

By the way, group your RUN, as indicated in the Dockerfile best practices

https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

like image 58
user2915097 Avatar answered Oct 03 '22 00:10

user2915097


You need to use WORKDIR not cd in a RUN. https://docs.docker.com/engine/reference/builder/#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. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.

It can be used multiple times in the one Dockerfile. If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction. For example:

WORKDIR /a WORKDIR b WORKDIR c RUN pwd The output of the final pwd command in this Dockerfile would be /a/b/c.

The WORKDIR instruction can resolve environment variables previously set using ENV. You can only use environment variables explicitly set in the Dockerfile. For example:

ENV DIRPATH /path WORKDIR $DIRPATH/$DIRNAME RUN pwd The output of the final pwd command in this Dockerfile would be /path/$DIRNAME

# cat Dockerfile 
FROM ubuntu:16.04
COPY $pwd/intel_virtual_gateway_console64_1_9_0.tar /root/
COPY $pwd/login.exp /root/
WORKDIR /root
RUN echo $PWD
RUN tar -xvf intel_virtual_gateway_console64_1_9_0.tar 
WORKDIR virtualgatewayconsole_package
RUN apt-get update && apt-get install expect \
                  expect-dev

Regarding grouping RUN commands: You want to logically group commands into whatever you think of as a layer. Something that could be a template for other images. If you think of this as a single layer, then just concatenating all your run commands with &&s. Also, the final WORKDIR will be the current working directory of the docker image, so keep that in mind.

# cat Dockerfile 
FROM ubuntu:16.04
COPY $pwd/intel_virtual_gateway_console64_1_9_0.tar /root/
COPY $pwd/login.exp /root/
RUN cd /root && \
    echo $PWD && \
    tar -xvf intel_virtual_gateway_console64_1_9_0.tar && \
    cd virtualgatewayconsole_package && \
    apt-get update && apt-get install expect \
        expect-dev
WORKDIR /root/virtualgatewayconsole_package
like image 36
Novaterata Avatar answered Oct 03 '22 01:10

Novaterata