After the installation, you have to add your user to docker group to run docker without sudo permission
sudo usermod -aG docker $USER
FROM ubuntu:tag # Sets the image
MAINTAINER Denis Policastro <[email protected]>
ADD . . # Copy items from host to VM (All from dir to workdir)
COPY /src . # Files or dir
LABEL Description=”Descrição do projeto” # Metadata, D version
ENTRYPOINT [“usr/bin/apache2ctl”, “-D”, “FOREGROUND”] # Main process running on the container, if the process dies the container dies
CMD [“node”, “app.js”] # Params for the entrypoint, example if entrypoint is a bash, ls is a param
ENV TEST=”Test env” # Declare env vars
USER denis # Sets the user, default is root
WORKDIR /srv # Set working dir
VOLUME /host/dir /container/dir
docker run \
-d | -it \ #Default -d
[--publish | -p] \
[--memory] \
[--cpu-shares] \
[--rm] \
[--env | -e] \
[--network] \
[--net-alias] \
[--name] \
[-v] \
nginx
-
--publish | -p: - Specifies the exposed port
HOST_PORT:CONTAINER_PORT
-
--cpu-shares: - Sets the cpu share percent, i.e, if(100 = total) => 25 = 25%
512
-
--memory: - Specifies the max memory consumption for that container
512m
-
--rm: - Automatic remove container when exit
-
--environment | -e: - Sets environment variable
MYSQL_RANDOM_ROOT_PASSWORD=yes
-
--network: - Specifies the network
NETWORK_NAME
-
--net-alias: - Gives an alias for load balancing the containers inside the container network
ALIAS
-
--name: - Gives the container a name
CONTAINER_NAME
-
-v: - Bind a custom volume to the host from the container for persistent data
named_volume:/var/lib/mysql
./mount/to/bind:/var/lib/mysql
Execute the commands to see it working:
docker container run -d --network es_network --net-alias search elasticsearch:2
docker container run -d --network es_network --net-alias search elasticsearch:2
# Create another one for querying:
docker container run -it --network es_network centos:7 bash
nslookp search
curl -s search:9200
# Stopping
docker container top
# Inspecting
docker container inspect --format=’{{ .NetworkSettings }}’ CONTAINER_ID
docker container stats -> CPU, MEM
# Starting (--attach --interactive)
docker start --ai CONTAINER_NAME
# Enter container or execute command
docker exec [-it] CONTAINER_NAME {bash | command}
# Change runtime configuration:
docker update --help
# Limits CPU, RAM
docker run --memory 512m --cpu-shares 1024 --name nginx1 nginx:lastest
docker run --memory 512m --cpu-shares 512 --name nginx2 nginx:lastest
docker run --memory 512m --cpu-shares 512 --name nginx3 nginx:lastest
docker container -m 256m nginx
- Overlay: - Used by docker swarm to communicate between containers
- Bridge: - Used by containers to communicate network
Commands:
docker network ls
docker network inspect NETWORK_NAME
docker network create NETWORK_NAME
docker network connect NETWORK_NAME CONTAINER_NAME
docker network disconnect
Commands:
docker-compose -f docker-compose.yml {up|top|down|start|top|build} [-d, --build]
version: '2'
services:
drupal:
image: drupal-custom
build:
context: .
dockerfile: Dockerfile
ports:
- '8080:80'
volumes:
- drupal-modules:/var/www/html/modules
- drupal-profiles:/var/www/html/profiles
- drupal-sites:/var/www/html/sites
- drupal-themes:/var/www/html/themes
postgres:
image: postgres:9.6
#ports: We dont need ports here, because we're using the docker network
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=mypass
- POSTGRES_DB=postgres
volumes:
- drupal-data:/var/lib/postgresql/data
volumes:
drupal-modules:
drupal-profiles:
drupal-sites:
drupal-themes:
drupal-data:
Commands:
docker info
# Check if swarm is enabled
docker swarm init
# Initialize swarm’s master node
docker service create alpine ping 8.8.8.8
# Creates a service
docker service ls | docker service ps SERVICE_NAME
# Check service status or config
docker service update SERVICE_NAME --replicas 3
Create the overlay network, for the communication between swarm services:
docker network create --driver overlay mydrupal
Create the services, passing the --network:
docker service create --name drupal --network mydrupal -p 80:80 drupal
docker service create docker service create --name psql --network mydrupal -e POSTGRES_PASSWORD=mypass postgres
Changes service configuration dynamicly:
docker swarm update --help
# This command generates an output, that needs to be executed on worker nodes
docker swarm init --advertise-addr PUBLIC_IP
# Changes node role to manager
docker node update --role manager NODE_NAME
# Generates the output to join the service as a manager
docker swarm join-token manager