"Docker-Notes"

Published: Sat 18 September 2021

In content.

Docker Notes

Docker container is a running instance of Docker image.

Commands

$docker images For example, it shows available images in the local repo
docker search <SEARCH_STRING> For example docker search ubuntu
docker pull <IMAGE> For example docker pull ubuntu:latest
docker run -i -t ubuntu To run the image, -i for interactive, -t to attach to the terminal and then the image name or by image id.

docker ps list currently running docker processes
docker ps -a list docker processes that are running or not

To rename the docker container, you can use docker rename <DOCKER_NAME> <NEW_NAME> . <DOCKER_NAME> can be obtained from output of docker ps -a command.

If you run docker run <IMAGE_NAME> again, it will not start the container but create a new instance of the image, so you use docker start <CONTAINER_NAME>.

To remove a lot of containers, use docker rm $(docker ps -aq)

To remove docker images, use docker rmi <IMAGE_ID/IMAGE_NAME>

To remove all images, use docker rmi $(docker images -q)

Dockerfile

docker build Dockerfile or docker build -f <PATH_OF_DOCKERFILE> to build custom docker image.
docker build -t <DOCKERFILE> , to elevate to sudo before docker build.
To run the newly built custom docker image, run docker run -p 80:80 -i -t <NAME_OF_CUSTOM_IMAGE>

Example of a Dockerfile

# Add the hello world file and execute upon container launch

FROM ubuntu:latest
MAINTAINER vgnshlvnz
RUN apt-get update
RUN apt-get install python3 python3-pip
RUN pip install --upgrade pip
ADD hello.py /home/hello.py
WORKDIR /home
CMD python3 hello.py

Example Web Server

create a docker file with following content:

#Example of a webserver running Apache serving simple html file  

FROM ubuntu:latest  
MAINTAINER vgnshlvnz  
ENV DEBIAN_FRONTEND=noninteractive  
ENV APACHE_LYNX=lynx  
RUN apt-get update  
RUN apt-get install -y apache2 apache2-utils php libapache2-mod-php lynx  
RUN a2enmod php7.4  
RUN ln -fs /usr/share/zoneinfo/Asia/Kuala_Lumpur /etc/localtime  
RUN dpkg-reconfigure --frontend noninteractive tzdata  
ENV APACHE_LOG_DIR /var/log/apache2  
COPY index.html /var/www/html  
EXPOSE 80  
CMD apachectl -D FOREGROUND

create a file called index.html in the same directory and put simple html content.

now run docker build -t <IMAGE_NAME> .. You can also specify -f parameter and pass the location of your docker file if it is in a different location.

Docker Swarm

to have a working docker swarm you will need least 2 or 3 virtual machines.

one the first virtual machine type docker swarm init --advertise-addr=<IP_ADDR_OF_HOST>. first virtual machine will become manager node
will give you output to add worker to the swarm, something like following docker swarm join --token <TOKEN_STRING> <IP_ADDR_OF_MANAGER_NODE>:2377. Paste the string in second and third virtual machine.

on the master node, type docker node ls and you should see 3 nodes with one node as leader and the rest as worker nodes.

To create a service, type following command in your manager node: docker service create --replicas 2 -p 80:80 --name <NAME_OF_SERVICE> nginx . What this command will do is it will create 2 replicas, expose port 80 to host port 80 based on nginx image.

Type docker service ls again to see newly created Docker service.

To see the processes related to newly created service, type docker service ps <NAME_OF_SERVICE>

Docker Registry

docker pull registry it will be part of a image

docker run -d -p 5000:5000 --name <NAME_OF_CONTAINER> registry this will start a container with port 5000 exposed

docker pull busybox:latest will pull latest busybox image

run docker images to see the newly downloaded image

docker tag busybox:latest <IP_ADDR_OF_HOST>:5000/busybox:latest to push the image to local repository

to scale your node, run following command docker service scale <NAME_OF_SERVICE>=3

To verify, type docker service ps web you should see 3 nodes set up.

Written with StackEdit.

social