0% found this document useful (0 votes)
18 views10 pages

Docker

Uploaded by

italimbd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Docker

Uploaded by

italimbd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Dockerfile:

FROM node:20

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 5000

CMD ["npm", "run", "dev"]

Bind Mount, Dev Container & Ts Node Dev


Cheat Sheet

Bind Mound Syntax For

For Git Bash

docker run -p 5000:5000 --name ts-container -w //app -v ts-docker-logs://app/logs -v


"//$(pwd)"://app/ -v //app/node_modules --rm ts-docker
docker run -p 5000:5000 --name ts-container -w //app -v ts-docker-logs://app/logs -v
"${PWD}://app" -v //app/node_modules --rm ts-docker

For CMD

docker run -p 5000:5000 --name ts-container -w //app -v ts-docker-logs://app/logs -v


"%cd%"://app/ -v //app/node_modules --rm ts-docker

ts-node-dev command for Docker Container

ts-node-dev --respawn --transpile-only --poll src/server.ts

1. First Open A .devcontainer folder in the root of the project


2. Inside the .devcontainer folder open a file named devcontainer.json and paste the following code
3. Change the json name, container names and file directories according to your project

"name": "ts-container",

"image": "node:20",

"workspaceFolder": "/app",

"mounts": [

// Bind mount for your local project

"source=/c/Projects/next-level/Docker/docker-with-typescipt-backend,target=/app,type=bind",

// Named volume for logs (similar to: -v ts-docker-logs://app/logs)

"source=ts-docker-logs,target=/app/logs,type=volume",
// Anonymous volume for node_modules (similar to: -v //app/node_modules)

"target=/app/node_modules,type=volume"

],

"runArgs": [

"--name",

"ts-container",

"-p",

"5000:5000",

"--rm" // Automatically remove the container after exiting VS Code

],

"postCreateCommand": "npm install"

Docker command Cheat sheet


https://find-saminravi99.notion.site/Docker-Cheat-Sheet-10dc48b8ac8c80b79f73ece2abfc6841

Docker Cheat Sheet


Getting Started with Docker
Check Docker Installation:

docker --version

1.

Basic Commands
Pull an Image from Docker Hub:

docker pull <image-name>

Example:

docker pull ubuntu


1.

List Available Images:

docker images

2.

Run a Container:

docker run -it <image-name>

Example:

docker run -it ubuntu

3.

Run a Container in Detached Mode:

docker run -d <image-name>

4.

Stop a Running Container:

docker stop <container-id>

5.

Start a Stopped Container:

docker start <container-id>

6.

Remove a Stopped Container:

docker rm <container-id>

7.
Remove an Image:

docker rmi <image-name>

8.

Managing Containers
List Running Containers:

docker ps

1.

List All Containers (including stopped):

docker ps -a

2.

View Container Logs:

docker logs <container-id>

3.

Execute a Command in a Running Container:

docker exec -it <container-id> <command>

Example:

docker exec -it <container-id> bash

4.

Inspect a Container:

docker inspect <container-id>

5.
Networking
List Docker Networks:

docker network ls

1.

Create a New Network:

docker network create <network-name>

2.

Connect a Container to a Network:

docker network connect <network-name> <container-id>

3.

Disconnect a Container from a Network:

docker network disconnect <network-name> <container-id>

4.

Volumes and Data Management


Create a Volume:

docker volume create <volume-name>

1.

List Volumes:

docker volume ls

2.

Remove a Volume:

docker volume rm <volume-name>


3.

Mount a Volume to a Container:

docker run -v <volume-name>:<container-path> <image-name>

4.

Dockerfile and Building Images


Create a Dockerfile: Basic structure:

FROM <base-image>

MAINTAINER <your-name>

COPY <source> <destination>

RUN <command>

CMD ["<executable>"]

1.

Build an Image from a Dockerfile:

docker build -t <image-name>:<tag> .

Example:

docker build -t myapp:latest .

2.

List Built Images:

docker images

3.

Advanced Commands
Tag an Image:

docker tag <image-id> <new-image-name>:<tag>


1.

Push an Image to Docker Hub:

docker push <image-name>:<tag>

2.

Save an Image to a Tar File:

docker save -o <path-to-output-file> <image-name>

3.

Load an Image from a Tar File:

docker load -i <path-to-input-file>

4.
5. Docker Compose:

Start Services:

docker-compose up

Stop Services:

docker-compose down

Scale Services:

docker-compose up --scale <service-name>=<number>

6.

View Running Docker Compose Services:


docker-compose ps

7.

Useful Docker Commands


Remove All Stopped Containers:

docker container prune

1.

Remove Unused Images:

docker image prune

2.

Remove All Unused Data (containers, networks, images):

docker system prune

3.

Get Docker System Information:

docker info

4.

You might also like