RUN
- Run instruction will run the command in the container which is used to build image.
- RUN instruction will execute any linux command
-
Refer Here for official docs
-
In Docker file when we pass RAW commands to instructions we have two forms
- shell form: you pass raw command
bash
apt update
- exec form: you convert the raw command into array represntation
["apt", "update"]
-
In RUN instruction we prefer shell form
- IN CMD/Entrypoint we prefer exec form
Exercise 1
- Lets take ubuntu base image
- Lets install nodejs
FROM ubuntu:24.04
RUN apt update
RUN apt install nodejs npm -y
docker image build -t exercise:1 .
- Practice: Install the same with centos 9 as base image and alpine as base image
Exercise 2
- Lets take python base image
-
Lets run python –version
-
Dockerfile
FROM python:3-slim
RUN python --version
- exercise:
- Try java -version with java image
- try dotnet –list-runtimes with dotnet images
ADD/COPY
- ADD or COPY instructions help us in copying content into docker container which is used to build image and will be part of the image
- ADD supports urls and as well local paths
- COPY supports only local paths
Exercise 3
- Lets create an alpine base container where we copy a local text file into image
FROM alpine:3
ADD README.md /README.md
FROM alpine:3
COPY README.md /README.md
Exercise 4
- Lets create an alpine base container where we copy a file from internet into image
FROM alpine:3
ADD https://templatemo.com/tm-zip-files-2020/templatemo_617_pixel_forge.zip /design.zip
Exercise 5
- Lets copy the whole folder into docker image
FROM alpine:3
RUN mkdir app
COPY . /app
- I want to copy a local folder but i dont want Dockerfile to be copied into container and .venv folder to be copied into container
-
Create a file called as .dockerignore and in this file mention the files or folders or types which you dont want to copy
-
Exercise: COPY the fast api code into /app ignore
- virtual environment
- Dockerfile
- .dockerignore