15 Apr, 2025 ยท 8 min read
"Learn Docker on AWS EC2 Instance - Ubuntu"
Yo devs!
FROM
(we'll dive into this in the sections below).1# Comment (describe the purpose) 2COMMAND arguments
COMMAND
doesn't have to be in uppercase, but it's a common practice to make it stand out from the arguments.1# Use Node.js version 18 as the base image 2FROM node:18 3 4# Set the working directory inside the container to /app 5WORKDIR /app 6 7# Copy all files from current directory on host to working directory in container 8COPY . . 9 10# Install dependencies using npm 11RUN npm install 12 13# Command to run when the container starts 14CMD [\"node\", \"index.js\"]
docker build -t <image_name> <dir>
and make sure you point it to the directory where your Dockerfile is hanging out..dockerignore
file comes in handy when you want to skip over some files in your Docker image context directory. 1FROM [--platform=<platform>] <image> [AS <name>] 2 3FROM [--platform=<platform>] <image>[:<tag>] [AS <name>] 4 5FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
1FROM python:3.11
1# syntax 2COPY <source> <destination> 3 4# example 5COPY localfile.txt /app/localfile.txt 6COPY . .
1# syntax 2ADD <source> <destination> 3 4# example 5ADD anotherfile.txt /app/anotherfile.txt 6ADD https://example.com/file.txt /app/file.txt 7ADD archive.tar.gz /app/
1# shell form, the command is run in a shell, 2# which by default is /bin/sh -c on Linux or cmd /S /C on Windows 3RUN <command> 4 5# exec form 6RUN [\"executable\", \"param1\", \"param2\"]
1# Install python packages 2RUN pip install -r requirements.txt
1# exec form, this is the preferred form 2CMD [\"executable\",\"param1\",\"param2\"] 3 4# as default parameters to ENTRYPOINT 5CMD [\"param1\",\"param2\"] 6 7# shell form 8CMD command param1 param2
1CMD echo $HOME 2or 3CMD [\"sh\", \"-c\", \"echo $HOME\"]
1# syntax 2WORKDIR <directory> 3 4# example 5WORKDIR /usr/src/app
1# exec form, which is the preferred form 2ENTRYPOINT [\"executable\", \"param1\", \"param2\"] 3 4# shell form 5ENTRYPOINT command param1 param2
Overriding CMD and ENTRYPOINT
1FROM ubuntu:latest 2 3ENTRYPOINT [\"echo\", \"Default ENTRYPOINT:\"] 4CMD [\"Hello from CMD\"] 5 6# Default behaviour will be like below 7# Default ENTRYPOINT: Hello from CMD
docker run <container_name> <arguments>
1docker run mycontainer \"Overridden CMD\" 2 3# CMD overriden output will be like below 4# Default ENTRYPOINT: Overridden CMD
--entrypoint
docker run --entrypoint <command> <container_name> <arguments>
1docker run --entrypoint /bin/echo mycontainer \"Overridden ENTRYPOINT\" 2 3# ENTRYPOINT overriden output will be like below 4# Overridden ENTRYPOINT
1LABEL maintainer=\"you@example.com\"
[\"/bin/sh\", \"-c\"]
, and on Windows, it's [\"cmd\", \"/S\", \"/C\"]
.1SHELL [\"executable\", \"parameters\"]
1SHELL [\"powershell\", \"-command\"] 2RUN echo Hello
1HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
1VOLUME <dir>
<key>=<value>
variables can be set at the same time using the ENV instruction.1# Syntax 2ENV <key>=<value> 3 4# Example 5ENV APP_NAME=\"MyApp\"
-p
flag with docker run
for specific ports, or use the -P
flag to open all exposed ports and map them to higher ports.1# Syntax 2EXPOSE <port> 3EXPOSE <port> [<port>/<protocol>...] 4 5# Example 6EXPOSE 8080 7EXPOSE 80/udp
1# sample .dockerignore file 2*.pyc 3__pycache__/ 4*.log 5config/ 6README.md
1my-project/ 2โโโ Dockerfile 3โโโ .dockerignore 4โโโ app/ 5โ โโโ main.py 6โ โโโ requirements.txt 7โ โโโ utils.py 8โโโ config/ 9โ โโโ config.yaml 10โโโ README.md
1# Use the official Python image 2FROM python:3.9 3 4# Set the working directory 5WORKDIR /app 6 7# Copy requirements file and install dependencies 8COPY requirements.txt . 9RUN pip install -r requirements.txt 10 11# Copy the rest of the application code 12COPY . . 13 14# Set the default command to run the application 15CMD [\"python\", \"app.py\"]
1# Use the official Node.js image 2FROM node:14 3 4# Set the working directory 5WORKDIR /app 6 7# Copy package.json and package-lock.json and install dependencies 8COPY package.json package-lock.json ./ 9RUN npm install 10 11# Copy the rest of the application code 12COPY . . 13 14# Build the application 15RUN npm run build 16 17# Expose the port the app runs on 18EXPOSE 3000 19 20# Set the default command to run the application 21CMD [\"npm\", \"start\"]
You're CRUSHING IT with Docker now! ๐ฅ Upcoming Part 3 - Docker Compose blog SOON! ๐๐ณ Stay tuned - it's gonna be EPIC! #DockerGang โจ Till thenโฆ