___ # Tags #docker #docker-cli #dockerfiles #ubuntu # Helpful docs - [LearnLinuxTV](https://www.youtube.com/watch?v=PgR3Wc4_X1g&list=PLT98CRl2KxKECHltRib03tG8pyKEzwf9t&index=3) ## Install docker ```txt sudo apt install docker.io ``` ## Add your user to the docker group to avoid using sudo all the time ```txt sudo usermod -aG docker theoss ``` ## Running a container ```txt docker run hello-world ``` ## Enable the docker service so it starts on reboot ```txt sudo systemctl enable docker ``` ## Start the docker service ```txt sudo systemctl start docker ``` ## The output should pull down the hello-world image if it wasn't found locally ```txt theoss@containers:~$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. ``` ## Check on current installed docker images ```txt theoss@containers:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 months ago 13.3kB ``` ## Search for containers ```txt theoss@containers:~$ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 13383 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 596 [OK] websphere-liberty WebSphere Liberty multi-architecture images … 282 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 256 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 243 [OK] ``` ## Grab the container image from dockerhub ```txt theoss@containers:~$ docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 7b1a6ab2e44d: Pull complete Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest ``` ## When you run a container without a task the container autokills its process - Some containers contain apps that auto launch when started like running the **nginx webserver** ```txt theoss@containers:~$ docker run nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx a2abf6c4d29d: Pull complete f3409a9a9e73: Pull complete 9919a6cbae9c: Pull complete fc1ce43285d7: Pull complete 1f01ab499216: Pull complete 13cfaf79ff6d: Pull complete Digest: sha256:366e9f1ddebdb844044c2fafd13b75271a9f620819370f8971220c2b330a9254 Status: Downloaded newer image for nginx:latest /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/12/28 03:34:00 [notice] 1#1: using the "epoll" event method 2021/12/28 03:34:00 [notice] 1#1: nginx/1.21.4 2021/12/28 03:34:00 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2021/12/28 03:34:00 [notice] 1#1: OS: Linux 4.15.0-163-generic ``` ## To check on running containers ```txt docker ps ``` ## Use ps -a to see historically running containers ```txt theoss@containers:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fc1cbbfb4f81 nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) About a minute ago youthful_grothendieck a5f28a0e1802 ubuntu "bash" 4 minutes ago Exited (0) 4 minutes ago elegant_robinson 02eeadf6ebdb ubuntu "bash" 4 minutes ago Exited (0) 4 minutes ago wizardly_knuth 6aecdae69dcc ubuntu "bash" 4 minutes ago Exited (0) 4 minutes ago zen_payne 046b6cb55134 ubuntu "bash" 4 minutes ago Exited (0) 4 minutes ago fervent_hugle d6352a5db402 hello-world "/hello" 8 minutes ago Exited (0) 8 minutes ago hopeful_hofstadter ``` ## Run a switch in docker to keep a container running even if it doesn't have a task assigned to it - The `-it ubuntu /bin/bash` command opens a shell into the ubuntu container - You can install pretty much any app within this container, but containers are not persistent and will be removed when the container gets exited ```txt theoss@containers:~$ docker run -it ubuntu /bin/bash root@65af8ae7c4aa:/# apt update Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB] Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:3 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [733 kB] Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] ``` ## To keep a container running in the background regardless of having a task - `-it` can be thought of as interactive mode, this will open up a shell without the `/bin/bash` added to the end - `-d` adding this switch runs the container as a daemon in the background ```txt docker run -it -d ubuntu ``` ## If you want to jump into the terminal of a container already running - Find the \*\*container id \*\*using `docker ps` ```txt theoss@containers:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d0020916b81 ubuntu "bash" 3 seconds ago Up 2 seconds tender_hodgkin ``` - Then take the \*\*container id \*\*and run `docker attach [container id]` and you should enter the terminal - If you exit this container it will exit itself - **ctl+p+q** will exit a container while leaving it running and persistent ```txt theoss@containers:~$ docker attach 0d0020916b81 root@0d0020916b81:/# ``` ## Access applications running on a container - Running nginx in the background to an accessible port - `-p 8080:80` is to assign the port, **8080** is the host machine's port and **80** is the port being forwarded to the docker container ```txt docker run -it -d -p 8080:80 nginx ``` - With the docker running it should now be accessible on **port 8080** ![[Pasted image 20220401175249.png]] - You can use the `stop` command as a shorthand by typing in some of the characters of the container id to kill it ```txt theoss@containers:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f9e925887b7d nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp fervent_shtern theoss@containers:~$ docker stop f9e f9e ``` - You can also add a `--restart unless-stopped` switch to a container's execution to prevent the container from exiting ## Creating docker images - We are going to create a state of a container using someone else's container and commit it to docker for reuse - In the Ubuntu container apache2 has been installed and setup along with nano ```txt theoss@containers:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad65ca2ab429 ubuntu "bash" 2 minutes ago Up 2 minutes agitated_franklin ee7db560a304 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp competent_ardinghelli theoss@containers:~$ docker commit ad65 theoss/apache2-nano-test sha256:1f6161227c4ff7f8faed5183e74192493079010ae2dc55c5355c9d96c6faccd7 ``` - So we run `docker ps` to get running containers and then run `docker commit [container id] [name/container name]` ```txt theoss@containers:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE theoss/apache2-nano-test latest 1f6161227c4f 11 seconds ago 219MB nginx latest f6987c8d6ed5 7 days ago 141MB ubuntu latest ba6acccedd29 2 months ago 72.8MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB ``` - The image has been committed to our local docker repository - You can add a tag to your image by using the `:[version/latest]` after the container id in the `name/container name` ## Changing an already committed container - In a previous example we used **nginx** to run our webserver  - Now we've installed **apache2** and want to run it on the same webserver port except we want **apache2 **to startup on container execution, we we add an `--change='ENTRYPOINT [params]'` line to insert a command on container execution ```txt theoss@containers:~$ docker commit --change='ENTRYPOINT ["apachectl", "-DFOREGROUND"]' 515 theoss/apache2-nano-test:v1.1 sha256:01c1a3a05b670b45b8c67591fc94bbc3300ab47dbf56e20092b7826307af6cd5 ``` - So now we can run the new container after the changes have been committed by stating the `:v1.1` tag to indicate we want to use the changed container ```txt docker run -d -it -p 8080:80 theoss/apache2-nano-test:v1.1 ``` - Now we should see the **apache2 webserver** ![[Pasted image 20220401175421.png]] ## Creating an automated config out of dockerfiles - Here is a sample config, all of the caps arguments are docker recognized commands, we are saying use the ubuntu image, any prompts that require input from a user can be ignored, update and upgrade all packages, install apache2 and nano, then run apache2 ```txt FROM ubuntu MAINTAINER theoss # skip interactive prompts prompts ARG DEBIAN_FRONTEND=noninteractive # update packages RUN apt update; apt dist-upgrade -y # install packages RUN apt install -y apache2 nano # set entrypoint ENTRYPOINT apache2ctl -D FOREGROUND ``` - To build the docker image using the docker file we run the following command, it'll run the commands within the dockerfile and then kick off the container ```txt theoss@containers:~/dockerfiles$ docker build -t theoss/apache2-nano-test:v1.2 /home/theoss/dockerfiles/ Sending build context to Docker daemon 2.048kB Step 1/6 : FROM ubuntu ---> ba6acccedd29 Step 2/6 : MAINTAINER theoss ---> Running in dadb0b68df53 Removing intermediate container dadb0b68df53 ---> 4791a8fb0732 Step 3/6 : ARG DEBIAN_FRONTEND=noninteractive ---> Running in 8fcc6404c818 Removing intermediate container 8fcc6404c818 ---> 0e17e4213cf5 Step 4/6 : RUN apt update; apt dist-upgrade -y ---> Running in 5d7a630deae1 ... ... ... Removing intermediate container 8f37871c1989 ---> 4c166f20bbf8 Step 6/6 : ENTRYPOINT apache2ctl -D FOREGROUND ---> Running in ed2d7e03659f Removing intermediate container ed2d7e03659f ---> 408c028d697d Successfully built 408c028d697d Successfully tagged theoss/apache2-nano-test:v1.2 ``` - Taking a look at all of our images now to see that it was committed ```txt theoss@containers:~/dockerfiles$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE theoss/apache2-nano-test v1.2 408c028d697d 3 minutes ago 220MB theoss/apache2-nano-test v1.1 01c1a3a05b67 15 minutes ago 219MB theoss/apache2-nano-test v1.0 2d2df80f38c0 20 minutes ago 219MB theoss/apache2-nano-test latest 1f6161227c4f 31 minutes ago 219MB nginx latest f6987c8d6ed5 7 days ago 141MB ubuntu latest ba6acccedd29 2 months ago 72.8MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB ``` - Docker works in layers, so cleaning up historical images and previously ran docker containers is something to be aware of