Skip to content

Instantly share code, notes, and snippets.

@phanatagama
Created March 11, 2025 07:54
Show Gist options
  • Select an option

  • Save phanatagama/a542b5265dd4a01157d8e4a374ab2292 to your computer and use it in GitHub Desktop.

Select an option

Save phanatagama/a542b5265dd4a01157d8e4a374ab2292 to your computer and use it in GitHub Desktop.
Getting Started with Docker

Getting Started with Docker

Quick tutorial for introduction to docker

  1. Create file with Dockerfile name in the root of your project (Example of Dockerfile is attached). Your directory structure should be like this:
└── your-app/
    ├── src/
    │   ├── component
    │   └── app.js
    ├── .env
    └── Dockerfile
  1. Create a docker image with the following command
  $ docker build -t image-name:v1 .
  1. Check the newly created docker image with the following command:
 $ docker images
  | REPOSITORY | IMAGETAG | IMAGE ID     | CREATED     | SIZE  |
  |------------|----------|--------------|-------------|-------|
  | image-name | v1       | f654b913b662 | 1 hours ago | 2.1GB |
  1. Run docker container with newly created docker image:
 $ docker run -d -p 80:8080 --name container-name image-name:v1
  • -d: detach container(running in background)
  • -p HOST_PORT:CONTAINER_PORT: publish/map container port 8080 into host port 80 (so you can access app via http://localhost:80 in the browser)
  1. Check the newly created container with the following command:
  # another command: docker ps --all or docker container ls
  $ docker ps
  | CONTAINER ID | IMAGE        | COMAND                  | CREATED     | STATUS        | PORTS                | NAMES          |
  |--------------|--------------|-------------------------|-------------|---------------|----------------------|----------------|
  | 8ae2617b5849 | image-name:v1 | "docker-entrypoint.s.." | 1 month ago | Up 10 seconds | 0.0.0.0:80->8080/tcp | container-name |
  1. Wait until container status is running (like "Up 1 seconds") then open http://localhost:80 in the browser
# pull nodejs image
FROM node:20.18.1-alpine
# put environment variables
ARG NODE_ENV=test
ENV NODE_ENV=$NODE_ENV
# create workdir
WORKDIR /app
# copy your project into container image
COPY . .
# install dependencies
RUN npm i --only=prod
# expose port
EXPOSE 8080
# run application
CMD [ "node", "src/app.js" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment