Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Docker
  4. / Lessons
  5. / 1

Prev

Next

[A] Basic Docker Commands

1. Build an image

Terminal window
docker build -t <tag-image> .

2. List an images

Terminal window
docker images

3. Run a container

Terminal window
Port exposed by docker (Internal Container)
docker run -p 3000:3000 <taged-image-name>
Port exposed by windows (External OS)

4.1 List container (currently running)

Terminal window
docker ps

4.2 List container (all running and not running)

Terminal window
docker ps -a

5. Kill (currently running) Container

Terminal window
docker kill <container-id>

6.1 Remove specific container

Terminal window
docker rm <container-id>

6.2 Remove all stopped container

Terminal window
docker container prune

7. Working with mongo

Terminal window
docker run mongo //this will install dependencies
docker run -p 27017:27017 mongo

Ctrl+C to exit from container

[B] Volume

1. Create a volume

Terminal window
docker volume create <volume-database>

2. List volume

Terminal window
docker volume ls

3. Remove volume

Terminal window
docker volume rm <volume-database>

4. Assigning volume to database (mongo)

Terminal window
docker run -v <volume-database>:/data/db -p 27017:27017 mongo

/data/db => this is the default place where mongo stores its data in Linux OS

5. Run bash inside a container

Terminal window
docker exec -it <container-id> /bin/bash

[C] Network

1. Create a network

Terminal window
docker network create <my-custom-network>

2. List networks

Terminal window
docker network ls

3. Use network

Terminal window
docker run -p 3000:3000 --name <name(backend)> --network <my-custom-network> <image-tag>
docker run -v <volume-database>:/data/db --name <name(mongodb)> --network <my-custom-network> -p 27017:27017 mongo

Initial Dockerfile

FROM mhart/alpine-node:16.4.2
WORKDIR /usr/src/app
COPY package* .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

Multi stage Dockerfile

FROM mhart/alpine-node:16.4.2 AS base
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
EXPOSE 3000
FROM base AS development
COPY . .
CMD ["npm", "run", "dev"]
FROM base as production
COPY . .
RUN npm prune --production
CMD ["npm", "run", "start"]

Building the development image

Terminal window
docker build --target <development> -t <myapp:dev> .
docker run -e MONGO_URI=mongodb://localhost:27017/mydatabase -p 27017:27017 -v .:/usr/src/app <myapp:dev>

-v .:/usr/src/app => very important line during development -> it map the current Windows directory (.) to container’s Linux directory (/usr/src/app), for hot reloading

Building the production image

Terminal window
docker build --target <production> -t <myapp:prod> .
docker run -e MONGO_URL=mongodb://localhost:27017/mydatabase -p 27017:27017 <myapp:prod>

[BULK CODE]

Terminal window
#running db container
docker run -p 27017:27017 --volume volume1:/data/db --name mongodb --network network1 mongo
#creating the image
docker build --target development -t app:dev4 .
#running the container
docker run -v .:/usr/src/app --network network1 -p 3000:3000 app:dev3 //ru

Note: use -L flag to trigger nodemon to hot-reload files. i.e.; nodemon -L index.js

We can run the container in detached mode by using -d flag. i.e.; docker run -d <image-tag>. it does not hold the terminal for logging.

but, then we have to use docker log <container-id> to see the log of a particular container.

Prev

Next