Learning Docker 001 ~Containers~

kojikanao profile image Koji (he/him) ・2 min read

I have used Docker, but just use for specific things and all images are made by others. So it's time to create my own image to understand Docker correctly.

Display Docker Info

$ docker info

Run docker

$ docker run -it --rm ubuntu bash

~/Desktop/docker » docker run -ti --rm ubuntu bash                                                               
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
5b7339215d1d: Pull complete
14ca88e9f672: Pull complete
a31c3b1caad4: Pull complete
b054a26005b7: Pull complete
Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
Status: Downloaded newer image for ubuntu:latest
root@7aae2cd83925:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

root@7aae2cd83925:/# uname -a
Linux 7aae2cd83925 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Show images

$ docker images

Run Container

$ ~/Desktop/docker » docker run -it ubuntu:latest bash                                                             
root@fa598f1d1951:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@fa598f1d1951:/# pwd
root@fa598f1d1951:/# cat /etc/lsb-release

Check Containers

$ docker ps -a --format=$FORMAT


Stop Container

root@d558c3197d2e:/# exit

$ docker kill container_name/id

Commit and tag

$ docker commit 82924883df95
$ docker tag 83cc840f9145b441be80be8fe45c094c162c56917ccf4f63ec38ea5b60e7ea4f ml_image
$ docker images

Attach & Detach

# Attach
$ docker attach container_name

# Detach
Control-p and Control-q

Clean up Containers

$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")



$ docker logs container_name

Expose port

$ docker run --name nc_test -it  -p 45678:45678 -p 45679:45679 bash
$ nc -lp 45678 | nc -lp 45679

Then open 2 sessions

# first session
$ nc localhost 45678
hello world

# second session
$ nc localhost 45679

You will see hello world in your second session.

Connect Containers

Open 2 sessions

# first
$ docker run --rm -it -p 1234:1234 ubuntu:14.04 bash
$ nc -lp 1234

# second
$ docker run --rm -it ubuntu:14.04 bash
$ nc ip_address 1234

Use private network

$ docker network create test
# first
$ docker run --rm -it --net=test --name server ubuntu:14.04 bash
$ nc -lp 1234

# second
$ docker run --rm -it --link server --net=test --name client ubuntu:14.04 bash
$ nc ip_address 1234

Volume(shared folder)

I created test.txt inside my docker folder.

$ docker run --rm -it -v /Users/koji.kanao/Desktop/docker:/shared-folder ubuntu bash

root@4340eb3f3de0:/# cd shared-folder/
root@4340eb3f3de0:/shared-folder# ls

Shared data between Containers

# first
$ docker run --rm -it -v /shared_folder ubuntu bash
root@4950f8cf8345:/# echo hello world > /shared_folder/test.txt

# second
$ docker run --rm -it --volumes-from flamboyant_hermann ubuntu bash
root@69669fa09156:/# ls shared_folder/


Editor guide