Skip to content

Instantly share code, notes, and snippets.

@vvardhanz
Created May 9, 2018 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvardhanz/69f3fde109507b0c67e42f9574fd114b to your computer and use it in GitHub Desktop.
Save vvardhanz/69f3fde109507b0c67e42f9574fd114b to your computer and use it in GitHub Desktop.
docker notes
12/9/2017
cmd: docker exec -it <container name>
Will allow you to execute something in a running container without effecting the process that is started by the container when it got instantiated.
—> If you want to execute other things without attaching to the the container . for example if you want to connect to bash instance in container.
you can do all these with docker exec.
15/9/2017
-P (capital P) : Its says that any port that are exposed to my container make them available to the host operating system on random port ranging from 32768 to 65000.
16/9/2017
Virtual Machine :
Virtual machine is an emulation of the
—> It is highly impossible to track/control the static ip address allocated to he containers/dockers.
Ip address can only be see when the container/docker up running.
Because ip address is configured dynamically upon start up.
Docker Commands
9/23/2017
Root privileged user will run all of the commands in the docker file.
Note:
When you use a non-privileged user to use/run commands in the docker file. It will cause problem. It will throw an error called “permission denied”.
EX: USER user (in docker file , denotes to use User as user for execute further commands with user permissions.
RUN vs CMD
Run command is basically used to execute or run something as a part of the build process.
Run command is used to execute at build time and output/result of it is included as part of the base image.
It creates a layer that goes into creating docker base image.
CMD is execute as a part of instantiation of a container. The result of it not a part of the base image.
CMD is not a part of build process. Its is used to execute a specific command but it is not executed during build. It is basically initial command. It sets up a command when the container is instantiated using the base image. So it is really initial or default command.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
—> Order of execution in Docker file matters. Its linear from top to button.
—> Always use -y after yum to tell its ok to run command or else build will fail. Docker expect user input so give -y after yum.
ex: RUN yum install -y net-tools wget
####################################################################
ENV:
ENV executes an parameter based format.(Key , value) .
This will be a system wide env variable.
ENTRYPOINT:
—> CMD command applies to the container that are instantiated with the base image when we choose not to run any application. I can change that container behavior based on the application i passed to it when i instantiate the container from the base image.
So, CMD will only will run for that command when no other command is passed to the container instantiated.
—> Entrypoint will display msg from every container you run . Every time my container. instantiated.
By indicating entry point, if u start apache or ssh it will still run the command you wanted to run no matter what. Entry point will force it run command every time you instantiate container.
NOTE:
Docker doesn’t associate a port/default port when you use -P if there is no port exposed by the container. In oder for the docker to associate we need to expose a port first.
VOLUMES :
To create a mount outside the container unified file system to put some file in it. It subsequently exposes what ever u put in there for the underline host.
It also helpful for mounting host file to container.
Docker Import Commands / faced issues
This is to run the docker container with the services up . When using the centos7 , systemd .
docker run --privileged --name=webtest -i -t -e "container=docker" -v /sys/fs/cgroup:/sys/fs/cgroup centos7:baseweb /usr/sbin/init
Anti pattern :
Static IPs and using IP’s for talking to containers is an anti pattern. Do your best to avoid it.
Docker DNS:
Docker daemon has a built-in DNS server that containers use by default.
Docker defaults the hostname to the container’s name, but you can also set aliases.
17/9/2017
exposing our container with port redirects.
1. Update your system as appropriate for the distribution you are using. Use the instructions in the videos OR on the Docker site to add the DOCKER repository for installing the latest copy of Docker for your distribution and version.
[user@linuxacademy:~] sudo yum update (or upgrade)
[user@linuxacademy:~] cat /etc/yum.repos.d/docker.repo
[docker-repo]
name=Docker Repo
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
2. Using the appropriate package management commands, install the 'docker' package. Once installed, enable the service so that it starts upon reboot. Additionally, start the 'docker' service and verify it is running.
[user@linuxacademy:~] sudo yum -y install docker-engine
(Output) Package downloads and installs here
CentOS 7.x SOLUTION
[user@linuxacademy:~] sudo systemctl enable docker
[user@linuxacademy:~] sudo systemctl start docker
[user@linuxacademy:~] ps aux | grep docker
(Output)
root 5802 0.0 0.3 346424 12936 ? Ssl 21:02 0:00 /usr/bin/docker -d --selinux-enabled
(Output)
3. Enable the non-root users to run 'docker' commands on the system. Create the appropriate group, add the users you want to have access to 'docker' commands, restart the 'docker' service and verify that non-root users can run basic 'docker' commands.
[user@linuxacademy:~] sudo groupadd docker
[user@linuxacademy:~] vim /etc/group
(Output)...
Find the line that looks like: docker:x:1001:
Add the 'user' user to the end of that line (after the :)
(Output)...
[user@linuxacademy:~] sudo service docker restart (OR sudo systemctl restart docker)
[user@linuxacademy:~] docker images
(Output)
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
(Output)
4. Once 'docker' is installed and non-root users can run commands, use the appropriate 'docker' commands and options to download the latest available image in the public repository for Ubuntu. Once downloaded and installed, verify the image appears in the local base image list.
[user@linuxacademy:~] docker pull ubuntu:latest
(Output) NOTE: Your output may differ if image has been updated
latest: Pulling from docker.io/ubuntu
6071b4945dcf: Pull complete
5bff21ba5409: Pull complete
e5855facec0b: Pull complete
8251da35e7a7: Pull complete
Digest: sha256:1572e29178048ad9ab72e78edd4decc91a3d8a8dea0ca39817efc7cf2d86c6d7
Status: Downloaded newer image for docker.io/ubuntu:latest
(Output)...
[user@linuxacademy:~] docker images
(Output) NOTE: Again, your output may differ slightly
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/ubuntu latest 8251da35e7a7 11 days ago 188.3 MB
(Output)...
5. Start a container based upon the Ubuntu base image downloaded in Step #4. When you start the container, start it connected to the current terminal, in interactive mode, starting the bash shell for you to connect to. You may exit the container at that time.[user@linuxacademy:~] docker run -it docker.io/ubuntu:latest /bin/bash
(Output) NOTE: Your output will differ based on the container ID assigned
root@7aaf3de3ed4f:/# exit
XXXXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsdfasdfhfdxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxkjhdfkljsdhflkjhaslkdfjhaksjdfhklasdhflkasjhdfxxxxxxxxxxxxxxxx
1. Using the CentOS base image download, start a container based on that image. Be sure that container starts connected to the current terminal in interactive mode and runs the bash command so you are logged in to the command prompt on the container once it boots.
[user@linuxacademy:~]$ docker run -it centos:centos6 /bin/bash
(Output)
[root@b237d65fd197 /]#
(Output)
2. Once you are sitting at a command prompt on the running container, execute the update command (installing all updates for the container OS).
[root@b237d65fd197 /]# yum -y update (or yum -y upgrade)
(Output)
List of packages needing update and being applied here
(Output)
3. Now that updates are complete, install the Apache Web Server. Once installed, make sure the web server service will start and verify that the container is listening on port 80 (install other software if needed to do so).
[root@b237d65fd197 /]# yum install httpd
(Output)
Installed:
httpd.x86_64 0:2.2.15-45.el6.centos
Dependency Installed:
apr.x86_64 0:1.3.9-5.el6_2 apr-util.x86_64 0:1.3.9-3.el6_0.1 apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
httpd-tools.x86_64 0:2.2.15-45.el6.centos mailcap.noarch 0:2.1.31-2.el6 redhat-logos.noarch 0:60.0.14-12.el6.centos
Complete!
(Output)
[root@b237d65fd197 /]# yum install telnet
(Output)
Like output above, telnet is installed
(Output)
[root@b237d65fd197 /]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2 for ServerName
[ OK ]
[root@b237d65fd197 /]# telnet localhost 80
(Output)
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
(Output)
4. Exit the container. Once the container is stopped, execute the appropriate command to list all stopped containers and locate the name and ID of the container you just exited. Make a note of the name and ID.
[root@b237d65fd197 /]# exit
[user@linuxacademy:~]$ docker ps -a
(Output)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b237d65fd197 centos:6 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago furious_rosalind
(Output)
5. Using the name or ID of the container, commit the changes you made within it to a new base image called "newcentos:withapache" and verify that it shows when you list the images on your system.
[user@linuxacademy:~]$ docker commit b237d65fd197 newcentos:withapache
(Output)
18bd1fc4d60fa29ff9591f46b86ea0ad7652214d81b4e26343723e81fdbffd8a
(Output)
[user@linuxacademy:~]$ docker images
(Output)
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
newcentos withapache 18bd1fc4d60f 4 seconds ago 480.6 MB
centos 6 a005304e4e74 9 weeks ago 203.1 MB
centos centos6 a005304e4e74 9 weeks ago 203.1 MB
(Output)
xxxxxxxxxxxxxxxxxxxxxxxxasdlkfjalkfjldskjfl;kasjdl;akjd;lfjkldsjxxxxxxxxxxxxxxxxxxxxxxxxxsadkfjalskdfjalkdsfjl;askdjfacxxxxxxxxxxxxxxxxxxxxxxxxxxx
1. Create a container from the 'centos:6' base image on your system. This container does not need to be name but should be run in daemon mode, interactive and connected to the current terminal. Finally, it should start the bash shell on start up.
[user@linuxacademy ~]$ docker run -itd docker.io/centos:6 /bin/bash
99f87625ff34a5a25af8edd7e95ad9b6a4bc70db63c2ac6e0850dd4cfae58cef
[user@linuxacademy ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99f87625ff34 docker.io/centos:6 "/bin/bash" 3 seconds ago Up 2 seconds elegant_bohr
[user@linuxacademy ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99f87625ff34 docker.io/centos:6 "/bin/bash" 5 seconds ago Up 5 seconds elegant_bohr
2. Using the appropriate Docker inspection command, find the IP address and name for the running container. Once you have the IP, ping the IP to be sure it is running. Finally, attach to the running container so you are logged into the shell.
[user@linuxacademy ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99f87625ff34 docker.io/centos:6 "/bin/bash" 7 minutes ago Up 7 minutes elegant_bohr
[user@linuxacademy]$ docker inspect elegant_bohr | grep IP
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null
[user@linuxacademy ~]$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.096 ms
^C
--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.069/0.082/0.096/0.016 ms
[user@linuxacademy ~]$ docker attach elegant_bohr
[root@99f87625ff34 /]#
3. From within the container, install the Open-SSH server and make sure the service is running. From another terminal, try to log into the container over SSH by IP and note the result.
[root@99f87625ff34 /]# yum install openssh-server
... Lots of Output Here
Installed:
openssh-server.x86_64 0:5.3p1-112.el6_7
Dependency Installed:
fipscheck.x86_64 0:1.2.0-7.el6 fipscheck-lib.x86_64 0:1.2.0-7.el6 openssh.x86_64 0:5.3p1-112.el6_7
tcp_wrappers-libs.x86_64 0:7.6-57.el6
Complete!
[root@99f87625ff34 /]# service sshd start
Generating SSH2 RSA host key: [ OK ]
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd:
(Different Terminal)
[user@linuxacademy ~]$ ssh test@172.17.0.2
ssh: connect to host 172.17.0.2 port 22: Connection refused
4. Exit and stop the container. Remove the container from the list of previously run containers once you obtain the name from the appropriate Docker command.
[user@linuxacademy ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99f87625ff34 docker.io/centos:6 "/bin/bash" About an hour ago Exited (0) 4 seconds ago elegant_bohr
8ef073d5c7f4 docker.io/centos:6 "/bin/bash" About an hour ago Exited (0) About an hour ago silly_poincare
[user@linuxacademy ~]$ docker rm elegant_bohr
elegant_bohr
[user@linuxacademy ~]$ docker rm silly_poincare
silly_poincare
[user@linuxacademy ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/ubuntu latest 91e54dfb1179 4 days ago 188.3 MB
docker.io/centos 6 a005304e4e74 9 weeks ago 203.1 MB
[user@linuxacademy ~]$
5. Create another container, name this container 'test_ssh'. When creating the container, it should be run in interactive mode and attached to the current terminal running the bash shell. Finally, expose port 22 on the container to port 8022 on the host system. Once logged in, install the Open-SSH server and make sure the service is running. Find the IP address of the container and note it.
[user@linuxacademy ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[user@linuxacademy ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99f87625ff34 docker.io/centos:6 "/bin/bash" About an hour ago Exited (0) 4 seconds ago elegant_bohr
8ef073d5c7f4 docker.io/centos:6 "/bin/bash" About an hour ago Exited (0) About an hour ago silly_poincare
[user@linuxacademy ~]$ docker rm elegant_bohr
elegant_bohr
[user@linuxacademy ~]$ docker rm silly_poincare
silly_poincare
[user@linuxacademy ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/ubuntu latest 91e54dfb1179 4 days ago 188.3 MB
docker.io/centos 6 a005304e4e74 9 weeks ago 203.1 MB
[user@linuxacademy ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/ubuntu latest 91e54dfb1179 4 days ago 188.3 MB
docker.io/centos 6 a005304e4e74 9 weeks ago 203.1 MB
[user@linuxacademy ~]$ docker run -it --name="test_ssh" -p 8022:22 docker.io/centos:6 /bin/bash
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
[root@de1119934beb /]# yum install openssh-server
Loaded plugins: fastestmirror
Setting up Install Process
base | 3.7 kB 00:00
base/primary_db | 4.6 MB 00:07
extras | 3.4 kB 00:00
extras/primary_db | 27 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 1.3 MB 00:01
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:5.3p1-112.el6_7 will be installed
--> Processing Dependency: openssh = 5.3p1-112.el6_7 for package: openssh-server-5.3p1-112.el6_7.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-5.3p1-112.el6_7.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-5.3p1-112.el6_7.x86_64
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.2.0-7.el6 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.2.0-7.el6.x86_64
---> Package openssh.x86_64 0:5.3p1-112.el6_7 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-57.el6 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.2.0-7.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================
Installing:
openssh-server x86_64 5.3p1-112.el6_7 updates 324 k
Installing for dependencies:
fipscheck x86_64 1.2.0-7.el6 base 14 k
fipscheck-lib x86_64 1.2.0-7.el6 base 8.3 k
openssh x86_64 5.3p1-112.el6_7 updates 274 k
tcp_wrappers-libs x86_64 7.6-57.el6 base 62 k
Transaction Summary
===============================================================================================================================
Install 5 Package(s)
Total download size: 682 k
Installed size: 1.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): fipscheck-1.2.0-7.el6.x86_64.rpm | 14 kB 00:00
(2/5): fipscheck-lib-1.2.0-7.el6.x86_64.rpm | 8.3 kB 00:00
(3/5): openssh-5.3p1-112.el6_7.x86_64.rpm | 274 kB 00:00
(4/5): openssh-server-5.3p1-112.el6_7.x86_64.rpm | 324 kB 00:00
(5/5): tcp_wrappers-libs-7.6-57.el6.x86_64.rpm | 62 kB 00:00
-------------------------------------------------------------------------------------------------------------------------------
Total 306 kB/s | 682 kB 00:02
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-6.el6.centos.12.2.x86_64 (@CentOS/$releasever)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : fipscheck-1.2.0-7.el6.x86_64 1/5
Installing : fipscheck-lib-1.2.0-7.el6.x86_64 2/5
Installing : openssh-5.3p1-112.el6_7.x86_64 3/5
Installing : tcp_wrappers-libs-7.6-57.el6.x86_64 4/5
Installing : openssh-server-5.3p1-112.el6_7.x86_64 5/5
Verifying : tcp_wrappers-libs-7.6-57.el6.x86_64 1/5
Verifying : fipscheck-lib-1.2.0-7.el6.x86_64 2/5
Verifying : fipscheck-1.2.0-7.el6.x86_64 3/5
Verifying : openssh-5.3p1-112.el6_7.x86_64 4/5
Verifying : openssh-server-5.3p1-112.el6_7.x86_64 5/5
Installed:
openssh-server.x86_64 0:5.3p1-112.el6_7
Dependency Installed:
fipscheck.x86_64 0:1.2.0-7.el6 fipscheck-lib.x86_64 0:1.2.0-7.el6 openssh.x86_64 0:5.3p1-112.el6_7
tcp_wrappers-libs.x86_64 0:7.6-57.el6
Complete!
[root@de1119934beb /]# service sshd start
Generating SSH2 RSA host key: [ OK ]
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd: [ OK ]
[root@de1119934beb /]# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3944 errors:0 dropped:0 overruns:0 frame:0
TX packets:2104 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7151212 (6.8 MiB) TX bytes:116622 (113.8 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
[root@de1119934beb /]#
6. Install the 'sudo' utility. Add a user called 'test' and set a password for that user. Add the 'test' user to the 'sudoers' file. From another terminal window, attempt to log into the container via SSH on port 8022 as the 'test' user and confirm access.
[root@de1119934beb /]# yum install sudo
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
* base: repos.dfw.quadranet.com
* extras: centos.mirror.lstn.net
* updates: mirror.steadfast.net
Resolving Dependencies
--> Running transaction check
---> Package sudo.x86_64 0:1.8.6p3-20.el6_7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================
Installing:
sudo x86_64 1.8.6p3-20.el6_7 updates 707 k
Transaction Summary
===============================================================================================================================
Install 1 Package(s)
Total download size: 707 k
Installed size: 2.4 M
Is this ok [y/N]: y
Downloading Packages:
sudo-1.8.6p3-20.el6_7.x86_64.rpm | 707 kB 00:02
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : sudo-1.8.6p3-20.el6_7.x86_64 1/1
Verifying : sudo-1.8.6p3-20.el6_7.x86_64 1/1
Installed:
sudo.x86_64 0:1.8.6p3-20.el6_7
Complete!
[root@de1119934beb /]# adduser test
[root@de1119934beb /]# passwd test
Changing password for user test.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[root@de1119934beb /]#
(Other Terminal Window)
[user@linuxacademy ~]$ ssh test@172.17.0.3
The authenticity of host '172.17.0.3 (172.17.0.3)' can't be established.
RSA key fingerprint is e8:5e:28:d8:64:1f:81:3a:d9:4c:2c:0c:8e:a1:27:b7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.3' (RSA) to the list of known hosts.
test@172.17.0.3's password:
[test@de1119934beb ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment