Docker¶
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¶
Create a file name docker_setup.sh and copy the below script
vim docker_setup.sh
#!/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
sudo chmod +x docker_setup.sh
./docker_setup.sh
Docker Commands¶
List all images that are locally stored with the Docker Engine
docker image ls
docker build -t <imagename>:<tag>
docker image rm <imagename>:<tag>
Run a container in an interactive mode:
docker run -it <imagename>:<tag>
docker run --name web -d -p 5000:80 nginx:latest
docker network create <mynetwork>
docker run --name web -d --net mynetwork -p 5000:80 nginx:latest
docker logs -f <container name or container container-id>
docker ps
docker ps -a
docker stop <container name or container container-id>
docker stop -t1
docker rm <container name or container container-id>
docker rm -f <container name or container container-id>
docker rm -f $(docker ps-aq)
docker rm $(docker ps -q -f “status=exited”)
docker exec -it <container name or container-id> bash
docker inspect <container name or container container-id>
To Establish Connections from Local to Remote. log in with your Dockerhub Credentials.
docker login
docker pull <imagename>:<tag>
docker tag myimage:1.0 myrepo/myimage:2.0
docker push myrepo/myimage:2.0
Sample Dockerfile for Deploying a Static website.
vim Dockerfile
FROM centos:7
LABEL "Author"="LIONEL TCHAMI"
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
docker build -t apotitech/wavecafe:v1 .
docker run --name wavecafe -d -p 9699:80 apotitech/wavecafe:v1
Public-IPaddress:9699
Docker Image
You can pull my image and you can also run a container from my image without creating Dockerfile.
docker pull apotitech/wavecafe:v1
docker run --name wavecafe -d -p 9999:80 apotitech/wavecafe:v1
Public-IPaddress:9999
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: apotitech/wavecafe:v1
ports:
- "8085:80"
Creating a Separate Directory to Store Container data
mkdir mountbind
docker run --name db01 -e MYSQL_ROOT_PASSWORD=secret123 -p 3300:3306 -v /root/mountbind:/var/lib/mysql -d mysql:5.7
ls mountbind
docker volume --help
docker volume create datadb
docker run --name db02 -e MYSQL_ROOT_PASSWORD=secret123 -p 3301:3306 -v datadb:/var/lib/mysql -d mysql:5.7
ls /var/lib/docker/volumes/datadb/_data/
touch /var/lib/docker/volumes/datadb/_data/milkyway
docker exec -it db02 /bin/bash
ls /var/lib/mysql/
exit
sudo apt update
sudo apt install mysql-client
docker inspect db02 |grep -i ipaddress
mysql -h 172.17.0.4 -u root -psecret123