Last active
July 1, 2020 10:38
-
-
Save rakibulinux/3f6e197606e2ed428ad7fb123f616723 to your computer and use it in GitHub Desktop.
How to Install and Use Docker on Ubuntu 18.04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#https://youtu.be/oW0ahpCJsPU | |
sudo apt update | |
sudo apt install apt-transport-https ca-certificates curl software-properties-common | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" | |
sudo apt update | |
apt-cache policy docker-ce | |
sudo apt install docker-ce | |
sudo systemctl status docker | |
#Executing the Docker Command Without Sudo | |
sudo usermod -aG docker ${USER} | |
su - ${USER} | |
id -nG | |
sudo usermod -aG docker rakib | |
#Using the Docker Command | |
docker [option] [command] [arguments] | |
docker | |
docker docker-subcommand --help | |
docker info | |
docker --version | |
#Working with Docker Images | |
docker run hello-world | |
docker search ubuntu | |
docker pull ubuntu | |
docker images | |
docker image ls | |
#Running a Docker Container | |
docker run -it ubuntu | |
apt update | |
apt install nodejs | |
node -v | |
exit | |
#Managing Docker Containers | |
docker ps | |
docker ps -a | |
docker ps -l | |
docker start d9b100f2f636 | |
docker stop NAME of file (ubuntu) | |
docker rm NAME of file (ubuntu) | |
#Remove all docker conteiner | |
docker rm -f $(docker ps -a -q) | |
#Purging All Unused or Dangling Images, Containers, Volumes, and Networks | |
docker system prune | |
docker system prune -a | |
#Removing Docker Images | |
docker rmi Image Image | |
#Committing Changes in a Container to a Docker Image | |
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name | |
docker commit -m "added Node.js" -a "RakibuLinux" d9b100f2f636 rakib/ubuntu-nodejs | |
docker images | |
#Pushing Docker Images to a Docker Repository | |
docker login -u docker-registry-username | |
docker login -u rakibulinux | |
#Note: If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username. For the example given in the last step, you would type: | |
sudo docker tag rakibulinux/ubuntu-nodejs docker-registry-username/ubuntu-nodejs | |
#Then you may push your own image using: | |
docker push docker-registry-username/docker-image-name | |
#To push the ubuntu-nodejs image to the sammy repository, the command would be: | |
docker push rakib/ubuntu-nodejs | |
#Create and manage volumes | |
#Create a volume: | |
docker volume create my-vol | |
#List volumes: | |
docker volume ls | |
#Inspect a volume: | |
docker volume inspect my-vol | |
#Remove a volume: | |
docker volume rm my-vol | |
#Start a container with a volume. The -v and --mount examples below | |
#Use --mount | |
docker run -d \ | |
--name devtest \ | |
--mount source=myvol2,target=/app \ | |
nginx:latest | |
#Use -v | |
docker run -d \ | |
--name devtest \ | |
-v myvol2:/app \ | |
nginx:latest | |
#Use docker inspect devtest to verify that the volume was created and mounted correctly. Look for the Mounts section: | |
docker inspect devtest | |
#Stop the container and remove the volume | |
docker container stop devtest | |
docker container rm devtest | |
docker volume rm myvol2 | |
#Start a service with volumes | |
docker service create -d \ | |
--replicas=4 \ | |
--name devtest-service \ | |
--mount source=myvol2,target=/app \ | |
nginx:latest | |
#Use docker service ps devtest-service to verify that the service is running: | |
docker service ps devtest-service | |
#Remove the service, which stops all its tasks: | |
docker service rm devtest-service | |
#Populate a volume using a container | |
#To illustrate this, this example starts an nginx container and populates the new volume nginx-vol with the contents of the container’s /usr/share/nginx/html directory, which is where Nginx stores its default HTML content. | |
#The --mount and -v examples have the same end result. | |
#Use --mount | |
docker run -d \ | |
--name=nginxtest \ | |
--mount source=nginx-vol,destination=/usr/share/nginx/html \ | |
nginx:latest | |
#Use -v | |
docker run -d \ | |
--name=nginxtest \ | |
-v nginx-vol:/usr/share/nginx/html \ | |
nginx:latest | |
#After running either of these examples, run the following commands to clean up the containers and volumes. Note volume removal is a separate step. | |
docker container stop nginxtest | |
docker container rm nginxtest | |
docker volume rm nginx-vol | |
#What is Docker Swarm? | |
#Introduction: A Docker Swarm is a group of either physical or virtual machines that are running the Docker application and that have been configured to join together in a cluster. ... Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines. | |
#For this part you need to install docker-machine. 1st check "docker-machine -v or docker-machine --version" if it's installed then you don't need to install it. If not then folow this step. | |
base=https://github.com/docker/machine/releases/download/v0.16.0 && | |
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine && | |
sudo mv /tmp/docker-machine /usr/local/bin/docker-machine && | |
chmod +x /usr/local/bin/docker-machine | |
docker-machine --version | |
#You will need VertualBox. Follow this step for VertualBox install. | |
#Prerequsities | |
sudo apt update | |
sudo apt upgrade | |
#Setup Apt Repository | |
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add - | |
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add - | |
sudo add-apt-repository "deb http://download.virtualbox.org/virtualbox/debian bionic contrib" | |
#Install VirtualBox on Ubuntu 18.04 | |
sudo apt update | |
sudo apt install virtualbox-6.0 | |
# Start VirtualBox | |
virtualbox | |
#Also you can try this: | |
sudo -s | |
curl -L https://github.com/docker/machine/releases/download/v0.7.0/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine && \ | |
chmod +x /usr/local/bin/docker-machine | |
#Now follow this step for Swarm | |
Step 1 : Create Docker machines (to act as nodes for Docker Swarm) Create one machine as manager and others as workers | |
docker-machine create --driver hyperv manager1 | |
docker-machine create --driver virtualbox manager1 | |
docker-machine:Error with pre-create check: “exit status 126” | |
sudo apt-get install virtualbox; | |
yum install virtualbox; | |
Create one manager machine | |
and other worker machines | |
Step 2 : Check machine created successfully | |
docker-machine ls | |
docker-machine ip manager1 | |
Step 3 : SSH (connect) to docker machine | |
docker-machine ssh manager1 | |
Step 4 : Initialize Docker Swarm. You will get an IP for maneger when you create it. Add the IP at "MANAGER_IP" | |
docker swarm init --advertise-addr MANAGER_IP | |
docker node ls | |
(this command will work only in swarm manager and not in worker) | |
Step 5 : Join workers in the swarm | |
Get command for joining as worker | |
In manager node run command | |
docker swarm join-token worker | |
This will give command to join swarm as worker | |
docker swarm join-token manager | |
This will give command to join swarm as manager | |
SSH into worker node (machine) and run command to join swarm as worker | |
In Manager Run command - docker node ls to verify worker is registered and is ready | |
Do this for all worker machines | |
Step 6 : On manager run standard docker commands | |
docker info | |
check the swarm section | |
no of manager, nodes etc | |
Now check docker swarm command options | |
docker swarm | |
Step 7 : Run containers on Docker Swarm | |
docker service create --replicas 3 -p 80:80 --name serviceName nginx | |
Check the status: | |
docker service ls | |
docker service ps serviceName | |
Check the service running on all nodes | |
Check on the browser by giving ip for all nodes | |
Step 8 : Scale service up and down | |
On manager node | |
docker service scale serviceName=2 | |
Inspecting Nodes (this command can run only on manager node) | |
docker node inspect nodename | |
docker node inspect self | |
docker node inspect worker1 | |
Step 9 : Shutdown node | |
docker node update --availability drain worker1 | |
Step 10 : Update service | |
docker service update --image imagename:version web | |
docker service update --image nginx:1.14.0 serviceName | |
Step 11 : Remove service | |
docker service rm serviceName | |
docker swarm leave : to leave the swarm | |
docker-machine stop machineName : to stop the machine | |
docker-machine rm machineName : to remove the machine | |
#Docker Manage Ports | |
git clone https://github.com/tecrahul/dockerfile | |
cd dockerfile | |
#Now build the docker image with name apacheimage. | |
docker build -t apacheimage . | |
#Now run the container using docker run command. The Apache service will start on the container on port 80. You need to specify -p 8080:80 to bind host system port 8080 with container port 80. | |
docker run -it -p 8080:80 apacheimage | |
#You can use multipe port for one image | |
docker run -it -p 8080:80,8081:443 image_name | |
#If you need to bind port with the specific interface of host machine define its IP address as below. The below example, port 8080, 8081 will accessible with 127.0.0.1 IP only. | |
docker run -it -p 127.0.0.1:8080:80,127.0.0.1:8081:443 image_name | |
docker run -it -p 172.158.1.151:8080:80,99.198.1.111:8081:443 image_name | |
#Docker Networking | |
docker network [options] | |
#List Docker Networks | |
docker network ls | |
#Create Docker Network | |
Syntax: | |
docker network create -d [network_type] [network_name] | |
Example: | |
docker network create -d bridge my-bridge-network | |
#Connect Container to Network | |
Syntax: | |
docker network connect [network_name] [container_name] | |
Example: | |
docker network connect my-bridge-network centos | |
#Disconnect Container from Network. You can disconnect a container from the specific network any time using the following command. | |
Syntax: | |
docker network disconnect [network_name] [container_name] | |
Example: | |
docker network disconnect my-bridge-network centos | |
#Inspect a Docker Network. Use inspect option with docker network comment to view the details of docker network | |
docker network inspect my-bridge-network | |
#Remove a Docker Network. Use rm option to remove any existing unused Docker network. You can specify one or more networks with space separated to remove. | |
Example: | |
docker network rm my-bridge-network network2 network3. | |
#You can also remove all unused networks from docker host using prune option. You will see this worning massege. WARNING! This will remove all networks not used by at least one container. Are you sure you want to continue? [y/N] y | |
docker network prune | |
#Docker Networking Example | |
#Create Network | |
docker network create -d bridge my-bridge-network | |
#Run MySQL Container. Now, run a new MySQL docker container. Set the default root user password with MYSQL_ROOT_PASSWORD variable as showing in below command. | |
docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server | |
#After creating container add this to our network. | |
docker network connect my-bridge-network mysql | |
#Now view the new IP address of the MySQL container, which is required in next step. | |
docker inspect mysql | grep "IPAddress" | |
#Run PHPMyAdmin Container. Now run a new Docker container of phpmyadmin using the following command. Change the PMA_HOST value with the IP address of MySQL container IP address get in last step. | |
docker run --name phpmyadmin -d -e PMA_HOST=172.17.0.3 -p 8080:80 phpmyadmin/phpmyadmin | |
#Add this container to our network. | |
docker network inspect my-bridge-network | |
#Inspect Our Network. As you have attached both containers to our new network. Let’s inspect the current network settings. | |
docker network inspect my-bridge-network | |
#Allow MySQL to PHPMyAdmin Host. The default MySQL doesn’t allow remote hosts to connect. So allow phpmyadmin for MySQL connection. Get shell access to your MySQL container using below command. | |
docker exec -it mysql bash | |
Login to your MySQL server using the password provided during instance creation. | |
bash-4.2# mysql -u root -p | |
CREATE USER 'vtiger'@'localhost' IDENTIFIED BY 'Stsx#ngPass@#SDs#'; | |
CREATE DATABASE vtiger; | |
GRANT ALL PRIVILEGES ON vtiger.* TO 'vtiger'@'localhost'; | |
FLUSH PRIVILEGES; | |
SHOW DATABASES; | |
QUIT | |
#Access MySQL with PHPMyAdmin. Use MySQL credentials created in above step to login to PHPMyAdmin. | |
http://localhost:8080/index.php | |
#Install Docker-Compose | |
Step 1 : install docker compose | |
(already installed on windows and mac with docker) | |
docker-compose -v | |
#2 Ways | |
git clone https://github.com/docker/compose/releases | |
#Using PIP | |
pip install -U docker-compose | |
#Also you can try this for Install Docker-Compose | |
curl -L https://github.com/docker/compose/releases/download/1.26.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose | |
chmod +x /usr/local/bin/docker-compose | |
#Docker-Compose | |
mkdir DockerComposeFile | |
cd DockerComposeFile/ | |
touch docker-compose.yml | |
nano docker-compose.yml | |
version: '2.2' | |
services: | |
web: | |
image: nginx | |
ports: | |
- "8080:80" | |
database: | |
image: redis | |
#Save and exit | |
cat docker-compose.yml | |
services: | |
web: | |
image: nginx | |
database: | |
image: redis | |
#Check the validity of file by command | |
docker-compose config | |
#Run docker-compose.yml file by command | |
docker-compose up -d | |
#Bring down application by command | |
docker-compose down | |
#How to scale services (—scale) | |
docker-compose up -d --scale database=4 | |
#Check Docker Compose List | |
docker-compose ps | |
#exec – | |
docker-compose exec web ls -l | |
#Start all containers | |
docker-compose start | |
#Start single container | |
docker-compose start web | |
# Stop all containers | |
docker-compose stop | |
#Stop single container | |
docker-compose stop web | |
#Restart all containers | |
docker-compose restart | |
# Restart single container | |
docker-compose restart web | |
# Pause all containers | |
docker-compose pause | |
# Pause single container | |
docker-compose pause web | |
# Start all paused containers | |
docker-compose pause | |
# Start single paused container | |
docker-compose pause web | |
# Start all paused containers | |
docker-compose rm | |
# Start single paused container | |
docker-compose pause web | |
#Prune Objects in Docker | |
docker [object] prune [options] | |
docker system prune | |
#The –all option will remove all unused images | |
docker system prune --all | |
#The filtering flag (-f or –filter) format is “key=value”. Like until () will remove containers, images, and networks created before given timestamp | |
docker system prune --filter "until=24h" | |
#Prune Images. This will remove all unused images from system. | |
docker image prune | |
#Prune Containers: This will remove all unused volumes from system. | |
docker container prune | |
#Prune Volume: This will remove all unused volumes from system. | |
docker volume prune | |
#–filter Provide filter values (e.g. ‘label=’) | |
#–force , -f false Do not prompt for confirmation | |
#Prune Network: This will remove all unused docker networks from system. | |
docker network prune | |
#–filter Provide filter values (e.g. ‘label=’) | |
#–force , -f false Do not prompt for confirmation | |
#Use a read-only volume | |
#This example modifies the one above but mounts the directory as a read-only volume, by adding ro to the (empty by default) list of options, after the mount point within the container. Where multiple options are present, separate them by commas. | |
#The --mount and -v examples have the same result. | |
#Use --mount | |
docker run -d \ | |
--name=nginxtest \ | |
--mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \ | |
nginx:latest | |
#Use -v | |
docker run -d \ | |
--name=nginxtest \ | |
-v nginx-vol:/usr/share/nginx/html:ro \ | |
nginx:latest | |
#Use docker inspect nginxtest to verify that the readonly mount was created correctly. | |
docker inspect nginxtest | |
#Stop and remove the container, and remove the volume. Volume removal is a separate step. | |
docker container stop nginxtest | |
docker container rm nginxtest | |
docker volume rm nginx-vol | |
#Use a volume driver | |
#When you create a volume using docker volume create, or when you start a container which uses a not-yet-created volume, you can specify a volume driver. The following examples use the vieux/sshfs volume driver, first when creating a standalone volume, and then when starting a container which creates a new volume. | |
#Initial set-up | |
#This example assumes that you have two nodes, the first of which is a Docker host and can connect to the second using SSH. | |
#On the Docker host, install the vieux/sshfs plugin: | |
docker plugin install --grant-all-permissions vieux/sshfs | |
#Create a volume using a volume driver | |
#Using an -o | |
docker volume create --driver vieux/sshfs \ | |
-o sshcmd=test@node2:/home/test \ | |
-o password=testpassword \ | |
sshvolume | |
#Start a container which creates a volume using a volume drive | |
docker run -d \ | |
--name sshfs-container \ | |
--volume-driver vieux/sshfs \ | |
--mount src=sshvolume,target=/app,volume-opt=sshcmd=test@node2:/home/test,volume-opt=password=testpassword \ | |
nginx:latest | |
#Create a service which creates an NFS volume | |
#This example shows how you can create an NFS volume when creating a service. This example uses 10.0.0.10 as the NFS server and /var/docker-nfs as the exported directory on the NFS server. Note that the volume driver specified is local | |
#NFSV3 | |
docker service create -d \ | |
--name nfs-service \ | |
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,volume-opt=o=addr=10.0.0.10' \ | |
nginx:latest | |
#NFSV4 | |
docker service create -d \ | |
--name nfs-service \ | |
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/,"volume-opt=o=10.0.0.10,rw,nfsvers=4,async"' \ | |
nginx:latest | |
#Backup, restore, or migrate data volumes | |
#Backup a container. For example, create a new container named dbstore: | |
docker run -v /dbdata --name dbstore ubuntu /bin/bash | |
#Then in the next command, we: | |
1. Launch a new container and mount the volume from the dbstore container | |
2. Mount a local host directory as /backup | |
3. Pass a command that tars the contents of the dbdata volume to a backup.tar file inside our /backup directory. | |
docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata | |
#Restore container from backup | |
#For example, create a new container named dbstore2: | |
docker run -v /dbdata --name dbstore2 ubuntu /bin/bash | |
#Then un-tar the backup file in the new container`s data volume: | |
docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1" | |
#Remove volumes | |
1. Named volumes have a specific source from outside the container, for example awesome:/bar. | |
2. Anonymous volumes have no specific source so when the container is deleted, instruct the Docker Engine daemon to remove them. | |
#Remove anonymous volumes. To automatically remove anonymous volumes, use the --rm option. For example, this command creates an anonymous /foo volume. When the container is removed, the Docker Engine removes the /foo volume but not the awesome volume. | |
docker run --rm -v /foo -v awesome:/bar busybox top | |
#Remove all volumes | |
docker volume prune | |
##1. Use Docker Data Volume Container | |
#Create Data Volume Container: | |
docker create -v /data --name data_container centos | |
#Use Data Volume to New Container: | |
docker run -it --volumes-from data_container centos /bin/bash | |
cd /data | |
echo "Hello Docker" > file.txt | |
#Now you can exit from current instance. | |
#Verify Files on Data Volume | |
docker run -it --volumes-from data_container centos /bin/bash | |
##2. Sharing Host Volume to Docker Container | |
docker run -it -v /var/www:/data centos | |
#Working with Dockerfile | |
mkdir /home/rakib/Dockerfile | |
#Build Image with Dockerfile | |
docker build -t image_name . | |
#You can use -f flag to point to a Dockerfile anywhere in your file system | |
docker build -t image_name -f /path/to/Dockerfile . | |
#Create a Dockerfile | |
git clone https://github.com/tecrahul/dockerfile | |
cd dockerfile | |
#Now build the docker image with name apacheimage. | |
docker build -t apacheimage . | |
#After build, You can view the image under “docker images” command. | |
docker images | |
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils | |
#Launch Container with Image | |
docker run -it -p 8080:80 apacheimage | |
#Use this for test Docker commands | |
#https://www.katacoda.com/courses/docker | |
#What are Dockerfile Directives | |
#FROM Examples: | |
FROM ubuntu | |
FROM rastasheep/ubuntu-sshd:18.04 | |
#How to make a SSH dockerfile | |
#You should make a folder and use this Dockerfile name | |
mkdir /home/Dockerfile | |
#You also make a file on /home/Dockerfile/ with Dockerfile | |
touch /home/Dockerfile/Docketfile | |
#Pest it on the file name Docketfile | |
FROM ubuntu:18.04 | |
RUN apt-get update && apt-get install -y openssh-server | |
RUN mkdir /var/run/sshd | |
RUN echo 'root:1234' | chpasswd | |
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/ssh_config | |
# SSH login fix. Otherwise user is kicked off after login | |
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd | |
ENV NOTVISIBLE "in users profile" | |
RUN echo "export VISIBLE=now" >> /etc/profile | |
EXPOSE 22 | |
CMD ["/usr/sbin/sshd", "-D"] | |
#Now save and exit | |
#Now perfrom with this command | |
docker build -t rakibulinux_sshd . | |
#Run a test_sshd container. You can provide any name like "rakibulinux" | |
docker run -d -P --name rakibulinux rakibulinux_sshd | |
docker port test_sshd 22 | |
ssh root@192.168.1.2 -p 49154 | |
# or | |
ssh root@localhost -p 49154 | |
# The password is ``screencast``. | |
root@f38c87f2a42d:/# | |
#Now you can remove docker image | |
docker container stop rakibulinux | |
docker container rm rakibulinux | |
docker image rm rakibulinux |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment