Dockerfile Reference – VOLUME, USER and WORKDIR Instructions
Hello Everyone,
Hope you’re doing good and utilizing time in learning new things. In this Dockerfile series, let’s learn VOLUME, USER and WORKDIR instructions in this article. If you want to refer other instructions please visit index page.
VOLUME Instruction:
VOLUME instruction is a huge topic, over here we’ll just understand it as a instruction. VOLUME basically mounts a host’s memory location with container. Syntax,
# JSON Array
VOLUME ["/sample/location"]
# Plain Arguments
VOLUME /sample/location1 /sample/location2
- If the value is JSON array, you must use double quote (“).
- docker run command creates the mentioned volume with any speified data.
- When using windows based container, volume must be non existing / empty directory and a drive other than C:.
- If you modify any content of volume directory once it is declared in Dockerfile, those changes will be discarded.
- To make VOLUME instruction platform indipendent, it is not mapped to a specific host location from Dockerfile as the same location might not be available when running image on other host. So we need to mention mountpoint when we create or run container.
FROM ubuntu
RUN mkdir /myvolume
# Creating greeting file with "Hello World"
RUN echo "Hello World" > /myvolume/greeting
# Create new mountpoint runtime at /myvolume and copy greeting file to newly created location.
VOLUME /myvolume
User Instruction:
User Instruction sets User using user name or UID and optionally user group or GID to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow in Dockerfile. Syntax,
USER <user>[:<group>]
USER <UID>[:<GID>]
- When you specify group for the user, the user will have only the specified group membership. Any other configured group membership will be ignored.
- When user doesn’t have a primary group then the image or following instructions will run with root group.
- On windows, user must be created if it’s not built-in account. It can be done using net user command called from Dockerfile.
FROM microsoft/windowsserver
# create user
RUN net user /add sanket
# set user
USER sanket
WORKDIR Instruction:
WORKDIR instruction sets the current working directory. While writing Dockerfile, we can not use change directory or cd command, so to change directory we can use WORKDIR instruction. Syntax,
WORKDIR /new/location
- WORKDIR sets the working directory for following instructions like RUN, CMD, ENTRYPOINT, COPY and ADD instruction that follows in Dockerfile.
- If the given directory doesn’t exist then it will be created even if it’s not being used in following instruction.
- Path can be absolute or relative, if relative path is given, it will be considered relative to current location.
- You can also use previously created environment variable in Dockerfile.
# Setting WORKDIR as /usr/local
WORKDIR /user/local
# changing WORKDIR as /usr/local/app
WORKDIR app
# Create Environment variable
ENV PATH=/usr
# Using variable in WORKDIR instruction - location /usr/local
WORKDIR $PATH/local
That’s it for this article, hope you got to know someting new or upgrade your knowledge. Please let me know your suggestion / doubts in comments. For complete series on Docker file, please visit index page. Keep Learning and Keep Sharing.
Reference : https://docs.docker.com