0
0
Dockerdevops~15 mins

WORKDIR instruction for directory in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Using WORKDIR Instruction in Dockerfile
📖 Scenario: You are creating a Docker image for a simple web application. You want to organize your files inside the container by setting a working directory.
🎯 Goal: Learn how to use the WORKDIR instruction in a Dockerfile to set the working directory for subsequent commands.
📋 What You'll Learn
Create a Dockerfile with a base image
Set a working directory using WORKDIR
Add a file inside the working directory
Print the current working directory inside the container
💡 Why This Matters
🌍 Real World
Setting a working directory in Docker images is common to keep files organized and commands simple.
💼 Career
Understanding <code>WORKDIR</code> is essential for writing clean Dockerfiles used in software development and deployment.
Progress0 / 4 steps
1
Create a Dockerfile with base image
Create a Dockerfile and write the line FROM alpine:latest to use the Alpine Linux base image.
Docker
Need a hint?

The FROM instruction sets the base image for your Dockerfile.

2
Set the working directory
Add the line WORKDIR /app to set the working directory inside the container to /app.
Docker
Need a hint?

The WORKDIR instruction sets the directory where commands will run inside the container.

3
Add a file inside the working directory
Add the line RUN echo "Hello from /app" > message.txt to create a file named message.txt inside the /app directory.
Docker
Need a hint?

The RUN instruction executes commands inside the container. Here it creates a file in the working directory.

4
Print the current working directory
Add the line CMD ["pwd"] to print the current working directory when the container runs.
Docker
Need a hint?

The CMD instruction defines the command to run when the container starts.