Docker handbook

=>Docker?

==============

Virtualization -?

  • Applications? << Run On >> VM? (VM can have same or different O/S)
  • VMs(Virtual Machines)? <<< Sit on >>> Hypervisor? (VM are mimic of physical server)
  • Hypervisor ? <<< Sit on >> O/S (i.e Host O/S)
  • Host O/S? <<< Sits on >>>? Physical Server


Docker Containerization:

  • Dockerfile <<< Used to Create >>> Docker Image?
  • Docker Image <<< Used to Create >>> Docker Container (container also have its own O/S - it might be the same as host OS type or the different type)
  • Docker Container? <<< Run on >>> Docker Engine (Container Contains Apps)
  • Docker Engine? <<< Sit on top of >>> O/S? (called Host O/S)
  • Host O/S? <<< Installed on >>>? Physical Server?


Containerization -

  • Where all container will be using same Physical server (ALWAYS Running)?
  • But all containers will have their own O/S ISOLATED to Host O/S - hence easy to manage.
  • Light weight - Because Physical Server & Host O/S are always Running hence reboot time is saved.


Docker image :? Read - only binary file used to create docker container

Docker Host :? Provides a runtime environment to run applications. Contains - images, container, network, storage.


Docker Client :? User interacts with Docker daemon using Client terminal. Docker client use Commands & API to interact with Deamon.?

Users use Shell to interact with Kernel in Unix & execute commands.


Docker Registry/hub :? To store docker images, users pull images from hub and run image to create a container.

- Public : Docker hub

-? Private : Within the enterprises



How to set up docker on Ubuntu?


sudo apt-get update


  • To update system before setting docker


sudo apt-get install \

????ca-certificates \

????curl \

????gnupg \

????lsb-release

  • Install packages to allow apt to use a repository over HTTPS


sudo mkdir -p /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

  • Add GPA key


echo \

??"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \

??$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


  • To set up the repository


sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

  • To install docker engine?


How to pull images from hub.docker.com and run on any machine?


docker pull <imageName>


  • To get image on your host system where you want to run that application
  • E.g.?

docker pull trainwithshubham/react-django-app



Create & Run container from docker image:


docker run -dp <appPort>:<HostSystemPort> <imageName:<version>


  • appPort - Application port that you have EXPOSE in Dockerfile
  • HostSystemPort - where you want to run application - this port must be open in firewall / Security group if EC2?
  • -d? -deamon runs in background
  • imageName - Name of the image and version e.g. 1.2, 1.2 etc, you can also use latest?
  • Example:
  • sudo docker run -dp 8001:8001 trainwithshubham/react-django-app:latest


docker run -dp 8005:8005 myfirstimage:latest


sudo dockedocker run -dp 8005:8005 myfirstimage:latest

r run -p 8001:8001 -d trainwithshubham/react-django-app:latesudo docker run -p 8001:8001 -d trainwithshubham/react-django-app:latest

docker search?

  • To search docker image from docker hub


Dockerfile - Used to create Docker image.


E.g.?


FROM python:3.9 ? ? ? ? #python + OS environment


WORKDIR /app # Location where code will be lies on container


COPY | ADD . .? ? ? ? # First dot - source & Second dot - destination


RUN pip install -r requirement.txt? ? # Application language specific, can have more RUN


EXPOSE 8000 # Port that will be exposed to outside

CMD ["python" , "manage.py", "runserver","0.0.0.0:8000"]???

#command will execute inside the container





docker build . -t djago-new-img


  • To create docker image from Dockerfile
  • . (dot) : Dockerfile path - in this case current directory
  • <Dockerfile directory path>? - not required if already on same location
  • Use -t to give name to image

?e.g. Docker build -t <imageName>


docker images?

  • To view the images

192:react_django_demo_app ribhu-divine$ docker images

REPOSITORY? ? ? ? ? TAG ? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? CREATED ? ? ? ? ? ? SIZE

djago-new-img ? ? ? latest? ? ? ? ? ? ? b0e0165cf28e? ? ? ? 17 hours ago? ? ? ? 962 MB

django-img? ? ? ? ? latest? ? ? ? ? ? ? c7baf6c56e3d? ? ? ? 18 hours ago? ? ? ? 962 MB

python? ? ? ? ? ? ? 3.9 ? ? ? ? ? ? ? ? e4bf78b64f77? ? ? ? 4 days ago? ? ? ? ? 915 MB



docker images -a?


  • To view all previously created old deleted images as well


docker run -d new-image:latest


  • To create container from docker image


docker run -dp 8001:8001 django-img:latest



  • -d :? to run in background
  • -p? : port mapping (e.g. localhost:8001)


E.g.?

192:react_django_demo_app ribhu-divine$ docker run -d -p 8001:8001 djago-new-img:latest

280b65179995f454435c660960cb9d1341c87eee699742471e78b28d1f42272a

192:react_django_demo_app ribhu-divine$?




docker ps | grep 662027fc68f1


  • To check if the container is running.
  • You can use container ID or name?


Rename docker image:


docker image tag 2dc9f7f1e25f myfirstimage


  • 2dc9f…? - is image id to myfirstimage?



Docker login:


192:devops_zero_to_hero ribhu-divine$ docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.

Username (komalanandpandey): komalanandpandey

Password:?

Login Succeeded

192:devops_zero_to_hero ribhu-divine$?



Push local image to hub.docker.io using below steps:


  • Sudo docker login

sudo docker login

Password:

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.

Username: komalanandpandey

Password:?

Login Succeeded



  • Tag your image


sudo docker tag local-image:tagname reponame:tag?


sudo docker tag myfirstimage:latest komalanandpandey/devops_zero_to_hero:latest


  • Push your image


sudo docker push reponame:tag




sudo docker push komalanandpandey/devops_zero_to_hero

The push refers to a repository [docker.io/komalanandpandey/devops_zero_to_hero]

f5f51a4f62ec: Pushed?

8e308c45972f: Pushed?

d8808771d980: Pushed?

4a7fc96599f2: Pushed?

13e67d691443: Pushed?

4876aa0a9ee9: Pushed?

0c7daf9a72c8: Pushed?

75ba02937496: Pushed?

288cf3a46e32: Pushed?

186da837555d: Pushed?

955c9335e041: Pushed?

8e079fee2186: Pushed?

latest: digest: sha256:439219d5d6fe2a53eb8bfc7ac1646fc62636dbd335b9a856415f46569524cf9e size: 2847




  • To see all container’s ids


docker ps -aq


vagrant@vagrant:~$ sudo docker ps -a

CONTAINER ID? ? ? ? IMAGE ? ? ? ? ? ? ? COMMAND? ? ? ? ? ? ? ? ? CREATED ? ? ? ? ? ? STATUS? ? ? ? ? ? ? ? ? ? ? ? ? ? PORTS ? ? ? ? ? ? ? NAMES

514221944c31? ? ? ? ubuntu? ? ? ? ? ? ? "/bin/bash"? ? ? ? ? ? ? 31 minutes ago? ? ? Exited (137) About a minute ago ? ? ? ? ? ? ? ? ? ? ? trusting_euler

60811136f045? ? ? ? ubuntu? ? ? ? ? ? ? "/bin/bash"? ? ? ? ? ? ? 37 minutes ago? ? ? Exited (0) 37 minutes ago ? ? ? ? ? ? ? ? ? ? ? ? ? ? dazzling_noyce

4720f43af6f0? ? ? ? ubuntu? ? ? ? ? ? ? "/bin/bash"? ? ? ? ? ? ? 41 minutes ago? ? ? Exited (0) 41 minutes ago ? ? ? ? ? ? ? ? ? ? ? ? ? ? optimistic_kalam

72455c25fe35? ? ? ? nginx ? ? ? ? ? ? ? "/docker-entrypoint.…" ? 10 hours ago? ? ? ? Exited (0) 5 hours ago? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? wizardly_northcutt

vagrant@vagrant:~$ sudo docker? ps -aq

514221944c31

60811136f045

4720f43af6f0

72455c25fe35


  • To stop all container at once

sudo docker container stop $(sudo docker ps -aq)


  • To remove all container once

sudo docker container rm $(sudo docker ps -aq)


  • To Inspect container?


sudo docker inspect <containerID>


  • To view container’s process id (PID


sudo docker container top <containerID>


  • To view containers memory uses stats


sudo docker container stats


e.g.

Sudo docker container stats

?

CONTAINER ID? ? ? ? NAME? ? ? ? ? ? ? ? CPU % ? ? ? ? ? ? ? MEM USAGE / LIMIT ? MEM % ? ? ? ? ? ? ? NET I/O ? ? ? ? ? ? BLOCK I/O ? ? ? ? ? PIDS

c321b3372177? ? ? ? youthful_brown? ? ? 0.00% ? ? ? ? ? ? ? 1.969MiB / 992MiB ? 0.20% ? ? ? ? ? ? ? 648B / 0B ? ? ? ? ? 0B / 16.4kB ? ? ? ? 3

908da2622820? ? ? ? loving_hoover ? ? ? 0.00% ? ? ? ? ? ? ? 1.969MiB / 992MiB ? 0.20% ? ? ? ? ? ? ? 648B / 0B ? ? ? ? ? 0B / 16.4kB ? ? ? ? 3

e6773b7f55be? ? ? ? naughty_goldstine ? 0.00% ? ? ? ? ? ? ? 1.953MiB / 992MiB ? 0.20% ? ? ? ? ? ? ? 828B / 0B ? ? ? ? ? 0B / 16.4kB ? ? ? ? 3



  • To view container logs


sudo docker container logs <containerID>


  • To rename container name


sudo docker container rename <containerID> <newName>


sudo docker container rename 80f0eace2f2e new-name


  • To inspect logs inside running container?


sudo docker container attach <containerID>


sudo docker container attach 809894r990


  • You can see all the input, output and error logs generated inside container
  • To keep container on Wait, Pause and Unpause


sudo docker container wait <containerID>


sudo docker container pause <containerID>


sudo docker container unpause <containerID>


vagrant@vagrant:~$ sudo docker container wait 26

^Z

[3]+? Stopped ? ? ? ? ? ? ? ? sudo docker container wait 26

vagrant@vagrant:~$ sudo docker container pause 59

59

vagrant@vagrant:~$ sudo docker ps

CONTAINER ID? ? ? ? IMAGE ? ? ? ? ? ? ? COMMAND? ? ? ? ? ? ? ? ? CREATED ? ? ? ? ? ? STATUS? ? ? ? ? ? ? ? ? PORTS? ? ? ? ? ? ? ? ? ? ? ? ? ? NAMES

2656611c90d5? ? ? ? nginx ? ? ? ? ? ? ? "/docker-entrypoint.…" ? 14 hours ago? ? ? ? Up 2 minutes? ? ? ? ? ? 80/tcp, 0.0.0.0:3601->8080/tcp ? stupefied_knuth

59b8a016e7d1? ? ? ? nginx ? ? ? ? ? ? ? "/docker-entrypoint.…" ? 14 hours ago? ? ? ? Up 2 minutes (Paused) ? 0.0.0.0:3600->80/tcp ? ? ? ? ? ? elastic_greider

vagrant@vagrant:~$ sudo docker container unpause 59

59

vagrant@vagrant:~$ sudo docker ps

CONTAINER ID? ? ? ? IMAGE ? ? ? ? ? ? ? COMMAND? ? ? ? ? ? ? ? ? CREATED ? ? ? ? ? ? STATUS? ? ? ? ? ? ? PORTS? ? ? ? ? ? ? ? ? ? ? ? ? ? NAMES

2656611c90d5? ? ? ? nginx ? ? ? ? ? ? ? "/docker-entrypoint.…" ? 14 hours ago? ? ? ? Up 2 minutes? ? ? ? 80/tcp, 0.0.0.0:3601->8080/tcp ? stupefied_knuth

59b8a016e7d1? ? ? ? nginx ? ? ? ? ? ? ? "/docker-entrypoint.…" ? 14 hours ago? ? ? ? Up 2 minutes? ? ? ? 0.0.0.0:3600->80/tcp ? ? ? ? ? ? elastic_greider

vagrant@vagrant:~$?



  • To find container port mapping


sudo docker container port <containerID>


E.g.

vagrant@vagrant:~$ sudo docker container port 2656611c90d5

8080/tcp -> 0.0.0.0:3601



  • To watch container logs live


Go inside container -


Sudo docker container exec -it <containderId>? /bin/bash


Use below command inside container:


watch ‘sudo docker container diff <containerID>’


Now open another terminal and add a folder or file in the container's /tmp/ folder, the logs will be generated on the first window.


  • To copy file/folder from localhost to container


sudo docker container cp <localhost file/directory> <containerId>:<containerFolder>


e.g.

sudo docker container cp testdir 2656611c90d5:/tmp/?

vagrant@vagrant:~$ sudo docker container exec -it 2656611c90d5 /bin/bash

root@2656611c90d5:/# cd /tmp/

root@2656611c90d5:/tmp# ls

root@2656611c90d5:/tmp# ls

testdir






  • To export / import container to image vise versa


Export container to tar file:


sudo docker container export <containerId>? -o filename.tar


E.g.

vagrant@vagrant:~$ sudo docker container export 2656611c90d5 -o nginx-exp.tar

vagrant@vagrant:~$ ls

chFile.txt? chtest.tar? chtest_dir? cut? date? echo? merge-result.txt? nginx-exp.tar ?tes.py? test.tar.gz? test.txt? test.txt.save? testdir


Convert tar to image:


sudo docker container image import <filename.tar> <image-file-name>


E.g.


vagrant@vagrant:~$ sudo docker image import nginx-exp.tar nginx-exp-image

?

sha256:acedc3fbb75a1d9b84cc70ccf0d26a9ef49c68ff3fc0aa46f03b3d1b760bfb5f


Now run container from image:


sudo docker run -it nginx-exp-image /bin/bash



  • To create image from running container


sudo docker container commit --author "Komala pandey" -m "This image was created while container was running" <containerID> <image–name>


E.g.?


vagrant@vagrant:~$ sudo docker container commit --author "Komala pandey" -m "This image was created while container was running" c08b9f2b6e44 my_running_container_img

sha256:e6cf6c6f46b667a151439a963fdb8a228ca13b5cadc535133bade2f48b898dc1

vagrant@vagrant:~$ sudo docker image ls

REPOSITORY ? ? ? ? ? ? ? ? TAG ? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? CREATED ? ? ? ? ? ? SIZE

my_running_container_img ? latest? ? ? ? ? ? ? e6cf6c6f46b6? ? ? ? 9 seconds ago ? ? ? 78.4MB

nginx-exp-image? ? ? ? ? ? latest? ? ? ? ? ? ? acedc3fbb75a? ? ? ? 17 minutes ago? ? ? 140MB

ubuntu ? ? ? ? ? ? ? ? ? ? latest? ? ? ? ? ? ? a8780b506fa4? ? ? ? 8 days ago? ? ? ? ? 77.8MB

nginx? ? ? ? ? ? ? ? ? ? ? latest? ? ? ? ? ? ? 76c69feac34e? ? ? ? 2 weeks ago ? ? ? ? 142MB



  • To get a particular column using format


vagrant@vagrant:~$ sudo docker image ls --format '{{.ID}}'

e6cf6c6f46b6

acedc3fbb75a

a8780b506fa4

76c69feac34e


  • Docker Copy v/s Add command


E.g.?

Dockerfile?


vagrant@vagrant:~$ cat Dockerfile?

FROM ubuntu:16.04

LABEL name="Komala Nand Pandey"

LABEL email="[email protected]"

ENV username="komal.pnd"

ENV password="123test"

RUN pwd > /tmp/1stpwd.txt

RUN cd /tmp/

RUN pwd >/tmp/2ndpwd.txt?

WORKDIR /tmp/

RUN pwd > /tmp/3rdpwd.txt

RUN mkdir -p /tmp/project

ADD test.tar.gz /tmp/project

RUN mkdir -p /tmp/project-copy

COPY test.tar.gz /tmp/project-copy


Both ADD & COPY directives used to transfer file/directories to docker image/container with below difference:


ADD directive -? Extracts all files from url and copies into target location. Do not have backward compatibility.


COPY directive - Do not extract files but keeps all files/directory to target location exactly as it is in source location. Copy have backward compatibility.?

The COPY directive is preferred for most cases. This is because the ADD directive provides additional functionality that should be used with caution and only when needed.



  • To map multiple port with a container


Dockerfile

FROM ubuntu:16.04

LABEL name="Komala Nand Pandey"

LABEL email="[email protected]"

ENV username="komal.pnd"

ENV password="123test"

WORKDIR /tmp/

RUN mkdir -p /tmp/project-copy

COPY test.tar.gz /tmp/project-copy

EXPOSE 22 8080

CMD ["sh"]


Build and run docker image:

sudo docker build -t test-ubuntu:27 .


sudo docker container run -P -itd test-ubuntu:27

Check ports in container:

vagrant@vagrant:~$ sudo docker ps

CONTAINER ID? ? ? ? IMAGE ? ? ? ? ? ? ? COMMAND ? ? ? ? ? ? CREATED ? ? ? ? ? ? STATUS? ? ? ? ? ? ? PORTS? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NAMES

43bfb7559f5f? ? ? ? test-ubuntu:27? ? ? "sh"? ? ? ? ? ? ? ? 10 seconds ago? ? ? Up 9 seconds? ? ? ? 0.0.0.0:32771->22/tcp, 0.0.0.0:32770->8080/tcp ? lucid_bassi


  • To Remove all the stopped container

sudo docker container prune


vagrant@vagrant:~/testdir$ sudo docker container prune

WARNING! This will remove all stopped containers.

Are you sure you want to continue? [y/N] y

Deleted Containers:

IT WILL SHOW REMOVE CONTAINER LIST HERE


  • To remove all dangling images?

sudo docker image prune


vagrant@vagrant:~/testdir$ sudo docker image prune

WARNING! This will remove all dangling images.

Are you sure you want to continue? [y/N] y

Deleted Images:

deleted: sha256:dbb857bada9f8d716ce05766751eb4bb57794050fe063f07963b96f0e5381e73


  • Dangling images are untagged Docker images that aren't used by a container or depended on by a descendant.


  • Difference between CMD and ENTRYPOINT


Using CMD in Dockerfile -?

vagrant@vagrant:~/testdir$ cat Dockerfile?

FROM ubuntu

CMD ["echo", "Hello World"]


Build image and run container by giving CLI arguments:

vagrant@vagrant:~/testdir$ sudo docker container run cmd-img2:02 test

  • It will not show any output since CMD do not accept arguments


Using ENTRYPOINT in Dockerfile:

vagrant@vagrant:~/testdir$ cat Dockerfile?

FROM ubuntu

ENTRYPOINT ["echo", "Hello World"]


Build image and run container by giving CLI arguments:

vagrant@vagrant:~/testdir$ sudo docker container run entrypoint-image:02 test

Hello World test

  • It returned command line arguments as well.


The ENTRYPOINT specifies commands that will always be executed when the container starts.

CMD will be overridden when running the container with alternative arguments.

?

For more detail:

https://www.bmc.com/blogs/docker-cmd-vs-entrypoint/

?


?==========Docker Volume =============>



  • To create new container by attaching existing volume


sudo docker container run -itd -v <existingVolumeID>:<mountLocation> <image>


e.g.

sudo docker container run -itd -v dce2f896a1e43935f6c007e848cc4f2d5a168c43187ca199919d6b00f3ab1ac8:/var/lib/mysql mysql


  • To see mount location inspect image e.g. sudo docker image inspect <imageID>


  • To create new container with user defined volume name

sudo docker container run -itd -v <yourVolumeName>:<mountLocation> <imageName>


e.g.

vagrant@vagrant:~$ sudo docker container run -itd -v customVolume:/var/lib/mysql mysql

0939a624cd080defe654d9645303ac2eb3e73afcad9507af4cd0986f0e275fb5

vagrant@vagrant:~$ sudo docker volume ls

DRIVER? ? ? ? ? ? ? VOLUME NAME

local ? ? ? ? ? ? ? c3f1ade0f00bf9e211e26789d82f694b49f0d05e629c890b9b6d5299f08f3ddd

local ? ? ? ? ? ? ? customVolume

??????????????dce2f896a1e43935f6c007e848cc4f2d5a168c43187ca199919d6b00f3ab1ac8


  • To create new volume?


sudo docker volume create <volumeName>


e.g.

vagrant@vagrant:~$ sudo docker volume create test_volume1

test_volume1

vagrant@vagrant:~$ sudo docker volume ls

DRIVER? ? ? ? ? ? ? VOLUME NAME

local ? ? ? ? ? ? ? 13e2a1c5e1e355cca5a7113af6d80477e50cb7986ad24ff2ebd546cb15641f42

local? ? ? ? ? ? ???test_volume1


  • To remove one or many volumes

sudo docker volume rm <volumeID>

sudo docker volume rm <VolumeId1> <VolumeId2> …<volumeIdN>



  • To remove all unused volumes

Sudo docker volume prune


  • To mount local directory to container directory during container creation

sudo docker ru -itd –name devtest –mount type=bind, source=”$(pwd)”/<sourceDirectoryInHostMachine>, target=<targetDirectoryInContainer>


vagrant@vagrant:~$ sudo docker run -d \

> ? -it \

> ? --name devtest \

> ? --mount type=bind,source="$(pwd)"/target,target=/app \

> ? nginx:latest




?==========Docker Networking =============>

Notes:?

  • Communication between containers does not require any host mapping if all container are within the same network (subnet)?
  • Customer network have DNS Enabled by default
  • 3 types networks - bridge & host and none
  • Bridge - port mapping required
  • Host -?
  • port mapping not required, it uses a host machine port.
  • Application will be isolated (except network)
  • Keep app that need isolation,?
  • Unlike bridge, only one host can be created.

None network used when you want to create a container without attaching any network on it.


  • To view docker network


sudo docker network ls


E.g.

vagrant@vagrant:~$ sudo docker network ls

NETWORK ID? ? ? ? ? NAME? ? ? ? ? ? ? ? DRIVER? ? ? ? ? ? ? SCOPE

920f5aaa8d23? ? ? ? bridge? ? ? ? ? ? ? bridge? ? ? ? ? ? ? local

a9b5b792cebf? ? ? ? host? ? ? ? ? ? ? ? host? ? ? ? ? ? ? ? local

329e4b6ea004? ? ? ? none? ? ? ? ? ? ? ? null? ? ? ? ? ? ? ? local



  • To inspect docker network


sudo docker network inspect bridge


  • This will show all bridge detail and the number of containers running as below:

"Containers": {

????????????"330cd0f899b6b2853893c51eeb8015d94ced811715f08fb83e76b0a7c31b22f2": {

????????????????"Name": "test-contnr",

????????????????"EndpointID": "cfcbb4931e24e3466698cffe9708a96964e00b505eb9ed6bbe3fb0f1a88d3202",

????????????????"MacAddress": "02:42:ac:11:00:02",

????????????????"IPv4Address": "172.17.0.2/16",

????????????????"IPv6Address": ""

????????????},

????????????"d97fade206ce406c7697816a9a0b966b57a43bdbbbf8ce073dfc82581ae3c275": {

????????????????"Name": "devtest",

????????????????"EndpointID": "00643baa07586cf110efbb77227ec95827561008f47bf79565002a037fc5d5df",

????????????????"MacAddress": "02:42:ac:11:00:03",

????????????????"IPv4Address": "172.17.0.3/16",

????????????????"IPv6Address": ""

????????????}



  • To create a new network


sudo docker network create test-network


  • To create new container with new created network


sudo docker container run -itd –name <containerName> –network <networkName> <image>


vagrant@vagrant:~$ sudo docker container run -itd --name cust-network-contnr --network test-network nginx

e03c55778ca555b9fd6107455c0160c65c3aa060a41bb77034aec9106f636039

vagrant@vagrant:~$ sudo docker container ls

CONTAINER ID? ? ? ? IMAGE ? ? ? ? ? ? ? COMMAND? ? ? ? ? ? ? ? ? CREATED ? ? ? ? ? ? STATUS? ? ? ? ? ? ? PORTS ? ? ? ? ? ? ? ? NAMES

e03c55778ca5? ? ? ? nginx ? ? ? ? ? ? ? "/docker-entrypoint.…" ? 9 seconds ago ? ? ? Up 8 seconds? ? ? ? 80/tcp? ? ? ? ? ? ? ? cust-network-contnr


  • To remove / prune network


sudo docker network rm 5ab1d372b690

  • Removes given network


sudo docker network prune

  • Removes all unused networks


  • To connect a container with multiple networks


sudo docker network connect <networkName> <containerName>


vagrant@vagrant:~$ sudo docker container inspect d6ae30f481c7 | grep NetworkID

????????????????????"NetworkID": "920f5aaa8d23791abdf6e9358c44f548b0251e21e2900e2e565d2cfa90e34fc2",

vagrant@vagrant:~$ sudo docker network connect host d6ae30f481c7

Error response from daemon: container cannot be disconnected from host network or connected to host network

vagrant@vagrant:~$ sudo docker network create test-network

730bcb7ac8449f5821d59de9cbcbfe3d945975facda633b14f4bd3d84bdc7313

vagrant@vagrant:~$ sudo docker network connect test-network d6ae30f481c7

vagrant@vagrant:~$ sudo docker container inspect d6ae30f481c7 | grep NetworkID

????????????????????"NetworkID": "920f5aaa8d23791abdf6e9358c44f548b0251e21e2900e2e565d2cfa90e34fc2",

????????????????????"NetworkID": "730bcb7ac8449f5821d59de9cbcbfe3d945975facda633b14f4bd3d84bdc7313",


  • To disconnect container from network


sudo docker network disconnect <networkID> <containerID>


E.g.

vagrant@vagrant:~$ sudo docker network disconnect test-network d6ae30f481c7

vagrant@vagrant:~$ sudo docker container inspect d6ae30f481c7 | grep NetworkID

????????????????????"NetworkID": "920f5aaa8d23791abdf6e9358c44f548b0251e21e2900e2e565d2cfa90e34fc2",



  • To run container with network?

sudo docker container run -itd --network=bridge --name my-nginx-container nginx:latest







?==========Docker Compose =============>


  • To create docker-compose.yaml file


ubuntu@ip-172-31-15-55:~/dc-demo$ cat docker-compose.yaml?

version: "3"

services:

??webapp:

????image: nginx

????ports:

??????- "8000:80"


  • To create services using docker-compose


sudo docker compose up -d


sudo docker compose -f docker-compose.yaml up -d


  • It will create & run nginx container?
  • It will also create networks - sudo docker network ls to check.
  • Use -f if filename is other than docker-compose.yaml


  • To remove services using docker-compose


sudo docker compose down


  • To remove services using docker-compose


sudo docker-compose create


  • It will create container(s) & network but container will not be in running state.
  • To start stopped container using docker-compose start


udo docker-compose start


e.g.


ubuntu@ip-172-31-15-55:~/dc-demo$ sudo docker-compose start

[+] Running 2/2

?? Container dc-demo-webapp1-1? Started ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1.0s

?? Container dc-demo-webapp2-1? Started?


  • To start stopped container using docker-compose start


sudo docker-compose stop


e.g.

ubuntu@ip-172-31-15-55:~/dc-demo$ sudo docker-compose stop

[+] Running 2/2

?? Container dc-demo-webapp2-1? Stopped ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.2s

?? Container dc-demo-webapp1-1? Stopped?


  • To remove containers using docker-compose start


sudo docker-compose rm



  • To pause / unpause using docker-compose pause / unpause


sudo docker-compose pause or unpause



  • To view container logs live


sudo docker-compose logs -f


Docker-compose ps ? V/S docker ps


E.g.


ubuntu@ip-172-31-15-55:~/dc-demo$ sudo docker-compose ps

NAME? ? ? ? ? ? ? ? COMMAND? ? ? ? ? ? ? ? ? SERVICE ? ? ? ? ? ? STATUS? ? ? ? ? ? ? PORTS

dc-demo-webapp1-1 ? "/docker-entrypoint.…" ? webapp1 ? ? ? ? ? ? running ? ? ? ? ? ? 0.0.0.0:8000->80/tcp, :::8000->80/tcp

dc-demo-webapp2-1 ? "/docker-entrypoint.…" ? webapp2 ? ? ? ? ? ? running ? ? ? ? ? ? 0.0.0.0:8002->80/tcp, :::8002->80/tcp


ubuntu@ip-172-31-15-55:~/dc-demo$ sudo docker ps

CONTAINER ID ? IMAGE ? ? COMMAND? ? ? ? ? ? ? ? ? CREATED? ? ? ? ? ? ? STATUS? ? ? ? ? PORTS ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NAMES

130ae049f2d5 ? nginx ? ? "/docker-entrypoint.…" ? About a minute ago ? Up 14 seconds ? 0.0.0.0:8000->80/tcp, :::8000->80/tcp ? dc-demo-webapp1-1

4ae73724f4de ? nginx ? ? "/docker-entrypoint.…" ? About a minute ago ? Up 14 seconds ? 0.0.0.0:8002->80/tcp, :::8002->80/tcp ? dc-demo-webapp2-1


  • To view port id of the containers


sudo docker-compose top


E.g.

ubuntu@ip-172-31-15-55:~/dc-demo$ sudo docker-compose top

dc-demo-webapp1-1

UID? ? ? ? PID? ? PPID ? C? ? STIME ? TTY ? TIME ? ? ? CMD

root ? ? ? 5569 ? 5535 ? 0? ? Nov15 ? ? ? ? 00:00:00 ? nginx: master process nginx -g daemon off;???

systemd+ ? 5683 ? 5569 ? 0? ? Nov15 ? ? ? ? 00:00:00 ? nginx: worker process????????????????????????

?

dc-demo-webapp2-1

UID? ? ? ? PID? ? PPID ? C? ? STIME ? TTY ? TIME ? ? ? CMD

root ? ? ? 5551 ? 5509 ? 0? ? Nov15 ? ? ? ? 00:00:00 ? nginx: master process nginx -g daemon off;???

systemd+ ? 5671 ? 5551 ? 0? ? Nov15 ? ? ? ? 00:00:00 ? nginx: worker process????????????

Hello, Komala Nand Pandey We are providing Bulk mailing and hosting services like web hosting, VPS hosting and dedicated server with IPv4 Subnets to our world wide clients on best rates across the industry. Let's connect to discuss further

回复

要查看或添加评论,请登录

Komala Nand Pandey的更多文章

  • Linux commands - A Handbook

    Linux commands - A Handbook

    =>Here are some handy linux commands used in daily basis ==============Linux Happy Learning================== To go…

  • How to set up ubuntu on mac os?

    How to set up ubuntu on mac os?

    Hello friends! Do you want to use Ubuntu OS on mac o/s? or you noticed some of the ubuntu commands do not work on mac…

社区洞察

其他会员也浏览了