Course followed: https://www.youtube.com/watch?v=fqMOX6JJhGo&ab_channel=freeCodeCamp.org
docker pull ubuntuThis will pull the latest image of ubuntu to the machine. Similar to git clone.
docker run -t -d --name test_ubuntu ubunturun will create the container -t assigns a terminal -d runs the container in the background --name assigns a name to the container
run -t -d -v D:\Docker\hello_docker:/mnt/hellodocker --name ubuntu_mapped ubuntu
-v Volume mapping (local_folder:container_folder)
docker run -e APP_COLOR=blue docker_container_nameHad you written a python script with a variable called color which you had set to blue, you could change that variable at container creation using an enviromental variable. In python your variable would be set as:
import os
color = os.environ.get('APP_COLOR')-e will pass the value in as the variable content.
docker run ubuntuCreates a defualt bridged network usually on the 172.x.x.x range. This allows all containers to talk with each other.
docker run ubuntu --network=noneDisables network access on that container
docker run ubuntu --network=hostThe host option will use all the same network configurations as the host machine so ports from the host machine will link directly from the host to the container. This will stop multiple containers being able to use the same port.
docker network create \
--driver bridge \
--subnet 182.18.0.0/16
custom-isolated-networkCreate a custom network named "custom-isolated-network" that uses the bridged driver allowing all containers to talk to each other on the same 182.18.x.x range
docker inspect container_nameThis will give information such as IP address of the container under "NetworkSettings"
Docker containers can communicate with each other using their host name / container name. This is the preferred method as an IP address can change on boot up.
NOTE: If using docker-compose & version 2+ yml format, linking is not required as all containers will use a default bridged network.
For a container to use another containers host name to communicate between each other, they need to be told about the container at creation.
docker run -d --name ubuntu_server --link mysql:mysql --link apache:apache ubuntuThis command will create a container with the name "ubuntu_server" using the "ubuntu" image. This container will be linked to the mysql and apache containers.
Docker DNS Default IP: 127.0.0.11
NOTE: -v is the old way of working with mounting and the --mount option is the new preferred way:
docker run \
--mount type=bind, source=/data/mysql, target=/var/lib/mysql mysqldocker volume create data_volume_nameThis will allow you to create a docker volume that you can assign to docker containers.
docker run -v data_volume_name:/var/lib/mysql mysqlSupplying a volume name will assign it to the location supplied after the colon. If the volume name does not exist it will be created.
docker run -v /var/mysql:/var/lib/mysql mysqlBy supplying a local folder instead of a docker volume it will bind the supplied host directory to the containers supplied directory (host:container).
Docker compose allows the creation of a "config" file to create the containers. Docker is a quick way of bringing up an image on the command line, however if there are a lot of options being added on the command line it can be confusing. This is where creating a config file and using docker compose is the better choice.
By using docker compose we are able to create a single yml file that can include networking, container links, port configuration and more all in one easily readable place.
There are multiple versions of docker-compose which can be distinguised from the format of the yaml file. Version 1 you will see the container names as the root of each section and has very limited config options. Networking in Version 1 requires --link options.
Version 2 needs to start with "version: 2" and the container names will be under a "services:" section.
Networking in Version 1 requires --link options network the containers together while in Version 2+ there is a default bridged network allowing all containers to talk to each other and the links section of docker-compose.yml can be removed.
Version 3 continues from Version 2 with the same features listed above but will start with "version: 3" and has additional options such as support for docker swarm.
Assuming we have a situation where we want 5 different containers set up. To do this with regular docker we would need to use docker run 5 times like this:
docker run -d --name redis redisdocker run -d --name db postgres:9.4docker run -d --name vote -p 5000:80 --link redis:redis voting-appdocker run -d --name result -p 5001:80 --link db:db result-appdocker run -d --name worker --link db:db --link redis:redis workerTo create this in a "docker-compose.yml" file would look like this (This example is docker-compose.yml v1 format):
redis:
image: redis
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
links:
- redis
result:
image: result-app
ports:
- 5001:80
links:
- db
worker:
image: worker
links:
- redis
- dbThen by running docker compose you can bring up all containers
docker-compose upNOTE: If using docker-compose & version 2+ yml format, linking is not required as all containers will use a default bridged network.
If you are not using an image that can be pulled from dockerhub or you have an unbuilt image (folder with docker file and files), you can replace "image" with "build" and supply the location to the folder with dockerfile to build the image at the time you bring this up with docker compose
version: 2
services:
redis:
image: redis
db:
image: postgres:9.4
vote:
build: ./vote_app_location
ports:
- 5000:80
links:
- redisversion: 2
services:
redis:
image: redis
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
depends_on:
- redisBy adding a "depends_on" option, we can make sure that the container will only start if the depended on container successfully starts.
Multiple networks can be set up with docker compose and assigned to specific containers allowing some networks to be front end facing and others back end facing or with no external network connections. This is done by adding a "networks:" section. In this example "vote" and "result" apps are websites so front facing for user access but also need to talk to back end databases, while db and redis only need a back end connection.
version: 2
services:
redis:
image: redis
networks:
- back-end
db:
image: postgres:9.4
networks:
- back-end
vote:
image: voting-app
networks:
- front-end
- back-end
result:
image: result
networks:
- front-end
- back-end
networks:
front-end:
back-end: