Docker

SAITEJA IRRINKI
5 min readSep 3, 2022

--

Docker is a containerized tool designed to make it easier to create, deploy and run applications using containers. Containers allow a developer to package up an application with libraries and other dependencies and deploy it as one package. Containers are OS virtualization. We don’t need an OS in the container to install our application. It depends on the Host OS kernel.

Install Docker Engine & Docker-Compose on Ubuntu & CentOS

“Docker Setup” Create a file name docker_setup.sh and copy the below script

vim docker_setup.sh

Copy and paste the below script

#!/bin/bash
apt --help >>/dev/null
if [ $? -eq 0 ]
then
echo " INSTALLING DOCKER IN UBUNTU"
echo
sudo apt update
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get -y install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo docker run hello-world
else
echo " INSTALLING DOCKER IN CENTOS"
echo
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io -y
sudo systemctl start docker
sudo docker run hello-world
fi
echo " Installing Docker Compose"
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

Give Execute permission for script

sudo chmod +x docker_setup.sh

Now run the Script

./docker_setup.sh

Docker Commands

Docker Images

List all images that are locally stored with the Docker Engine

docker image ls

Build an image from the Dockerfile in the current directory and tag the image

docker build -t <imagename>:<tag>

Delete an image from the local image store

docker image rm <imagename>:<tag>

Containers

Run a container in an interactive mode:

docker run -it <imagename>:<tag>

Run a container from the Image nginx:latest, name the running container “web” and expose port 5000 externally, mapped to port 80 inside the container in detached mode.

docker run --name web -d -p 5000:80 nginx:latest

Run a detached container in a previously created container network:

docker network create <mynetwork>docker run --name web -d --net <mynetwork> -p 5000:80 nginx:latest

Follow the logs of a specific container:

docker logs -f <container name or container container-id>

List only active containers

docker ps

List all containers

docker ps -a

Stop a container

docker stop <container name or container container-id>

Stop a container (timeout = 1 second)

docker stop -t1

Remove a stopped container

docker rm <container name or container container-id>

Force stop and remove a container

docker rm -f <container name or container container-id>

Remove all containers

docker rm -f $(docker ps-aq)

Remove all stopped containers

docker rm $(docker ps -q -f “status=exited”)

Execute a new process in an existing container: Execute and access bash inside a container

docker exec -it <container name or container-id> bash

To inspect the container

docker inspect <container name or container container-id>

Share

To Establish Connections from Local to Remote. log in with your Dockerhub Credentials.

docker login

Pull an image from a registry

docker pull <imagename>:<tag>

Retag a local image with a new image name and tag

docker tag myimage:1.0 myrepo/myimage:2.0

Push an image to a registry.

docker push myrepo/myimage:2.0

Dockerfile

Sample Dockerfile for Deploying a Static website.

vim Dockerfile

Copy and paste the Below File

FROM centos:7
LABEL "Author"="saiteja Irrinki"
LABEL "Project"="Wave"
RUN yum install httpd wget unzip -y
RUN wget https://www.tooplate.com/zip-templates/2121_wave_cafe.zip
RUN unzip 2121_wave_cafe.zip
RUN cp -r 2121_wave_cafe/* /var/www/html/
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
EXPOSE 80
WORKDIR /var/www/html
VOLUME /var/log/httpd

Build the image from the Docker file, Here Change my Name with your registry name and Image.

docker build -t saitejairrinki/wavecafe:v1 .

Run Container From the Image

docker run --name wavecafe -d -p 9699:80 saitejairrinki/wavecafe:v1

Now Access From the Browser, Make sure you have to allow the port number in my case 9699 in your security group if you are using cloud VM.

Public-IPaddress:9699

!!! tip “Docker Image” You can pull my image and you can also run a container from my image without creating Dockerfile.

docker pull saitejairrinki/wavecafe:v1docker run --name wavecafe -d -p 9999:80 saitejairrinki/wavecafe:v1

Now Access From the Browser, Make sure you have to allow the port number in my case 9999 in your security group if you are using cloud VM.

Public-IPaddress:9999

“Docker Compose” Creating Docker Compose for local Docker File

version: "3"
services:
Wavecafe:
build:
context: /Dockerfile_path/
ports:
- "5555:80"
container_name: wavecafe

Creating Docker Compose for DockerHub Images

version: '3'
services:
website:
image: saitejairrinki/wavecafe:v1
ports:
- "8085:80"

Docker Volume

Creating a Separate Directory to Store Container data

mkdir mountbind

Now link your Directory while running the container

docker run --name db01 -e MYSQL_ROOT_PASSWORD=secret123 -p 3300:3306 -v /root/mountbind:/var/lib/mysql -d mysql:5.7

Now do ls to the Directory there you can see the containers data

ls mountbind

Creating docker Volume, use the below command to see all the available options of docker volume

docker volume --help

Creating a new docker volume with name ==datadb==

docker volume create datadb

Now run your container with that volume

docker run --name db02 -e MYSQL_ROOT_PASSWORD=secret123 -p 3301:3306 -v datadb:/var/lib/mysql -d mysql:5.7

Now check

ls /var/lib/docker/volumes/datadb/_data/

Now for testing Create any file with any name of your choice, here I’m creating a file with the name Milkyway

touch /var/lib/docker/volumes/datadb/_data/milkyway

Now log in into the container and Verify your file.

docker exec -it db02 /bin/bashls /var/lib/mysql/

Now exit from the container

exit

If you want to access the MySQL database with a MySQL client then follow the below steps

sudo apt updatesudo apt install mysql-client

Now fetch the container IP by doing Docker Inspect

docker inspect db02 |grep -i ipaddress

Now Connect with that IP

mysql -h 172.17.0.4 -u root -psecret123

--

--

SAITEJA IRRINKI
SAITEJA IRRINKI

Written by SAITEJA IRRINKI

I’m SAITEJA IRRINKI Working as a Senior DevOps Engineer in Build & Release. Experienced in Provisioning and Managing Cloud Infrastructure.

No responses yet