Docker volume command

In this tutorial, How to use Docker volume command to details for Data volumes. There are three main use cases for Docker data volumes:

  • Keep data around when a container is removed
  • To share data between the host filesystem and the Docker container
  • To share data with other Docker containers

Docker volume command

Docker volume create

The syntax docker volume commands as below:

$ docker volume create [--option] NAME_VOLUME

Related docker volume command:

  • docker volume create: Create a volume docker volume inspect: Display detailed information on one or more volumes
  • docker volume ls: List volumes docker volume prune: Remove all unused volumes docker
  • volume rm: Remove one or more volumes

How to create a new volume and configure the container

$ docker volume create DATA
$ docker run -d -v DATA:/world centos ls /world

How to inspect container ID with docker inspect command

$ sudo docker inspect a04ae7b6cc17

For example, the output as below

"Mounts": [
 {
 "Name": "0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750",
 "Source": "/var/lib/docker/volumes/0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750/_data",
 "Destination": "/var/www/html",
 "Driver": "local",
 "Mode": "",
 "RW": true,
 "Propagation": ""
 },

To use docker inspect display Source and Destination for container ID

$ sudo docker inspect -f '{{ .Mounts}}' a04ae7b6cc17 | awk '{ print "Source:"$2 "\t " "Destination:"$3}'

The output below:

Source:/var/lib/docker/volumes/0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750/_data Destination:/var/www/html

How to use docker volume ls to list all volume for container

$ sudo docker volume ls

The output as below

[sudo] password for huupv:
 DRIVER VOLUME NAME
 local 0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750
 local 1d4c9030aea8554335b281d554a27422773f067f77ead5663e9b52f85d64ff6f
 local 387a0f11796f6d6d254abbbdf523410a42ed004dc5879c59ae45f5c922038441
 local 85621d725c9cee6444ab83162123000dd18bdd9a0a974a3cbe05b60d0175bc33
 local bbaaa1ea0d348d1e66ae6f606396502d895db8f6f2cd6f1cc9ca34605da3cf3c
 local devopsroles
 local e1f8e62ec711164a363bbc9bc53a0868e093dc7e4983fb327fe5f77b4e48d965
 local wordpress_db-data
 local wordpress_nginx-data
 local wordpress_php-data
 local wordpress_wp-data

How to use docker inspect exploit for container ID a04ae7b6cc17

$ sudo docker inspect -f '{{ (index .Mounts 0).Source }}' a04ae7b6cc17

The output Mounts source container ID a04ae7b6cc17

/var/lib/docker/volumes/0ed908df33e1790711933ad6b463fd2a88199802c5cf2b1beb5001b81bc27750/_data

Other dockers inspect command Mounts source container ID a04ae7b6cc17

$ sudo docker inspect --format '{{ range .Mounts }}{{ if eq .Destination "/var/www/html" }}{{ .Source }}{{ end }}{{ end }}' a04ae7b6cc17

How to create a Docker Volume Using a Dockerfile

$ vim Dockerfile

The content as below

# Create a volume
 VOLUME /dockerfilevolume

Next, build an image named dockerfile-volumetest from this Dockerfile with the command:

$ sudo docker build -t dockerfile-volumetest .

Then launch a container named “my-dockerfile-test” from this image with the command:

$ sudo docker run --name my-dockerfile-test -it dockerfile-volumetest /bin/bash

Detach from the container with Ctrl-p + Ctrl-q and return to the host machine’s command prompt.

Sharing Volumes Between Containers

Sharing a Volume on the Host

$ sudo mkdir /webdata
$ sudo docker run -it --name webapp -v /webdata:/var/www/html nginx /bin/bash

Using a Container as a Shared Data Volume

$ sudo docker run -it -v /shared-data --name data-storage centos /bin/bash
$ echo "Hello from the data-storage container." >> /shared-data/data-storage-hello.txt
$ sudo docker run -it --name app --volumes-from data-storage python /bin/bash

Mounting a Volume as Read-Only

$ docker run -v /directory:/path:ro
$ sudo docker volume create --name limited-access
$ sudo docker run -it --name allowed-to-write -v limited-access:/data centos /bin/bash

Conclusion

Thought the article, help you to details for data volume. Docker volume command helps useful to determine for containers. The update about docker volume command later. Thank you for reading the DevopsRoles page!

How to inspect Docker container

Introduction

In this tutorial, I’ll guide you on How to inspect docker container to gain detailed insights into containers. Docker inspect is a powerful tool that provides comprehensive information about various aspects of a container, including its network settings, current status, attached volumes, and more. By exploring these details, you’ll have a deeper understanding of your containers’ configurations and runtime characteristics.

This knowledge proves invaluable for troubleshooting, optimization, and gaining a holistic view of your Dockerized applications. Follow along as I demonstrate the versatility of Docker inspect, enabling you to effectively manage and analyze your containers with precision and ease.

What is docker inspect?

Docker inspect ” is a command in Docker that provides a detailed overview of container information. This powerful tool unveils specifics such as network configurations, status, and attached volumes. By leveraging “docker inspect,” users gain valuable insights into container settings and runtime details, essential for troubleshooting and optimizing Dockerized applications. Mastery of this command empowers efficient container management, allowing users to delve into the intricacies of their Docker environments with precision and ease

What is a Docker container?

  • Portable Packages: Docker containers encapsulate applications along with their dependencies, ensuring a consistent and portable environment.
  • Isolation: Containers operate independently, isolated from the host system and other containers, promoting stability and consistency.
  • Comprehensive Inclusion: Each container includes the necessary components—code, runtime, libraries, and tools—creating a self-contained unit ready to execute.
  • Docker Platform: Docker, a widely used containerization platform, facilitates the seamless packaging and deployment of applications.
  • Cross-Environment Consistency: Developers can build a container once and run it consistently across various environments, from local development setups to large-scale production systems.
  • Resource Efficiency: Containers optimize resource utilization, offering agility, scalability, and quick deployment, making them popular in modern software development and deployment practices.

Inspect Docker container

Utilize the “docker ps” command to showcase a list of containers as follows:

$ sudo docker ps

The displayed results below

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
055772699b49 devopsroles/nginx:v2 "nginx" 4 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp devopsroles

Provide details for the “devopsroles” container as illustrated below:

$ sudo docker inspect devopsroles

As an illustration, the output for the “devopsroles” container is presented below:

The omit
"NetworkSettings": {
 "Bridge": "",
 "SandboxID": "38a0b8ae70f0853cf36b25add2182132c2b31183c3d969f7c2b927f75080288c",
 "HairpinMode": false,
 "LinkLocalIPv6Address": "",
 "LinkLocalIPv6PrefixLen": 0,
 "Ports": {
 "80/tcp": [
 {
 "HostIp": "0.0.0.0",
 "HostPort": "8080"
 }
 ]

},
 "SandboxKey": "/var/run/docker/netns/38a0b8ae70f0",
 "SecondaryIPAddresses": null,
 "SecondaryIPv6Addresses": null,
 "EndpointID": "c0103fac129e8ff88fcb597b6cfd1836ca7ffb50580cbe60dc6e7b39f2de1e24",
 "Gateway": "172.17.0.1",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "IPAddress": "172.17.0.2",
 "IPPrefixLen": 16,
 "IPv6Gateway": "",
 "MacAddress": "02:42:ac:11:00:02",
 "Networks": {
 "bridge": {
 "IPAMConfig": null,
 "Links": null,
 "Aliases": null,
 "NetworkID": "42dd4e0f69e1edff2c81b2f311b2a0ed53478f47fdf5371643df9f1bdb150ba5",
 "EndpointID": "c0103fac129e8ff88fcb597b6cfd1836ca7ffb50580cbe60dc6e7b39f2de1e24",
 "Gateway": "172.17.0.1",
 "IPAddr$ sudo docker inspect devopsrolesess": "172.17.0.2",
 "IPPrefixLen": 16,
 "IPv6Gateway": "",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "MacAddress": "02:42:ac:11:00:02"
 }
 }

How to display the IP Address for the “devopsroles” container as below:

$ sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' devopsroles

Below is the presented output:

172.17.0.2

For example, display IP, Image, Name, Host Config, and status for containers as below:

$ sudo docker inspect -f 'Image:[{{ .Config.Image}}] Name:{{.Name}} HostConfig:{{ .HostConfig.Binds}} Status:{{ .State.Status}} IP_Container:{{ .NetworkSettings.IPAddress }} ' 055772699b49 | cat -n

The output below:

1 Image:[devopsroles/nginx:v2] Name:/devopsroles HostConfig:[/home/huupv/project/nginx/devopsroles:/var/www/html/devopsroles:ro] Status:running IP_Container:172.17.0.2

An alternative approach is as follows:

$ sudo docker inspect -f 'Image:[{{ .Config.Image}}] Name:{{.Name}} HostConfig:{{ .HostConfig.Binds}} Status:{{ .State.Status}} IP_Container:{{ .NetworkSettings.IPAddress }} ' $(sudo docker ps -a -q) | cat -n

How to display all containers, including non-running

$ sudo docker ps -a -q

The output below:

055772699b49

Conclusion

Throughout the article, we guide you on inspecting containers effectively. Learn how to determine crucial container details such as status, volume, and networking. We’ll continually update this post with useful commands for your reference. Thank you for exploring the DevopsRoles page – we appreciate your readership!

Docker nginx static website

Docker Nginx static website. How to use Docker to test a static website. I’m used Dockerfile to build and test as a local web development environment.

To create a directory for our Nginx Dockerfile

$ mkdir nginx
$ cd nginx
$ mkdir devopsroles
$ touch devopsroles/index.html
$ touch Dockerfile

The content our Dockerfile

FROM ubuntu:14.04
MAINTAINER PHAN VAN HUU <pvhuu90@gmail.com>, My blogs:devopsroles.com
RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html/devopsroles
#ADD devopsroles/index.html /var/www/html/devopsroles/
ADD devopsroles.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

The content of our devopsroles.conf file as below

server {
listen 0.0.0.0:80;
server_name _;
root /var/www/html/devopsroles;
index index.html index.htm;
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}

File configure nginx.conf as below

user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;
events {
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
}

To create our index.html file as below:

$ cat devopsroles/index.html

The content as below:

<head>
<title>Test website</title>
</head>
<body>
<h1>Hello! My name HuuPV</h1>
</body>

Building our new Nginx image

$ sudo docker build -t devopsroles/nginx:v2 .
$ sudo docker run -d -p 8080:80 --name devopsroles -v /home/huupv/project/nginx/devopsroles:/var/www/html/devopsroles:ro devopsroles/nginx:v2 nginx

The result, Docker Nginx static website

To use docker images command as below:

$ sudo docker images

The output devopsroles container as below:

[sudo] password for huupv: 
REPOSITORY TAG IMAGE ID CREATED SIZE
devopsroles/nginx v2 8598ffebf6ae 10 minutes ago 305.1 MB

To use docker ps command as below:

$ sudo docker ps

The output below

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ebab6b61682b devopsroles/nginx:v2 "nginx" 8 minutes ago Up 8 minutes 0.0.0.0:8080->80/tcp devopsroles

To use curl command test docker Nginx static website

$ curl http://localhost:8080

Conclusion

To deploy simple static website use docker. Docker Nginx static website uses Dockerfile to build as the container. Thank you for reading the DevopsRoles page!

Docker build image from Dockerfile

Introduction

Creating our own Docker images starts with a Dockerfile. Using the docker build command, we can easily transform the instructions in the Dockerfile into a new image. Wondering How to Docker build image from Dockerfile? Let’s dive in!

The first images built with Docker

To create a directory and a Dockerfile, the sample as below:

$ mkdir project
$ cd project
$ touch Dockerfile

The example Docker build the image from Dockerfile

$ vim Dockerfile

The content a Dockerfile as below:

 # CentOS 7 Dockerfile
 # Docker build:
 # sudo docker build -t devopsroles/centos:latest .
 # Docker create:
 # sudo docker create -it --name centos -h centos devopsroles/centos
 # Docker start:
 # sudo docker start centos
 # Connect with bash
 # sudo docker exec -it centos bash
 FROM centos:latest
 MAINTAINER PHAN VAN HUU <pvhuu90@gmail.com> My blogs: devopsroles.com
 #Update centos
 RUN yum update -y && yum upgrade -y
 RUN yum -y install git curl net-tools wget vim
 # Install EPEL Repository
 RUN yum install -y epel-release
 # Clean CentOS 7
 RUN yum clean all
 # Set the environment variables
 ENV HOME /root
 # Working directory
 WORKDIR /root
 # Default command
 CMD ["bash"]

Docker build image from Dockerfile

Running the Dockerfile use Docker build commands

$ cd project
$ sudo docker build -t .
$ sudo docker build -t devopsroles/centos:latest .
$ sudo docker create -it --name centos -h centos devopsroles/centos

The result, Docker images devopsroles/centos as below:

$ sudo docker images

The output, docker images as below:

 REPOSITORY TAG IMAGE ID CREATED SIZE
 devopsroles/centos latest c9ef43af9836 2 minutes ago 428.7 MB

The docker start container centos

$ sudo docker start centos
$ sudo docker ps

The output below:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 2d1a8e53668f devopsroles/centos "bash" 43 seconds ago Up 4 seconds centos

The connect container bash

$ sudo docker exec -it centos bash

The output below:

[root@centos ~]# ifconfig
 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
 inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link>
 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
 RX packets 8 bytes 648 (648.0 B)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 8 bytes 648 (648.0 B)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
 inet 127.0.0.1 netmask 255.0.0.0
 inet6 ::1 prefixlen 128 scopeid 0x10<host>
 loop txqueuelen 1 (Local Loopback)
 RX packets 0 bytes 0 (0.0 B)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 0 bytes 0 (0.0 B)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@centos ~]# ping 8.8.8.8
 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
 64 bytes from 8.8.8.8: icmp_seq=1 ttl=127 time=68.7 ms
 64 bytes from 8.8.8.8: icmp_seq=2 ttl=127 time=69.1 ms
 ^C
 --- 8.8.8.8 ping statistics ---
 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
 rtt min/avg/max/mdev = 68.720/68.945/69.170/0.225 ms

Or test ping 8.8.8.8 from docker run command as below

$ sudo docker run centos ping 8.8.8.8 -c4

The output below:

 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
 64 bytes from 8.8.8.8: icmp_seq=1 ttl=127 time=40.6 ms
 64 bytes from 8.8.8.8: icmp_seq=2 ttl=127 time=41.7 ms
 64 bytes from 8.8.8.8: icmp_seq=3 ttl=127 time=42.1 ms
 64 bytes from 8.8.8.8: icmp_seq=4 ttl=127 time=40.1 ms

--- 8.8.8.8 ping statistics ---
 4 packets transmitted, 4 received, 0% packet loss, time 3005ms
 rtt min/avg/max/mdev = 40.124/41.184/42.196/0.832 ms

Conclusion

Docker build image from Dockerfile. You can update and edit package from Dockerfile file. For example to install Nginx, PHP, MySQL package in Dockerfile file. Thank you for reading the DevopsRoles page!

Docker remove none images

While you build docker images, it is many untagged images as <none> images. In my tutorial, Docker remove none images or How to stop all CONTAINER ID.

Docker remove none images

To display <none> images:

[huupv@docker nginx-reverse]$ sudo docker images | egrep "^<none>"

The output below:

<none> <none> 55c87cfec131 9 minutes ago 196.6 MB
<none> <none> 8bdd100ab20d 11 minutes ago 196.6 MB
<none> <none> bddbdbcfac80 55 minutes ago 396.5 MB
<none> <none> e8aa668e28d0 19 hours ago 396.5 MB

Docker remove all <none> images

[huupv@docker nginx-reverse]$ sudo docker images | egrep "^<none>" | awk '{print $3}' | xargs sudo docker rmi -f

How to stop all CONTAINER ID

To display all container ID

[huupv@docker ~]$ sudo docker ps | egrep -v "CONTAINER ID" | awk '{print $1}'

The output below:

24e65acc6d21
24da33d6e0d5
09488028a7a9
b3b5926dcb82

To stop all container ID

[huupv@docker ~]$ sudo docker ps | egrep -v "CONTAINER ID" | awk '{print $1}' | xargs sudo docker stop

Thank you for reading the DevopsRoles page!

How to install docker compose

In this tutorial, How to install docker compose on centos. First of all, installed docker on your system. In this preview, Install docker and learn containers on Centos.

What is Docker compose?

Docker compose is tool orchestration manage containers, defining and running multi-container Docker applications, it’s working multiple containers docker application and use the compose file in the YAML format.

How to install docker compose on Centos

Install docker compose on centos 6

The docker compose the latest version, To check the https://github.com/docker/compose/releases

# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

To check docker compose version:

# docker-compose -v

The screen output terminal:

docker-compose version 1.16.1, build 6d1ac21

Install docker compose on centos 7

To install docker compose

# yum install -y python-pip
# pip install docker-compose

To upgrade all the python packages on Centos 7

# yum upgrade python*

The screen output terminal:

docker-compose version 1.16.1, build 6d1ac21

Testing Docker Compose

To create a new directory and move into it

 $ mkdir hello-world
 $ cd hello-world

To create a new YAML file

$ vim docker-compose.yml

The content file docker-compose.yml as below

 devopsroles-test:
 image: hello-world

To save and exit

To rune the container

$ sudo docker-compose up

The screen output terminal:

 $ docker-compose up
 Pulling devopsroles-test (hello-world:latest)...
 latest: Pulling from hello-world

3619f633e2e6: Pulling fs layer
 3619f633e2e6: Downloading [==================================================>] 3619f633e2e6: Extracting [==================================================>] 93619f633e2e6: Extracting [==================================================>] 93619f633e2e6: Pull complete
 bef02f2f6467: Extracting [==================================================>] bef02f2f6467: Extracting [==================================================>] bef02f2f6467: Pull complete
 Digest: sha256:a69dda95faa021234ab2d92118e9539d0175b46587803e32435ea2e534f2db06
 Status: Downloaded newer image for hello-world:latest
 Creating helloworld_devopsroles-test_1...
 Attaching to helloworld_devopsroles-test_1
 devopsroles-test_1 |
 devopsroles-test_1 | Hello from Docker!
 devopsroles-test_1 | This message shows that your installation appears to be working correctly.
 devopsroles-test_1 |
 devopsroles-test_1 | To generate this message, Docker took the following steps:
 devopsroles-test_1 | 1. The Docker client contacted the Docker daemon.
 devopsroles-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 devopsroles-test_1 | 3. The Docker daemon created a new container from that image which runs the
 devopsroles-test_1 | executable that produces the output you are currently reading.
 devopsroles-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
 devopsroles-test_1 | to your terminal.
 devopsroles-test_1 |
 devopsroles-test_1 | To try something more ambitious, you can run an Ubuntu container with:
 devopsroles-test_1 | $ docker run -it ubuntu bash
 devopsroles-test_1 |
 devopsroles-test_1 | Share images, automate workflows, and more with a free Docker ID:
 devopsroles-test_1 | https://cloud.docker.com/
 devopsroles-test_1 |
 devopsroles-test_1 | For more examples and ideas, visit:
 devopsroles-test_1 | https://docs.docker.com/engine/userguide/
 devopsroles-test_1 |

Conclusion

Through the article, you can use How to install docker compose as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Docker commands line reference

In this tutorial, List all useful commands for docker. How to restart one or more containers and stop one or more containers and so forth. The description useful Docker commands line reference as the link below

What is Docker?

  • Docker is a platform for developers and sysadmins to build, ship and run application”. It’s allow run containers and A container running Application. It’s dependencies on the host operating system.
  • Docker is OS level Virtualization.

All containers are started based on Docker image. To find existing images at https://hub.docker.com/

Docker commands line reference

To use command find an image

$ docker search <name>

To run a container based on a Docker Image. The default, to rune command in the foreground. To rune in the background with option -d. Docker to rune latest version available.

$ docker run <options> <image_name>

Find out running containers

$ docker ps

More details running a container

$ docker inspect <name-container>|<container-id>

Providing logs details running a container

$ docker logs <name-container>|<container-id>

To access a container with <host-port>:<containers-port>

$ docker run -d --name <name-container> -p <host-port>:<containers-port> <Image>

Persisting Data for containers

$ docker run -d --name <name-container> -v <host-dir>:<container-dir> <Image>

accessing to a bash shell inside of a container

$ docker run -it <name-container> bash

List all the images on the host

$ docker images

To use docker build command to build the image

$ docker build -t <name> .

Docker Ignoring Files during Build, to exclude sensitive files during build the image.

$ echo file_name.txt >> .dockerignore

To create Data Container

$ docker create -v /config --name dataContainers busybox

Copy files to data containers

$ docker cp config.conf dataContainers:/config/

Mount Volumes From data containers

$ docker run --volumes-from dataContainers ubuntu ls /config

To Export / Import for data Containers

 $ docker export dataContainer > dataContainer.tar
 $ docker import dataContainer.tar

Docker communicating between containers

$ docker run --link <container-name|id>:<alias> New_container

How to get quick stats on Docker containers

$ docker stats

Conclusion

Through the article, you can use the Docker commands line reference as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

git command line for beginners

In this tutorial, the Git command line is for beginners, Git server is a repository. I use the command line for my project.

Git is a powerful version control system used for tracking changes in software projects. Here are some basic Git commands for beginners:

Git command line for beginners

Git global setup

 git config --global user.name "PHAN VAN HUU"
 git config --global user.email "pvhuu90@gmail.com"

To create a new repository

 cd BashScripts
 git clone https://gitlab.com/huupv/DevopsSkills.git
 touch README.md
 git add README.md
 git commit -m "add README"
 git push -u origin master

Existing my folder

 cd BashScripts
 git init
 git remote add origin https://gitlab.com/huupv/DevopsSkills.git
 git add .
 git commit -m "Initial commit"
 git push -u origin master -f

Existing git my repository

 cd BashScripts
 git remote rename origin old-origin
 git remote add origin https://gitlab.com/huupv/DevopsSkills.git
 git push -u origin --all
 git push -u origin --tags

I explained the commands below

Git current state

How to list which (unstaged) files have changed, Shows the current status of the repository, including modified and untracked files.

git status

How to list (unstaged) changes to files: (Shows the differences between the working directory and the staging area)

git diff

How to list recent commits: (Displays the commit history of the repository)

git log

Tell me who you are:

git config --global user.name "PHAN VAN HUU"
git config --global user.email "pvhuu90@gmail.com"

To create a new local repository:

git init

To check out a repository

To create a working copy of a local repository

git clone https://gitlab.com/huupv/DevopsSkills.git

For remote server

git clone huupv@host:/path/to/repository

How to add files:

git add File_Name

To commit change but not yet to the remote repository:

git commit -m "Add file to repository commit"

To commit any file with git add:

git commit -a

How to “push” send changes to the master branch of your remote repository:

git push origin master

How to connect to a remote repository:

git remote add origin https://gitlab.com/huupv/BashScripts.git

How to list all currently configured remote repositories:

git remote -v

For Git branches

To create a new branch and switch to it:

git checkout -b <branchname>

To switch from one branch to another:

git checkout <branchname>

To list all the branches in your repository:

git branch

To delete the branch:

git branch -d <branchname>

To push all branches to your remote repository:

git push --all origin

To delete a branch on your remote repository:

git push origin :<branchname>

How to fetch and merge changes on the remote server to your working directory:

git pull

The conclusion

Thought the article, To help you understand the git command line. Thanks for reading my “git command line” post. I’m updating more useful commands line for Git. Thank you for reading the DevopsRoles page!

How to Gitlab SSH key pair

Introduction

In this guide, We’ll explore how to use gitlab SSH keys for Git operations in your projects. Learn the step-by-step process to generate an SSH key pair for GitLab and integrate it with your account.

To effectively use GitLab via SSH, you must create an SSH key pair and link the public key with your GitLab account.

How to do Gitlab SSH key pair

Open a terminal on your Linux system.

If you do not have an SSH key pair, then the error message is as below:

$ cat ~huupv/.ssh/id_rsa.pub
 cat: /home/huupv/.ssh/id_rsa.pub: No such file or directory

To generate a new ssh key pair by running the following command

$ ssh-keygen -t rsa -C "huupv@devopsroles.com" -b 4096

Replace “huupv@devopsroles.com” with your actual email address associated with your GitLab account. You can press Enter to accept the default file path and passphrase (or set a passphrase for added security).

The output Gitlab SSH key pair is below

Generating public/private rsa key pair.
 Enter file in which to save the key (/home/huupv/.ssh/id_rsa):
 Created directory '/home/huupv/.ssh'.
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /home/huupv/.ssh/id_rsa.
 Your public key has been saved in /home/huupv/.ssh/id_rsa.pub.
 The key fingerprint is:
 2a:16:6d:94:35:a2:02:db:2c:ce:fb:4f:79:56:bf:0b huupv@devopsroles.com
 The key's randomart image is:
 +--[ RSA 4096]----+

To generate 2 files as below:

Private key: id_rsa
Public key: id_rsa.pub

I already have ssh key pair to create on my laptop.

$ cat ~/.ssh/id_rsa.pub

The content public key id_rsa.pub is as below:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAgEAwEts938rLe7B1KKgYYBrhCMHFzeumk1IaoVo3rD48qVCig8fCHvp+W3Z2UzHHS9iT1IscXi0Bxq+/fTO9cbB5EznGTIW3I7y26yzIq/6iv3I/biYMmi6EiWCd2Ed2188uZ9aR0+gN4QIAmU8Grh91eaEy04d9H67GF2UT3lqx3PEi7v7aIH6FdkCdOp2YIE25UTuJoMoZ3kjKo02tGP7y/PUw8lbm/ezvYFLBN5l5DfmebDwSBNrR/K84mLWGx3L/bB9XnkEZRSh2vi+YFQ5FMomJA8+RHzfUS5zV3tI8VFDe1bGvcpYxKLKGc1bfKBZSYsOpsKgOofBcENaOJ8l0xCLWMFbJkAKcLEFuwBpLTx75WF7MEaxBnEIWekhhBR5hC+cR2fKEYuZaOYmpBfZUGnIDadad83+3qOQs+50/11AoinHCBOEphWRbn6pk0JP+pyTDsRKEmZ4KNOJt/WNwfVMnCVKb/5wcwCrmQ8ztWilcjT0UdMYUoH4u2IidWV5F38vAdG+LNH5dmKr2pcI7CdgZSXQqppkrvcqMLeH2BprlxuJW8qLc1hYV6HhWYbJc3EX28a6mZfohXLQAgN8HVpBaQ9UAZ33MiDPUhHeIW1OeXSXQHgH5t0CjXqzg3G9/vM7qOUr/27kN+D0ExBPENQf7hZ5Aw24fJl/PLQHAHs= huupv@devopsroles.com

Copy and paste the content id_rsa.pub to the Profile Settings in the Gitlab server web interface. ( step by step as below)

Copy the ssh key to GitLab

Copy the contents of the public key by running the following command: cat ~/.ssh/id_rsa.pub

  1. Copy the entire public key output from the terminal.
  2. Now, log in to your GitLab account using a web browser.
  3. In the top right corner, click on your profile picture and select “Settings.”
  4. From the left-hand side menu, click on “SSH Keys.”
  5. Paste the copied public key into the “Key” field.
  6. Optionally, give a recognizable “Title” to the SSH key (e.g., “My Linux Workstation”).
  7. Click on the “Add key” button to save the SSH key to your GitLab account.
  8. The SSH key is now added to your GitLab account, and you can use SSH to interact with GitLab repositories.

To test the SSH key connection

You can run the following command in the terminal:

Conclusion

Throughout this article, we’ll guide you on how to set up and use an SSH key pair for your project. You’ll learn the straightforward steps to generate an SSH key pair that secures your account. This essential setup enhances the security of your project by ensuring that only authorized users can access it. Follow along to easily create and implement your SSH keys.

You are now ready to use your GitLab SSH key for secure interactions with your repositories. Thank you for reading the DevopsRoles page!

Setting Up a Vagrant Centos box example: A Practical

Introduction

Explore the practical steps to set up a Vagrant CentOS box example in this detailed guide. Whether you’re working with CentOS 6 or 7, this tutorial provides clear instructions and essential tips for using Vagrant to deploy CentOS virtual machines effectively.

In this tutorial, I deployed a centos VM from Vagrantfiles. For example, the Vagrant Centos box example for Centos 6 and Centos 7. To use Vagrant, Virtualbox, and Centos.

The first is to create a Project folder as below:

$ mkdir vagrant_centos
$ cd vagrant_centos
$ vagrant init

To create a vagrant SSH key

[huupv@huupv example01]$ ssh-keygen

The Private key and public key in /home/huupv/project/keys/.ssh

To configure the vagrant SSH key in Vagrantfiles

config.ssh.insert_key = false
config.vm.boot_timeout = 800
config.ssh.private_key_path = ["keys/.ssh/vagrant_rsa", "~/.vagrant.d/insecure_private_key"]
config.vm.provision "file", source: "keys/.ssh/vagrant_rsa.pub", destination: "~/.ssh/authorized_keys"

To configure a vagrant box in Vagrantfiles

For centos 6 or Centos 7

 config.vm.box = “centos/6”
 config.vm.box = “centos/7”

Modifying Vagrant box memory and CPU settings

# Set VM memory size
vb.customize ["modifyvm", :id, "--memory", "512"]
# Set VM cpu size
config.vm.customize ["modifyvm", :id, "--cpus", 2]

Configure Forwarded port network

config.vm.network "forwarded_port", guest: 80, host: 8888
config.vm.network :public_network, :bridge => "eth1", :auto_config => false

To set VM hostname for Guest VM

#Set VM hostname
config.vm.hostname = "Server01"

To install the packages necessary for the Vagrant Centos box

config.vm.provision :shell, path: "VAGRANT_ENV/bootstrap.sh"

Vagrant Centos box example in a Vagrantfile.

VAGRANTFILE_API_VERSION = "2"
 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 config.vm.box = "centos/6"
 config.vm.hostname = "Server01"
 config.ssh.insert_key = false
 config.vm.provision :shell, path: "VAGRANT_ENV/bootstrap.sh"
 # Create a private network, which allows host-only access to the machine
 # using a specific IP.
 # config.vm.network :private_network, ip: "192.168.33.10"
 config.vm.network "forwarded_port", guest: 80, host: 8888
 config.vm.network :public_network, :bridge => "eth1", :auto_config => false
 # Share an additional folder to the guest VM
 # config.vm.synced_folder ".", "/vagrant", disabled: true
 # config.vm.synced_folder ".", "/home/vagrant/provision", type: "rsync"
 config.vm.provider :virtualbox do |vb|
 # Set VM memory size
 vb.customize ["modifyvm", :id, "--memory", "512"]
 # these 2 commands massively speed up DNS resolution, which means outbound
 # connections don't take forever (eg the WP admin dashboard and update page)
 vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
 vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
 end
 end

To run vagrant up a Virtual Machine, to start guest VM

[huupv@huupv example01]$ vagrant up

Conclusion

This guide demystifies the process of deploying CentOS using Vagrant, making it accessible for developers and IT professionals alike. With these insights, you’re now equipped to streamline your development and testing environments efficiently.

Thought the article, you can use the Vagrant Centos box example as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Devops Tutorial

Exit mobile version