Skip to content

Instantly share code, notes, and snippets.

@santuman
Last active August 27, 2021 09:56
Show Gist options
  • Save santuman/8f7e1373d486a0409dd8b0cf1074e859 to your computer and use it in GitHub Desktop.
Save santuman/8f7e1373d486a0409dd8b0cf1074e859 to your computer and use it in GitHub Desktop.

Common Docker Command

Spin up a alpine container

$ docker run -it --name nodejs-alpine --rm alpine:3.10

--name -> giving custom name

-it -> interactive tty

--rm -> removes metadata after container is killed or exited

Running Continaer in background

$ docker run -it --detach --name nodejs-alpine --rm alpine:3.10

or,

$ docker run -dit --name nodejs-alpine --rm alpine:3.10

Killing all docker containers at once

$ docker kill $(docker ps -q)

Remove images

$ docker image rm <image-name>

or,

$ docker rmi node:latest --force

Removing all images

$ docker rmi $(docker images -aq)

Prune all stoped container (this will clear space)

$ docker container prune

Prune all images (this will clear space)

$ docker image prune

Searching Docker containers

$ docker search python

Nodejs Container

$ docker run -it node:14-stretch

This run a Node REPL by default but if you want to run bash then

$ docker run -it node:14-stretch bash
$ docker run -it node:14-stretch cat /etc/issue
$ docker run -it node:14-stretch node -v

Get latest Nodejs Container

$ docker run -it node

or,

$ docker run -it node:latest

Dockerfile

FROM node:14-stretch
CMD ["node","-e","conosle.log(\" Hi there \")"]
$ docker build . 
$ docker build --tag my-node-app .

or,

$ docker build -t my-node-app .

Running nodejs app through docker file

// index.js

const http = require("http");

http
  .createServer(function(request, response) {
    console.log("request received");
    response.end("omg hi", "utf-8");
  })
  .listen(3000);
console.log("server started");
// Dockerfile

FROM node:14-stretch
COPY index.js index.js
CMD ["node", "-e", "index.js"]
$ docker build -t my-node-app .
$ docker run --init --rm -p 3000:3000 my-node-app

--init -> tini package for listening Ctrl+C commnad

--rm -> remove data after contianer is killed

-p -> public (network sharing)

Pointing to a docker file

$ docker build -t my-node-app -f dev.Dockerfile 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment