Skip to content

kr1s6/Docker-node-nginx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Docker – Practical Notes & Commands

Pulling Base Images

docker pull nginx:latest

Removing Images

docker rmi (image id)

Running Containers & Port Mapping

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.

Removing Containers

docker rm -f $(docker ps -aq)  //Removes **all** containers.
docker rm (container id/name)
  • -f – force removal
  • -a – all containers
  • -q – show only IDs

Volumes (Host ↔ Container)

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

Creating Docker Images (Dockerfile)

Base images are always required, e.g.:

FROM nginx:latest
ADD . /usr/share/nginx/html

Then 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

Building an Image

docker build -t (name:version)
docker build -t website:latest .
  • -t --tag – tag

Node.js Application Example

FROM node:latest
WORKDIR /app
ADD . .
RUN npm install
CMD node index.js
  • WORKDIR /app sets the working directory (if it doesn't exist like in nginx)
  • RUN npm install installs all dependencies from package.json
  • CMD node index.js defines the command executed when the container starts

Node listens on port 3000 by default, so run with:

docker run -p 8080:3000 image-name

.dockerignore

Used to exclude unnecessary files from the image:

node_modules   //is created during 'npm install' command
.git
.env

Reduces image size and build time.


Layering & Caching

To improve image's actualization speed.

Problematic Dockerfile

ADD . .
RUN npm install

Any change triggers npm install again.

Optimized Dockerfile

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.


Reducing Image Size (Alpine Linux)

You use alpine linux, every base image should have supported tags where will be alpine.

  • Alpine significantly reduces image size.
docker pull node:lts-alpine

Lightweight Dockerfile

FROM node:lts-alpine
WORKDIR /app
ADD package*.json .
RUN npm install
ADD . .
CMD node index.js
  • lts - Long-Term-Support

Versioning & Tagging

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-alpine

Tagging Existing Images

docker tag user-service:latest user-service:1

Both tags reference the same image ID, enabling easy rollback.


Pushing Images to Docker Hub

  1. Create a repository on Docker Hub
  2. Rename image:
docker tag user-service:1 kr1s6/krzysztof-repo:1
  1. Login to Docker
  2. Push images:
docker push kr1s6/krzysztof-repo:1
docker push kr1s6/krzysztof-repo:2
docker push kr1s6/krzysztof-repo:latest

Inspecting Containers & Logs

docker inspect <container_id>
docker logs <container_id>
  • inspect – container metadata
  • logs – application output (e.g. console.log, HTTP requests)

Bash into Containers

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).

Accessing Container Shell

docker exec -it website bash
  • Enables creating/editing files inside the container
  • Changes are reflected on the host via the volume

About

Docker basic tutorial - amigoscode

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published