Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to use pushd/popd in Dockerfile?

Is there any way to use pushd/popd commands within a Dockerfile? It'd make some install scripts I have a lot easier if it were possible.

like image 309
Austin Avatar asked Jun 25 '18 15:06

Austin


People also ask

How do you use pushd and popd?

Unlike cd command, pushd and popd commands are used to manage a stack of directories. Just enter into a directory and do something you want to do, and "pop" back to the previous directory quickly without having to type the long path name. dirs command is used to show the current directory stack, just like "ls" command.

What does pushd popd do?

Both pushd and popd are shell builtin commands. The pushd command is used to save the current directory into a stack and move to a new directory. Furthermore, popd can be used to return back to the previous directory that is on top of the stack.

What happens when you use the pushd command?

The pushd command saves the current working directory in memory so it can be returned to at any time, pushd moves to the parent directory. The popd command returns to the path at the top of the directory stack. This directory stack is accessed by the command dirs in Unix or Get-Location -stack in Windows PowerShell.

How do I run multiple commands in Dockerfile?

Run Multiple Commands With Dockerfilethe semicolon (;) operator. the ambersand (&) operator. the AND (&&) operator. the OR (||) operator.


1 Answers

It can be done but your image must have bash and all commands must be in the same RUN directive:

Dockerfile

FROM debian
RUN mkdir -p /test/dir1/dir2
RUN bash -xc "\
pushd /test/dir1; \
pwd; \
pushd dir2; \
pwd; \
popd; \
pwd; \
"
RUN pwd
Sending build context to Docker daemon  77.25MB
Step 1/4 : FROM debian
 ---> 2d337f242f07
Step 2/4 : RUN mkdir -p /test/dir1/dir2
 ---> Using cache
 ---> d609d5e33b08
Step 3/4 : RUN bash -xc "pushd /test/dir1; pwd; pushd dir2; pwd; popd; pwd; "
 ---> Running in 79aa21ebdd15
+ pushd /test/dir1
+ pwd
+ pushd dir2
+ pwd
+ popd
+ pwd
/test/dir1 /
/test/dir1
/test/dir1/dir2 /test/dir1 /
/test/dir1/dir2
/test/dir1 /
/test/dir1
Removing intermediate container 79aa21ebdd15
 ---> fb1a07d6e342
Step 4/4 : RUN pwd
 ---> Running in 9dcb064b36bb
/
Removing intermediate container 9dcb064b36bb
 ---> eb43f6ed241a
Successfully built eb43f6ed241a
Successfully tagged test:latest
like image 98
Thomasleveil Avatar answered Sep 17 '22 17:09

Thomasleveil