docker pull nginx:latestdocker rmi (image id)docker run --name website -d -p 8080:80 nginx:latest
docker run --name website -d -p 8080:80 -p 3000:80 nginx:latest-d– detached mode-p hostPort:containerPort– port mapping
The container exposes port 80/tcp, so mapping 8080:80 allows access via http://localhost:8080.
docker rm -f $(docker ps -aq) //Removes **all** containers.
docker rm (container id/name)-f– force removal-a– all containers-q– show only IDs
docker run --name website \
-v C:/Users/krzys/OneDrive/Desktop/website:/usr/share/nginx/html \
-d -p 8080:80 nginx- /Desktop/website must have index.html file
- Creates a volume between the host directory and the Nginx HTML directory with index.html file
- Allows live updates of container content
docker exec -it website bash- you can go inside container and create file about.html inside nginx/html
Base images are always required, e.g.:
FROM nginx:latest
ADD . /usr/share/nginx/htmlThen you add all files which are needed to run the project,
like dependencies and all files using ADD,
destination is usually in base image documentation
docker build -t (name:version)
docker build -t website:latest .-t --tag– tag
FROM node:latest
WORKDIR /app
ADD . .
RUN npm install
CMD node index.jsWORKDIR /appsets the working directory (if it doesn't exist like in nginx)RUN npm installinstalls all dependencies from package.jsonCMD node index.jsdefines the command executed when the container starts
Node listens on port 3000 by default, so run with:
docker run -p 8080:3000 image-nameUsed to exclude unnecessary files from the image:
node_modules //is created during 'npm install' command
.git
.env
Reduces image size and build time.
To improve image's actualization speed.
ADD . .
RUN npm installAny change triggers npm install again.
FROM node:latest
WORKDIR /app
ADD package*.json .
RUN npm install
ADD . .
CMD node index.js CACHED [2/5] WORKDIR /app 0.0s
=> CACHED [3/5] ADD package*.json . 0.0s
=> CACHED [4/5] RUN npm install (see, npm install is CACHED) 0.0s
=> [5/5] ADD . .
This leverages Docker layer caching and speeds up rebuilds.
You use alpine linux, every base image should have supported tags where will be alpine.
- Alpine significantly reduces image size.
docker pull node:lts-alpineFROM node:lts-alpine
WORKDIR /app
ADD package*.json .
RUN npm install
ADD . .
CMD node index.jslts- Long-Term-Support
Avoid using latest in production to avoid sudden changes in version of base image for your app.
FROM nginx:1.29.4-alpine
FROM node:24.12.0-alpinedocker tag user-service:latest user-service:1Both tags reference the same image ID, enabling easy rollback.
- Create a repository on Docker Hub
- Rename image:
docker tag user-service:1 kr1s6/krzysztof-repo:1- Login to Docker
- Push images:
docker push kr1s6/krzysztof-repo:1
docker push kr1s6/krzysztof-repo:2
docker push kr1s6/krzysztof-repo:latestdocker inspect <container_id>
docker logs <container_id>inspect– container metadatalogs– application output (e.g.console.log, HTTP requests)
docker exec -it <container_id> /bin/bash-i– interactive-t– pseudo-TTY
If /bin/bash is unavailable, inspect the container to find the correct shell (Cmd).
docker exec -it website bash- Enables creating/editing files inside the container
- Changes are reflected on the host via the volume