Skip to content

Instantly share code, notes, and snippets.

@dantejauregui
Created January 15, 2024 07:00
Show Gist options
  • Save dantejauregui/acf73f2b874cf977bce7828f465ddae7 to your computer and use it in GitHub Desktop.
Save dantejauregui/acf73f2b874cf977bce7828f465ddae7 to your computer and use it in GitHub Desktop.
Docker Volumes & Networking
Docker Volumes & Networking
2 Types of Mounts:
1.-Volume Mounting:
-When you use the command:
docker volume create “data_volume”
docker volume ls
-> docker volume inspect (Volume name)
-> it will create a Persistent Volume in the route: /var/lib/docker/volumes/data_volume
and then you use the command:
docker run -v data_volume:/var/lib/mysql mysql
-> you Mount the Volume inside the Docker Container (the “write-read layer” ), so with that the Data will persist event the Container is deleted
* this route in the container: /var/lib/mysql is where is stored the Mysql data
----------------------------------------------
2.-Bind Mounting:
is when we use this command:
docker run -v /data/mysql:/var/lib/mysql -d mysql
or in a Verbose and easy to read way the command is:
docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql -d mysql
docker run -d --mount type=volume,src=sample-volume,target=/usr/share/nginx/html mysql
=======================================================
Docker Default Networks
By default once docker is installed come w 3 networks: Bridge, none and Host. You can see it using: docker network ls
To see the default configuration of the BRIDGE network: docker network inspect bridge
And any container will automatically use the default Bridge network!
But also we can create other Bridge Networks using: docker network create --driver bridge (network name)
To see the network settings of each Container, use the command: docker inspect [image ID]
Docker has an embedded DNS, so instead of use the IP of a specific container, you use the “name of the container” instead.
and to attach a newly created network to the docker run command:
docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql-db --network (network name XYZ) mysql
---------------------------------------------
*** you can link 2 containers using the docker run parameter “--LINK (source container):(recipient container)”:
--link mysql-db:mysql-db
Don’t forget the 2 containers have to share and be in the same network!
for example:
docker run --network=(network name XYZ) -e DB_Host=mysql-db -e DB_Password=db_pass123 -p 38080:8080 --name webapp --link mysql-db:mysql-db -d kodekloud/simple-webapp-mysql
---------------------------------------------
para ver la lista de containers con sus respectivos IPs:
docker network inspect bridge | jq .[0].Containers
para desconectarse de una network spacifica:
docker network disconnect (Network name) (Container name)
Y
para conectarse a una network spacifica:
docker network connect (Network name) (Container name)
----------------------------------------------
Para eliminar container completamente sin dejar registros:
docker rm -f app-1
Docker Port-forwarding
docker run -p 80:80 (docker image)
docker run -v /root/app-data:/usr/share/nginx/html -p 80:80 --name sample-app nginx:alpine
====================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment