dockerref.txt 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # How to install and use docker on Ubuntu 16.04
  2. Ref.: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-
  3. ubuntu-16-04
  4. Docker is an application that makes it simple and easy to run application processes in a
  5. container, which are like virtual machines, only more portable, more resource-friendly,
  6. and more dependent on the host operating system. For a detailed introduction to the
  7. different components of a Docker container, check out "The Docker Ecosystem: An Introdu-
  8. -ction to Common Components".
  9. Step 1: Installing Docker (from official docker repository)
  10. First, in order to ensure the downloads are valid, add the GPG key for the official
  11. docker repository to your system:
  12. $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  13. Add the docker repository to APT sources:
  14. $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu
  15. $(lsb_release -cs) stable"
  16. Next, update the package database with the docker packages from the newly added repo:
  17. $ sudo apt update
  18. Make sure you are about to install from the Docker repo instead of the default
  19. Ubuntu 16.04 repo:
  20. $ apt-cache policy docker-ce
  21. Note that docker-ce is not installed, but the candidate for installation is from the
  22. docker repository for Ubuntu 16.04 (xenial).
  23. Finally, install docker:
  24. $ sudo apt install -y docker-ce
  25. Docker should now be installed, the daemon started, and the process enabled to start on
  26. boot. Check that it's running:
  27. $ sudo systemctl status docker
  28. Step 2: Executing the Docker command without sudo (optional)
  29. By default, running the docker command requires root privileges - that is, you have to
  30. prefix the command with sudo. It can also be run by a user in the docker group, which is
  31. automatically created during the installation of docker.
  32. If you want to avoid typing sudo whenever you run the docker command, add your username
  33. to the docker group:
  34. $ sudo usermod -aG docker ${USER}
  35. To apply the new group membership, you can log out of the server and back in, or you can
  36. type the following:
  37. $ su - ${USER}
  38. You will be prompted to enter your user's password to continue. Afterwords, you can
  39. confirm that your user is now added to the docker group by typing:
  40. $ id -nG
  41. If you need to add a user to the docker group that you're not logged in as, declare that
  42. username explicity using:
  43. $ sudo usermod -aG docker username
  44. Note: The rest of this article assumes you are running the docker command as a user in
  45. the docker user group. If you choose not to, please prepend the commands with sudo.
  46. Step 3: Using the docker command
  47. With docker installed and working, now's the time to become familiar with the command
  48. line utility. Using docker consists of passing it a chain of options and commands
  49. followed by arguments. The syntax takes this form:
  50. $ docker [option] [command] [arguments]
  51. To view all available subcommands, type:
  52. $ docker
  53. To view the switches available to a specific command, type:
  54. $ docker docker-subcommand --help
  55. To view system-wide information about docker, use:
  56. $ docker info
  57. Step 4: Working with docker images
  58. Docker containers are run from docker images. By default, it pulls these images from
  59. docker hub, a docker registry managed by Docker.
  60. To check whether you can access and download images from docker hub, type:
  61. $ docker run hello-world
  62. Output:
  63. ...
  64. Hello from Docker!
  65. This message shows that your installation appears to be working correctly.
  66. ...
  67. You can search for images available on docker hub by using the docker command with the
  68. search subcommand. For example, to search for the Ubuntu image, type:
  69. $ docker search ubutnu
  70. In the offical column, OK indicates an image built and supported by the company behind
  71. the project. Once you've identified the image that you would like to use, you can down-
  72. -load it to your computer using the pull subcommand. Try this with the ubuntu image like
  73. so:
  74. $ docker pull ubuntu
  75. After an image has been downloaded, you may then run a container using the downloaded
  76. image with the run subcommand. If an image has not been downloaded when docker is
  77. executed with the run subcommand, the Docker client will first download the image, then
  78. run a container using it:
  79. $ docker run ubuntu
  80. To see the images that have been downloaded to your computer type:
  81. $ docker images
  82. Step 5: Running a Docker container
  83. The hello-world container you ran in the previous step is an example of container that
  84. runs and exits after emitting a text message. Containers can be much more useful than
  85. that, and they can be interactive. After all, they are similar to virtual machines, only
  86. more resource-friendly.
  87. As an example, let's run a container using the latest image of Ubuntu. The combination
  88. of the -i and -t switches gives you interactive shell access into the container:
  89. $ docker run -it ubuntu
  90. Note: The default behavior for the run command is to start a new container. Once you run
  91. the preceding the command, you will open up the shell interface of a second Ubuntu
  92. container.
  93. Your command prompt should change to reflect the fact that you're now working inside the
  94. container and should take this form:
  95. outpur:
  96. root@9b0db8a30ad1:/#
  97. Note: 9b0db8a30ad1 is the container id. You'll need that container id later to identify
  98. the container when you want to remove it.
  99. Now you can run any command inside the container. For example, let's update the package
  100. database inside the container. You don't need to prefix any command with sudo, because
  101. you're operating inside the container as the root user:
  102. root@9b0db8a30ad1:/# apt update
  103. Then install any application in it. Let's install Node.js:
  104. root@9b0db8a30ad1:/# apt install -y nodejs
  105. This installs Node.js in the container from the offical Ubuntu repository. When the
  106. installation finishes, verfiy that Node.js is installed:
  107. root@9b0db8a30ad1:/# node -v
  108. You'll see the version number displayed in your terminal:
  109. output:
  110. v8.10.0
  111. Any changes you make inside the container only apply to that container. To exit the
  112. container, type exit at the prompt.
  113. Step 6: Managing Docker containers
  114. After using Docker for a while, you'll have many active (running) and inactive container
  115. on your computer. To view the active ones, use:
  116. $ docker ps
  117. To view all containers - active and inactive - run docker ps with the -a switch:
  118. $ docker ps -a
  119. To view the latest container you created, pass it the -l switch:
  120. $ docker ps -l
  121. To start a stopped container, use docker start, followed by the container ID or the
  122. container's name. Let's start the Ubuntu-based container with the ID of 9b0db8a30adl:
  123. $ docker start 9b0db8a30adl
  124. The container will start, and you can use docker ps to see its status:
  125. $ docker ps
  126. To stop a running container, use docker stop, followed by the container ID or name. This
  127. time, we'll use the name that Docker assigned the container, which is
  128. xenodochial_neumann:
  129. $ docker stop xenodochial_neumann
  130. Onece you've decided you no longer need a container anymore, remove it with the docker
  131. rm command, again using either the container ID or the name. Use the docker ps -a
  132. command to find the container ID or name for the container associated with the
  133. hello-world image and remote it:
  134. $ docker rm youthful_roentgen
  135. You can start a new container and give it a name using the --name switch. You can also
  136. use the --rm switch to create a container that removes itself when it's stopped. See the
  137. docker run help command for more information on these options and others.
  138. Step 7: Committing changes in a container to a docker image
  139. When you start up a docker image, you can create, modify, and delete files just like you
  140. can with a virtual machine. The changes that you make will only apply to that container.
  141. You can start and stop it, but once you destory it with the docker rm command, the
  142. changes will be lost for good.
  143. # mysql installation in docker
  144. Ref: https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-getting-started.html
  145. sudo docker search mysql
  146. sudo docker pull mysql/mysql-server
  147. sudo docker images
  148. sudo docker rmi <imagename>
  149. docker help | less -N
  150. -----
  151. sudo docker run -d mysql/mysql-server
  152. sudo docker ps
  153. sudo docker logs <container_name> 2>&1 | grep GENERATED
  154. sudo docker exec -it <container_name> mysql -uroot -p
  155. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
  156. mysql> show databases;
  157. mysql> exit
  158. sudo docker rm <container_name>
  159. -----
  160. sudo docker images
  161. sudo docker ps
  162. sudo docker ps -a
  163. sudo docker start <containerID>
  164. sudo docker exec -it <containername> mysql -uroot -p
  165. sudo docker stop <containerID>
  166. -----
  167. # Docker Administration
  168. docker ps
  169. docker ps -a
  170. docker rm <NAMES>
  171. docker start <containerID>/<NAMES>
  172. docker exec -it <containerID>/<NAMES> bash
  173. docker stop <containerID>/<NAMES>
  174. # copying files from host to docker container
  175. docker cp file.txt <containerID>:/home/user/path/
  176. # remove
  177. docker rm <NAME>
  178. # rename
  179. docker rename <currentName> <newName>
  180. # Information regarding docker images
  181. docker info
  182. # Inspect docker image
  183. docker image inspect --format '{{json .}}' "$IMAGE_ID"
  184. docker image inspect debian | less -N
  185. docker inspect debian | less -N
  186. # Find OS running in Docker container
  187. lsb_release -sirc
  188. cat /etc/os-release
  189. cat /etc/issue
  190. # change user
  191. su - raman # msqdb
  192. # rpm commands
  193. # list all installed packages
  194. rpm -qa
  195. rpm -qa | grep httpd
  196. rpm -qa --last
  197. yum list installed
  198. sudo -su raman
  199. exit
  200. # ip address
  201. yum install iproute2 (ip addr) # not working in dbase
  202. yum install net-tools (ifconfig) # working in dbase
  203. # wget
  204. yum install wget
  205. # systemd
  206. wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -O /usr/local/bin/systemctl
  207. chmod u+x /usr/local/bin/systemctl
  208. /usr/local/bin/systemctl status sshd
  209. /usr/local/bin/systemctl start sshd
  210. yum remove openssh-server
  211. yum autoremove openssh-server
  212. # init.d
  213. sudo /etc/init.d/ssh restart
  214. # configuration files for ssh
  215. /etc/ssh/
  216. # to check sshd started when boot
  217. ps aux | grep sshd
  218. docker run -it -p 1001:25 --postfix1 --privileged -d --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro postfix
  219. docker run -d -name centos7 --privileged=true centos:7 /usr/sbin/init
  220. #
  221. docker container diff <container-id>/<name>