You are on page 1of 3

docker run <image-name>:<version | default : latest>

- pull image
- start image in same window(logs in the current window)
docker run -d <image-name>:<version | default : latest>
- pull image
- start image in detach mode (no logs in the current window)
docker run -p<host-port-number>:<container-port-number> <image-name>:<version |
default : latest>
-to bind(map) host machine port to container port
docker pull <image-name>:<version | default : latest>
- pull image to local host
docker ps
- list of all running container
docker stop <container id>
- stop that container
docker start <container id>
- start that container again
docker ps -a
- history of all container ran so far
docker images
-list of all available docker images locally
docker logs <container-id>
- check logs inside a container (running image logs)
docker logs <image-custom-name> (This is not image name)
- check logs inside a container (running image logs)
docker logs <container-id> | tail
- | tail will show latest part of the logs only
docker logs <container-id> -f
- -f will stream the real time logs in the console

docker run -d -p6000:6793 --name <any-custom-name> <original-image-name>


- start image <original-image-name>
- in detach mode
- bind host port 6000 to container port 6793
- set name of the image as <any-custom-name>
docker exec -it <container-id> or <container-custom-name> /bin/bash
- get the terminal of that container (get inside of the container through
terminal)
- using container-id or container-custom-name
- /bin/bash or /bin/sh either one will work (but need to check while running)

Note : use 'exit' command to get out from container terminal to host terminal

[FYI]docker run vs docker start


- docker run creates a new container with passing attributes like name
- docker start acctually start an existing container with container-id or
container-custom-name

docker network ls
- list of all available docker network
docker network create <custom-network-name>
- create a new docker network of our own
[FYI] Docker network help to communicate multiple container with each other

docker run -d -p27017:27017


-e MONGO_INITDB_ROOT_USERNAME=admin
-e MONGO_INITDB_ROOT_PASSWORD=password
--name <custom-container-name>
--network <custom-network-name> mongo
- this will run mongo in detach mode (-d)
- bind host machine port 27017 to container port 27017 (-p27017:27017)
- set couple of environment variable (-e) (usually get this info in docker
hub image desc)
- set the custom-name of the image (--name)
- link created container to network (--network or --net)

docker run -d -p8081:8081


-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password
-e ME_CONFIG_MONGODB_SERVER=<custom-container-name>
--network <custom-network-name>
--name <custom-container-name-of-mongodb> mongo-express
- this will run mongo-express in detach mode
- bind host machine port 8081 to container port 8081 (-p8081:8081)
- set couple of environment variable (-e) (usually get this info in docker
hub image desc)
- set the custom-name of the image (--name)
- link created container to network (--network or --net)

[FYI] instead of writing command multiple times we can use docker-compose

version:3
services:
mongodb: (this is <custom-container-name>)
image:mongo (acctual image name)
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- <custom-volume-name>:<directory-path-of-the-container>
mongo-express: (this is <custom-container-name>)
image: mongo-express (acctual image name)
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=<custom-container-name-of-mongo>
volumes:
<custom-volume-name>:
driver: local

[FYI] above contect saved in a .yaml file is equivalent with above two command.
This will automatically creates network and assign them into it,
so no need to create seperate network.

container can't persist data, once container stop / deleted all data removed, to
persist data we
use volumes to store container data into local file system.

in above .yaml file,


- <custom-volume-name> is the name that will be created in local host, and
- <custom-volume-name>:<directory-path-of-the-container> this copy data from
<directory-path-of-the-container>
and store it in <custom-volume-name> folder in local
docket-compose -f <.yaml file name> up
- run the .yaml file
docket-compose -f <.yaml file name> down
- stop all the container created by .yaml file and also delete the network
which get created bydefault

[FYI] Dockerfile sample example

FROM node:13-alpine
ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PWD=password
RUN mkdir -p /home/app [ this will create a folder app inside container]
COPY ./source /home/app [ this will copy everything from source folder in host
machine to app folder in container]
CMD ["node", "/home/app/server.js"]

- Dockerfile must be saved name as Dockerfile and it's a simple text file.
- FROM command is used for base layer of the image
- ENV is for setting environment variable to the container
- RUN is to run any linux command that will executed inside container
- COPY is used to copy files from host machine to container
- CMD is where we mentioned the entry point of the exceutions

docker build -t <custom-image-name>:<custom-version-name> <location-of-the-


Dockerfile>
- this will build an docker image with image name <custom-image-
name>:<custom-version-name>
- <location-of-the-Dockerfile> is only path where Dockerfile exists
- check 'docker images' command, new image will be there

Note : i. any changes made in Dockerfile need a re build of the image


ii. to re build an image old image must be deletd with 'docker rmi <image-
id>'
iii. in order to delete an image any container that is running that image /
previously ran that image must be deleted with 'docker rm <container-id>' command

[FYI] docker image name format is <registry-domain>/<image-name>:<image-tag>


for all image pulled from dockerhub are actually looks like
docker.io/library/<image-name>:<tag>
to push our custom images into private reporitory we need to remane our
image in above format

docker tag <local-image-name>:<tag> <registry-domain-url>/<new-image-name>:<tag>


- this will make a copy of our local image with this <registry-domain-
url>/<new-image-name>:<tag> format
- 'tag' command is used for renaming the image
docker push <image-name>:<image-tag>
- this will push our image to the registry
- here <image-name>:<image-tag> must be renamed in <registry-domain-
url>/<new-image-name>:<tag> format

You might also like