-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker.commands.txt
904 lines (596 loc) · 34.4 KB
/
docker.commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
>>>>>>>>>>>> Docker Commands <<<<<<<<<<<
---------------------------------------------------------------------------------------------------
Searching:
vim, Dockerfile, Docker Laravel,
working php in container,copy files in vlume, create file in volume
application, create Dockerfile, docker-compose,
Install Docker in WSL 2 without Docker Desktop, Dockekr Networking, Docker Commands,
-------------------------------------------------------------------------------------------------
Following are the commands list:
https://www.edureka.co/blog/docker-commands/
docker –version
docker volumn
docker pull
docker run //Create + Start + Atatch a container (3 works at a time)
docker ps
docker ps -a
docker exec
docker stop
docker kill
docker commit
docker login
docker push
docker images
docker rm
docker rmi
docker build
docker search //Searching images form docker hub
docker create //Create a container
docker
docker container
docker logs
docker start
docker network
docker info
service docker status
service docker start OR systemctl start docker.service
docker create volume
docker volume ls
docker volume ls <containerName>
docker cp <source> <destination>
---------------------------------------------------------
//Details:
1. docker –version
This command is used to get the currently installed version of docker
Ex: docker --version
2. docker pull
Usage: docker pull <image name>
This command is used to pull images from the docker repository(hub.docker.com)
Ex: docker pull ubuntu
3. docker run
Usage: docker run -it -d <image name>
This command is used to create a container from an image
Ex: docker run -it -d ubuntu
4. docker ps
This command is used to list the running containers
Ex: docker ps
5. docker ps -a
This command is used to show all the running and exited containers
Ex: docker ps -a
6. docker exec
Usage: docker exec -it <container id> bash
This command is used to access the running container
Ex: docker exec -it fe6e370a1c9c bash
7. docker stop
Usage: docker stop <container id>
This command stops a running container
Ex: docker stop fe6e370a1c9c
8. docker kill
Usage: docker kill <container id>
Ex: docker kill fe6e370a1c9c
This command kills the container by stopping its execution immediately.
The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives
the container time to shutdown gracefully, in situations when it is taking too much
time for getting the container to stop, one can opt to kill it
9. docker commit
Usage: docker commit <conatainer id> <username/imagename>
This command creates a new image of an edited container on the local system
Ex: docker commit fe6e370a1c9c hshar/ubuntunew
10. docker login
This command is used to login to the docker hub repository
Ex: docker login
11. docker push
Usage: docker push <username/image name>
This command is used to push an image to the docker hub repository
Ex: docker push hshar/ubuntunew
12. docker images
This command lists all the locally stored docker images
Ex: docker images
13. docker rm
Usage: docker rm <container id>
This command is used to delete a stopped container
Ex: docker rm 2b86a0703d4f
14. docker rmi
Usage: docker rmi <image-id>
This command is used to delete an image from local storage
Ex: docker rmi 2b86a0703d4f
15. docker build
Usage: docker build <path to docker file>
This command is used to build an image from a specified docker file
Ex: docker build.
16. docker search
Ex: docker search php
16. docker create
Ex: $ docker create --name my-container-name -it ubuntu bash
.: docker network ls
.: docker volumn ls
------------------------------------------------------------------
** Linking Two Containers:
docker run -it --name container1 -d ubuntu //After running it get ID 2x33kdd3k3
docker run -it --name container2 --link container1 -d ubuntu //After running it get ID 1x73keej3u
docker exec -it 1x73keej3u bash
cat/etc/hosts
-------------------------------------------------------------------
** Remove Running Containers:
docker rm -f (docker ps -a -q)
-------------------------------------------------------------------
-------------------------------------------------------------------
Example: <Create new container name "my-app">
docker create --name my-container-name -it ubuntu bash //Create New Container: (my-app)
docker container ls //Container list
docker container ls -a //List of all containers
docker start <container-name> //Container-name: my-app
docker attach <container-name> //Enter into container (my-app)
apt-get update //In ubuntu conatainer update..
apt-get install iputls-ping
ping 0.0.0.0
ping google.com
exitm //Exit form container
docker logs my-app //My Container name
-----------------------------------------------------------
-----------------------------------------------------------
Example 01: From Scratch (Docker Basics)
sudo dnf install docker-ce //Installing of docker
docker info
service docker status
service docker start OR systemctl start docker.service
docker version
-----------------------------------------------------------
Example 02:
docker --help |less //Readable list formate
//Docker Parent Modules:
config, container, image, network, node, plugin, secret, service, stack, swarm, system, volume
//Child Modules:
attach, build, commit, cp, create, diff, events
Ex: docker container ls, docker image ls, docker network ls
docker container run ubuntu cat /etc/os-release //ubuntu->imageName:ContainerOpenedThenClosed
//cat-information
docker container ls, docker container ls -a
docker container run ubuntu sleep 30
docker container ls //docker ps -> old command
docker container run nginx
docker container ls
docker container ls -a
docker container rm <container_id> //To remove container
docker container rm <container_id> <container_id> //To remove two containers
-----------------------------------------------------------
Example 03:
docker container stop <container_id>
docker container run -d ubuntu sleep 30 //Running on background, Not showing on screen
docker container run -it ubuntu /bin/bash //Going into container (ex. in ubuntu container)
ubuntu_id]# hostname
ubuntu_id]# cat /etc/os-release //os-release -> information
ubuntu_id]# apt-get install ----- //Install any software as apache etc.
ubuntu_id]# Ctrl+D Or exit //Out of container_id and container closed
ubuntu_id]# Ctrl+pq //Out of container but container is running
docker container rm $(docker container ls -aq) //To remove All containers
docker exec -u 0 -it 8662ea2fa000 /bin/sh // root user going Into running container
-----------------------------------------------------------
Example:
Hosting php project
1) index.php 2)Dockerfile
index.php:
vim index.php //Create file in vim editor
<?php
echo "Hello, this is my first project";
?>
:wq //Save & Exit from vim editor
Dockerfile:
vim Dockerfile //Creaating Dockerfile file
FROM php //In vim editor
COPY ./index.php ./
EXPOSE 80
CMD ["php"."-S"."0.0.0.0:80"]
:wq //Exit & save
//Running container command
docker run --name php-server -p=3030:80 image/php
cat /root/Dockerfile //To read Dockerfile file from root
------------------------------------------------
------------------------------------------------
//Example: nginx
docker container run -itd nginx //This is command line
vim docker-compose.yml //By docker-compose.yml file
version: '3'
services:
webapp1:
image: nginx
ports:
- "8000:80"
:wq //Exit and save
docker-compose up -d
docker container ls
docker network ls //Checking network
docker valumn ls
//SomeAreAutomaticallyStartedNowStopThem
docker compose down
//Now Creating Other Container
version: '3'
services:
webapp1:
image: nginx
ports:
- "8000:80"
webapp2:
image: nginx
ports:
- "8001:80"
:wq
docker compose up
^C
//Now Some Changing
version: '3'
services:
webapp1:
image: nginx
ports:
- "8000:80"
webapp2:
image: nginx
ports:
- "8002:80"
:wq
docker container ls
docker container ls -a
docker compose up -d //Only Container 2 recreated
docker container ls -a
docker container ls
clear
docker compose --help // It searches .yml file and do all (all file with .yml)
docker-compose -f docker-compose2.yml up -d //Not check all files, Only checks docker-compose2.yml file
docker-compose -f docker-compose2.yml down -d
//Now
cat docker-compose2.yml //Copy all contents form this file
//docker-compose.yml OR docker-compose.json //yml and json files have same working, Choose any one form these
------------------------------------------------
------------------------------------------------
Example: Wordpress (Example On docker compose file)
docker container run --name some-mysql -e MYSQL_ROOT_PASSWORD=mypassword -d mysql:5.7
docker container inspect 7111 //Checking ip : database tier id 172.17.0.2
docker container run --name some-wordpress -e WORDPRESS_DB_HOST=172.17.0.2:3306 -e WORDPRESS_DB_PASSWORD=mypassword -d wordpress //Standing two tiers sql & web
docker container inspect 2606
vim docker-compose.yml // For database & wordpress
version: '3.3'
services:
db:
images: mysql:5.7 //For mysql
volumn:
-db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_PASSWORD=wordpress
wordpress:
depends_on:
- db
image: wordpress:latest //For web
ports:
- "8000:80"
restart:always
environment:
-WORDPRESS_DB_HOST=db:3306
-WORDPRESS_DB_USER=wordpress
-WORDPRESS_DB_PASSWORD=wordpress
-WORDPRESS_DB_NAME=wordpress
volumes:
db_data:{}
:wq //Save and exit
cd /
cd wordpress
wordpress]# ls
wordpress]# docker-compose up ^C
wordpress]# docker container ls
wordpress]# docker volumn ls
wordpress]# docker network ls
wordpress]# docker-compose up -d
wordpress]# docker container ls
wordpress]# docker network ls
wordpress]# docker volumn ls
wordpress]# cat docker-compose.yml
wordpress]# docker-compose down //It will delete all but not delete volumns
wordpress]# docker volumn ls
wordpress]# docker-compose down --volume //It will delete volumns also
------------------------------------------------
------------------------------------------------
ls /root //Listing all file from root
ls -a /root
cat /root/Dockerfile //To read content of Dockerfile file from root
------------------------------------------------
------------------------------------------------
*** Some Practices in docker ***
docker image ls
docker pull php:7.1-cli
docker image ls
docker pull php:7.2-cli
docker image ls
docker pull php:7.4-rc-cli
docker image ls
php -v
docker run php:7.1-cli php -v
docker ps
docker ps -a
docker rm container_id
docker ps -a
docker run --rm php:7.1-cli php -v
dockeer ps -a
docker run --rm php:7.1-cli php -v
docker run --rm php:7.4-rc-cli php -v
vim script.php
//Writing some script in php in vim editor
<?php
echo "Hello";
?>
php script.php //Running php script
Hello //Output
cd /usr/src/myapp && php -v
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.1-cli php script.php
Hello //output
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.2-cli php script.php
Hello //Output
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.4-rc-cli php script.php
Hello //Output
//Depricated php
vim script.php
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.1-cli php script.php
string(11) *NONEXISTENT" //Output
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.2-cli php script.php
Warning //Output
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
//Copying files into docker container (into tomcat)
cat index.html //Displaying content from file
docker cp index.html <container_id> //Tomcat Id
docker exec -it <container_id> /bin/bash
container_id > tomcate cd /tmp
container_id > ls -l
------------------------------------------------------------------------------------
<-------------- vim Editor (Start) --------------------->
****** vim Editor *****
vim //Open vim editor
i //Write into file
:wq! //Save and Exit
:!q //Close vim editor
vim filename.txt //Open file in vim
:help //Helping
:q //Exiting Vim
:q! //To exit from vim without saving
:wq //To quit and save changes
x //Text editing-> deletion
//Text editing-> Insertion
i -> This key is used to put the cursor before the current position.
a -> This key is used to put the cursor after the current position.
o -> This key is used to put the cursor below the line.
O -> This key is used to puts the cursor above the line.
2w or 4$ //Count:
dw //delete the word
d2w //To delete 2 words
d$ //delete the line
//Undo and Redo:
u OR ctr + r
r //Replace
//Change Operator:
ce
c$
//Cursor location
:gg OR :G OR :ctrl+g
//Here is a list of some motions
w - until the start of the next word, EXCLUDING its first character.
e - to the end of the current word, INCLUDING the last character.
$ - to the end of the line, INCLUDING the last character.
* k -> move up ^
* j -> move down k
* h -> move right < h l >
* l -> move left j
//Search
:/word Or :n Or :N
//Search and Replace
:s/searchword/replaceword/ Or :s/searchword/replaceword/g OR :s/searchword/replaceword/gc Or
:%s/searchword/replaceword/gc
//Vim configuration
vim ~/.vimrc Or set number Or syntax on Or colorscheme COLOR_SCHEME_NAME
<----------------------- X (End of vim Editor) -------------------------->
-------------------------------------------------------------------------------
----------------------------------------------------
-----------> Working php in container End <---------
>>>> DocKer Files <<<<<<<<<<<
------------------------------------------
>>>>>> Files copy from container to local <<<<<<<<
-->
ImageName: nginx
Nginx Container:
docker run -itd --name nginx -v /src:/usr/share/nginx/html -p 8080:80 nginx
-->
Syntax:
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
Ex: //Copy all files into volume ( local or /src )
docker cp nginx:/usr/share/nginx/html /src //First nginx(containerName)
---------------------------------------------------
>>>>> Files Copy from local to container <<<<<<<<<
Syntax:
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Ex:
docker cp /src nginx:/usr/share/nginx/html //First nginx(containerName)
--------------------------------------------------
>>>> Create new file in volume ( local or /src )
touch /src/about.php //Create New file ( in volume or local)
or
vi /src/about.php //Create and write in a file (in volume or local )
--------------------------------------------------
# Install Docker in WSL 2 without Docker Desktop:
- Download the Linux kernel update package
Download the latest package:
WSL2 Linux kernel update package for x64 machines
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
wsl --set-default-version 2
wsl --install -d <DistroName>
wsl --list --online
wsl --status
wsl --update rollback
Step 1: Uninstall Docker Desktop
Since we’re installing Docker directly inside of WSL 2 you won’t need Docker Desktop installed to make this work.
Step 2: Install Docker / Docker Compose v2 in WSL 2
Here’s the condensed version for Ubuntu and Debian based distros:
# Install Docker, you can ignore the warning from Docker about using WSL
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
# Add your user to the Docker group
sudo usermod -aG docker $USER
# Install Docker Compose v2
sudo apt-get update && sudo apt-get install docker-compose-plugin
# Sanity check that both tools were installed successfully
docker --version
docker compose version
# Using Ubuntu 22.04 or Debian 10 / 11? You need to do 1 extra step for iptables
# compatibility, you'll want to choose option (1) from the prompt to use iptables-legacy.
sudo update-alternatives --config iptables
sudo service docker start
docker ps, docker ps, docker pull nginx etc. (Now, Docker is working)
If you’re using a different distro it’s very similar. Docker has docs for the steps above. These steps came from official Linux installation guides:
https://docs.docker.com/engine/install/ubuntu/#install-using-the-convenience-script
https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user
https://docs.docker.com/compose/install/linux/
Step 3: Ensure the Docker Service Runs in WSL 2
At the time of this post systemd doesn’t run inside of WSL 2 by default. We won’t need it.
All you have to do is drop this into your ~/.profile, .zprofile or equivalent file:
if grep -q "microsoft" /proc/version > /dev/null 2>&1; then
if service docker status 2>&1 | grep -q "is not running"; then
wsl.exe --distribution "${WSL_DISTRO_NAME}" --user root \
--exec /usr/sbin/service docker start > /dev/null 2>&1
fi
fi
The idea here is the Docker service will get started if it’s not already running. The first time this runs it’ll hang your terminal for a few seconds. However, even if you close your terminal the next time you open it, it will open instantly since Docker will already be running. Closing your terminal will not stop Docker, only rebooting Windows or fully shutting down WSL will.
You can run ps aux | grep docker to see the Docker daemon running:
root 8899 ... /usr/bin/dockerd -p /var/run/docker.pid
root 8908 ... containerd --config /var/run/docker/containerd/containerd.toml --log-level info
After opening a new terminal you should now be able to run docker run hello-world successfully without sudo. By the way, I have a bunch of open source web app examples on GitHub if you want to explore more in depth examples of using Docker.
---------------- X --------------------
#
Docker Networking
Learn about Docker network types, how containers communicate, common networking operations, and more.
In this page, you’ll learn everything you need to know about Docker Networking
Docker Networking
What is Docker Networking?
Docker Network Types
How Containers Communicate with Each Other
How Containers Communicate with the Outside World
Common Operations
Docker Networking with Multiple Hosts
Creating a New Network Driver Plugin
Summary
What is Docker Networking?
For Docker containers to communicate with each other and the outside world via the host machine, there has to be a layer of networking involved. Docker supports different types of networks, each fit for certain use cases.
For example, building an application which runs on a single Docker container will have a different network setup as compared to a web application with a cluster with database, application and load balancers which span multiple containers that need to communicate with each other. Additionally, clients from the outside world will need to access the web application container.
See Docker Documentation: Network Containers ›
Docker Default Networking (docker0)
When Docker is installed, a default bridge network named docker0 is created. Each new Docker container is automatically attached to this network, unless a custom network is specified.
Besides docker0 , two other networks get created automatically by Docker: host (no isolation between host and containers on this network, to the outside world they are on the same network) and none (attached containers run on container-specific network stack).
See Docker Documentation: Default Networks ›
Docker Network Types
Docker comes with network drivers geared towards different use cases. The most common network types being: bridge, overlay, and macvlan.
Bridge Networks
Bridge networking is the most common network type. It is limited to containers within a single host running the Docker engine. Bridge networks are easy to create, manage and troubleshoot.
For the containers on bridge network to communicate or be reachable from the outside world, port mapping needs to be configured. As an example, consider you can have a Docker container running a web service on port 80 . Because this container is attached to the bridge network on a private subnet, a port on the host system like 8000 needs to be mapped to port 80 on the container for outside traffic to reach the web service.
To create a bridge network named my-bridge-net , pass the argument bridge to the -d (driver) parameter as shown below:
$ docker network create -d bridge my-bridge-net
See Docker Documentation: Bridge Networks ›
Overlay Networks
An overlay network uses software virtualization to create additional layers of network abstraction running on top of a physical network. In Docker, an overlay network driver is used for multi-host network communication. This driver utilizes Virtual Extensible LAN (VXLAN) technology which provide portability between cloud, on-premise and virtual environments. VXLAN solves common portability limitations by extending layer 2 subnets across layer 3 network boundaries, hence containers can run on foreign IP subnets.
To create an overlay network named my-overlay-net, you’ll also need the --subnet parameter to specify the network block that Docker will use to assign IP addresses to the containers:
$ docker network create -d overlay --subnet=192.168.10.0/24 my-overlay-net
See Docker Documentation: An overlay network without swarm mode ›
Macvlan Networks
The macvlan driver is used to connect Docker containers directly to the host network interfaces through layer 2 segmentation. No use of port mapping or network address translation (NAT) is needed and containers can be assigned a public IP address which is accessible from the outside world. Latency in macvlan networks is low since packets are routed directly from Docker host network interface controller (NIC) to the containers.
Note that macvlan has to be configured per host, and has support for physical NIC, sub-interface, network bonded interfaces and even teamed interfaces. Traffic is explicitly filtered by the host kernel modules for isolation and security. To create a macvlan network named macvlan-net, you’ll need to provide a --gateway parameter to specify the IP address of the gateway for the subnet, and a -o parameter to set driver specific options. In this example, the parent interface is set to eth0 interface on the host:
$ docker network create -d macvlan \
--subnet=192.168.40.0/24 \
--gateway=192.168.40.1 \
-o parent=eth0 my-macvlan-net
See Docker Documentation: Get started with Macvlan network driver ›
How Containers Communicate with Each Other
Different networks provide different communication patterns (for example by IP address only, or by container name) between containers depending on network type and whether it’s a Docker default or a user-defined network.
Container discovery on docker0 network (DNS resolution)
Docker will assign a name and hostname to each container created on the default docker0 network, unless a different name/hostname is specified by the user. Docker then keeps a mapping of each name/hostname against the container’s IP address. This mapping allows pinging each container by name as opposed to IP address.
Furthermore, consider the following example which starts a Docker container with a custom name, hostname and DNS server:
$ docker run --name test-container -it \
--hostname=test-con.example.com \
--dns=8.8.8.8 \
ubuntu /bin/bash
In this example, processes running inside test-container, when confronted with a hostname not in /etc/hosts, will connect to address 8.8.8.8 on port 53 expecting a DNS service.
See Docker Documentation: Embedded DNS server in user-defined networks ›
Directly linking containers
It is possible to directly link one container to another using the --link option when starting a container. This allow containers to discover each other and securely transfer information about one container to another container. However, Docker has deprecated this feature and recommends creating user-defined networks instead.
As an example, imagine you have a mydb container running a database service. We can then create an application container named myweb and directly link it to mydb:
$ docker run --name myweb --link mydb:mydb -d -P myapp python app.py
For further reading see Docker Documentation: Legacy container links ›
How Containers Communicate with the Outside World
There are different ways in which Docker containers can communicate with the outside world, as detailed below.
Exposing Ports and Forwarding Traffic
In most cases, Docker networks use subnets without access from the outside world. To allow requests from the Internet to reach the container, you’ll need to map container ports to ports on the container’s host. For example, a request to hostname:8000 will be forwarded to whatever service is running inside the container on port 80, if a mapping from host port 8000 to container port 80 to was previously defined.
See Docker Documentation: Exposing and publishing ports ›
Containers Connected to Multiple Networks
Fine-grained network policies for connectivity and isolation can be achieved by joining containers to multiple networks. By default each container will be attached to a single network. More networks can be attached to a container by creating it first with docker create (instead of docker run) and then running the command docker network connect. For example:
$ docker network create net1 # creates bridge network name net1
$ docker network create net2 # creates bridge network name net2
$ docker create -it --net net1 --name cont1 busybox sh # creates container named cont1 attached to network net1
$ docker network connect net2 cont1 # further attaches container cont1 to network net2
The container is now connected to two distinct networks simultaneously.
See Docker Documentation: User-defined networks ›
How IPv6 Works on Docker
By default, Docker configures the container networks for IPv4 only. To enable IPv4/IPv6 dual stack the --ipv6 flag needs to be applied when starting the Docker daemon. Then the docker0 bridge will get an IPv6 link-local address fe80::1. To assign globally routable IPv6 addresses to your containers, use the flag --fixed-cidr-v6 followed by ipv6 address.
See Docker Documentation: IPv6 with Docker ›
Common Operations
Some common operations with Docker networking include:
Inspect a network: To see a specific network’s configuration details like subnet information, network name, IPAM driver, network ID, network driver, or connected containers, use the docker network inspect command.
List all networks: Run docker network ls to display all networks (along with their type and scope) present on the current host.
Create a new network: To create a new network, use the docker network create command and specify if it’s of type bridge (default), overlay or macvlan.
Run or connect a container to a specific network: Note first of all, the network must exist already on the host. Either specify the network at container creation/startup time (docker create or docker run) with the --net option; or attach an existing container by using the docker network connect command. For example:
docker network connect my-network my-container
Disconnect a container from a network: The container must be running to disconnect it from the network using the docker network disconnect command.
Remove an existing network: A network can only be removed using the command docker network rm if there are no containers attached to it. When a network is removed, the associated bridge will be removed as well.
Docker Networking with Multiple Hosts
When working with multi-host, there is a need to use higher-level Docker orchestration tools to ease management of networking between a cluster of machines. Popular orchestration tools today include Docker Swarm, Kubernetes, and Apache Mesos.
Docker Swarm
Docker Swarm is a Docker Inc. native tool used to orchestrate Docker containers. It enables you to manage a cluster of hosts as a single resource pool.
Docker Swarm makes use of overlay networks for inter-host communication. The swarm manager service is responsible for automatically assigning IP addresses to the containers.
For service discovery, each service in the swarm gets assigned a unique DNS name. Additionally, Docker Swarm has an embedded DNS server. You can query every container running in the swarm through this embedded DNS server.
See Docker Documentation: Manage swarm service networks ›
Kubernetes
Kubernetes Guide is a system used for automating deployment, scaling, and management of containerized applications, either on a single host or across a cluster of hosts.
Kubernetes approaches networking in a different way as compared to Docker, using native concepts like services and pods. Each pod has an IP address and no linking of pods is required, neither do you need to explicitly map container ports to host ports. There are DNS-based service discovery plugins which can be used for service discovery.
Apache Mesos
Apache Mesos is an open-source project used to manage a cluster of containers, providing efficient resource sharing and isolation across distributed applications.
Mesos uses IP address management (IPAM) server and client to manage containers networking. The role of the IPAM server is to assign IP addresses on demand while the IPAM client acts as a bridge between a network isolator module and the IPAM server. A network isolator module is a lightweight module that’s loaded into the Mesos agent. It looks at scheduler task requirements and uses IPAM and network isolator services to provide IP addresses to the containers.
Mesos-dns is a DNS-based service discovery for Mesos. It allows applications and services running on Mesos to find each other through the DNS service.
Creating a New Network Driver Plugin
Docker plugins are out-of-process extensions which add capabilities to the Docker Engine. The Docker engine network plugins API allows for support of a wide range of networking technologies to be realized. Once a networking plugin has been developed and installed, they behave just like the built-in bridge, overlay and macvlan network drivers.
See Docker Documentation: Engine network driver plugins ›
Summary
Docker offers a mature networking model. There are three common Docker network types – bridge networks, used within a single host, overlay networks, for multi-host communication, and macvlan networks which are used to connect Docker containers directly to host network interfaces.
In this page we explained how Docker containers discover and communicate with each other and how they communicate with the outside world. We showed how to perform common operations such as inspecting a network, creating a new network and disconnecting a container from a network. Finally, we briefly reviewed how docker networking works in the context of common orchestration platforms – Docker Swarm, Kubernetes Guide and Apache Mesos.
------------------ X -----------------------------------------
***Docker Import & Export ***
docker export -o [ContainerID] [tarfilename.tar]
docker image import [tarfilename.tar] [newimagename]
docker images
***Docker save & load***
docker save <imageNameWithVersion> -o <tarfileName.tar>
docker load -i <tarfileName.tar>
***Docker Network***
docker network create <networkName>
docker network rm <networkName>
docker network ls
docker network inspect <networkName>
docker network connect <networkName> <containerIdorName>
docker network disconnect <networkName> <containerNameOrId>
docker service create --network frontend --network backend
docker network create --driver=bridge network1 --subnet=172.19.0.0/24
docker network create --driver=bridge network2 --subnet=172.19.1.0/24
***Docker Volume***
docker volume create <volumeName>
docker volume rm <volumeName>
docker volume ls
docker volume inspect <volumeName>
***Docker Commands***
docker run --name db --network mynetwork -p 8000:80 -d nginx
docker run --name myserver --network mynetwork --link mydb -p 8000:80 -v "$PWD"/src:/var/www/html -d server01
---------------------------------------------------------