Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jazio/101f0f15d30b651048cbcd7460c01852 to your computer and use it in GitHub Desktop.
Save jazio/101f0f15d30b651048cbcd7460c01852 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Install Docker, Docker Compose and DDEV on Ubuntu/Linux Mint
# Notes: Installing Docker with snap resulted into faulted issues.
# Basics
#```````````````````````````
# Uninstall old versions of Docker
sudo apt-get remove docker docker-ce docker-engine \
docker.io containerd runc
# Install packages to get Docker on a secure protocol
sudo apt-get install apt-transport-https ca-certificates
sudo apt install curl gnupg-agent software-properties-common
# Import Docker GPG-key to ensure we have installed Docker packages from the authentic source
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Get the Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
# Install Docker (Community Edition) and docker-compose tool
sudo apt install docker-ce docker-compose
# Configure Docker to start on boot
sudo systemctl enable docker
sudo systemctl start docker
# Optionally, test docker with the official hello-world image fetched from hub.docker.com
# docker pull hello-world
# docker run -it hello
# Check if docker daemon/service is running.
systemctl status docker
# or
systemctl is-active docker
# Make sure you are belonging to the group docker.
# After that you need to log out and restart docker:
sudo groupadd docker
sudo usermod -ag docker $(whoami)
sudo service docker start
# List all the Docker CLI commands
docker
# Get a whole resume about the Docker installation
docker info
# Dockerfile
#```````````````````````````
# A .Dockerfile is a blueprint of a future Docker image.
# After writing the Dockerfile you tag it (= name it), then build an image based on it, then push the image to Docker Hub.
# https://hub.docker.com/
# Mapping ports in a Dockerfile
ports:
- "8080:80"
(Host:Guest)
# Mapping volumes in a Dockerfile
volumes:
- "local_directory:remote_directory"
volumes:
- type: volume
source: /data/mysql
target: /var/lib/mysql
# Create a tag for a new Dockerfile
docker build . -t vendor/name-tag
# Remote login to DockerHub
docker login
# Push the selected Dockerfile tagged to Dockerhub
docker push vendor/tag
# FROM instruction chooses the parent image for Docker.
# LABEL instruction creates labels.
# ENV instruction assigns environment variables.
# COPY instruction copies files or directories,
# WORKDIR instruction changes the current directory in Docker image.
# RUN instruction runs commands.
# EXPOSE instruction opens the port for communicating with the Docker container.
# CMD instruction runs commands like RUN, but the commands run when the Docker container launches.
# Pull the image from the Dockerhub
docker pull vendor/tag
# Run the docker container
sudo docker run -ip 5000:5000 sample_image:latest
# Images
#`````
#In Docker, everything is based on Images. An image is a combination of a file system and parameters.
#A Docker image is built up from a series of layers. Each layer represents an instruction in the image’s Dockerfile.
# Each layer except the very last one is read-only. Consider the following Dockerfile:
FROM ubuntu:15.04
COPY . /app
RUN make /app
CMD python /app/app.py
# Run an image, e.g. hello-world
docker run hello-world
# Build image from a certain Dockerfile
docker build -t web-custom-name /path/to/dockerfile/
# Get a list of existing images
docker images // docker image ls
# Get all the images but by its ID
docker image ls -q
# Run an instance of the former image
# and publish port 8080 in container
# to the port 8282 on host.
docker run -p 8282:8080 web-custom-name
# If the image (e.g. centos) isn't downloaded, then pull it from
# remote at DockerHub
docker run centos
# If only want download an image, without run
# a container
docker pull ubuntu
# Delete images by ID
docker rmi $(docker image ls -q)
# Remove unused images
docker image prune
# Containers
#`````
# Docker images and Containers. Docker containers are instances of Docker images, whether running or stopped. In fact, the major difference between Docker containers and images is that containers have a writable layer.
# Run a container but in detached mode and
# back to your prompt
docker run -d vendorexample/appexample
# Run a container with custom name.
docker run -d --name web-custom-name nginx:1.14-alpine
# Restart a Container
docker restart IDCONTAINER
# Run a container from the centOS image with bash and
# login in prompt
docker run -it centos bash
# Deploy a mysql database using the mysql image and
# name it mysql-db. Set the database password to
# use db_pass123. Lookup the mysql image on
# Docker Hub and identify the correct environment
# variable to use for setting the root password.
docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql-db mysql
# Run a container in background with a end of life
docker run -d centos sleep 100
# Run a container, mapping ports and mapping volumes and
# using a user from the container
docker run -p 80:8080 -v /locahost/folder:/container/folder \
-u root jenkins/jenkins
# Get a list of existing containers
docker ps
# List all containers showing it by its ID
docker ps -q
# Kill all containers running selected by ID
docker kill $(docker ps -q)
# Remove only a container
docker rm IDCONTAINER
# Remove all containers with status=exited
docker rm $(docker ps -q -f status=exited)
# Executes a command inside a running container
docker exec idcontainer unixcommand
# Connect to the Prompt of a Container
docker exec -it IDCONTAINER /bin/bash
# Connect to the Prompt of a Container as root
docker exec -ti -u root IDCONTAINER /bin/bash
# Copying files with Docker
# From Local to Remote Docker Container
docker cp db/dump.sql IDCONTAINER:/tmp/dump.sql
# From Remote Docker Container to local
docker cp IDCONTAINER:/tmp/dump_test.sql ./db
# Attach local standard output, input and error
# streams to a running container
docker attach IDCONTAINER
# Inspect all the info about a Docker Container
docker container inspect IDCONTAINER
# Show the log of a container
docker logs -f IDCONTAINER
# Tailing logs:
# Searching for 'error' (case - insensitive) in the
# last 1000 log lines of my jenkins (example)
# container adding the timestamp at the beginning
# of each line.
sudo docker logs -t --tail 1000 jenkins 2 >&1 \
| grep -i error
# Describe all the existing Docker Compose Networks
docker network ls
# Get all the info about an specific network
docker network inspect name_network
# Delete a network in your Docker Compose system
docker network rm name_network
# Remove unusued data and clean the Docker System
docker system prune -f
# Show stats about the running containers.
docker stats
# Same but with a formatted output.
docker stats --all --format "table \t\t"
# Fixing results in prompt.
docker ps -q | xargs docker stats --no-stream
# Docker Compose
#`````
# Run a multi-container application with Docker Compose. You run a set of containers.
docker-compose up -d
# Turn on the Docker Compose network
# but building images before starting containers
docker-compose up --build
# Stop a multi-container application with Docker Compose
docker-compose stop
# Kill and delete containers based in Docker Compose
docker-compose down
# Strem the container events for every container
# in a project
docker-compose events --json
# Connect to a container using its alias (no ID, no IP)
docker-compose exec ALIAS /bin/bash
# Example: executing drush cache rebuild in a Drupal Container
docker-compose exec web ./vendor/bin/drush cr
# Connecting to a Container (called mysql) as root by prompt
docker-compose exec -u root mysql /bin/bash
# Get the Docker container's log
docker-compose logs -t ALIAS
# Get the Docker container's log
# with direct connection in real time
docker-compose logs -t -f ALIAS
# Others
#```````````````````````````
# Docker Swarm: Deploy instances of application
# across docker host
docker stack deploy -c docker-compose.yml
# Remove ALL: stopped containers, all networks
# not used and all dangling images
docker system prune
# DDEV
#```````````````````````````
# Notes
# Ubuntu’s default firewall (UFW: Uncomplicated Firewall) denies all forwarding traffic by default, which is needed by docker.
# sudo nano /etc/default/ufw
# And change DEFAULT_FORWARD_POLICY="DROP" to DEFAULT_FORWARD_POLICY="ACCEPT"
# ddev quickstart with drupal
# for other quickstarts visit https://ddev.readthedocs.io/en/latest/users/cli-usage/#quickstart-guides
mkdir my-drupal8-site
cd my-drupal8-site
ddev config --project-type php
ddev composer create drupal-composer/drupal-project:8.x-dev --prefer-dist
# or
ddev composer create-project drupalcommerce/project-base mystore --stability dev
ddev config --project-type drupal8
ddev restart
# Successfully started my-drupal8-site
# Your project can be reached at: http://my-drupal8-site.ddev.site
# Git Clone Project and launch composer install
git clone https://github.com/randomuser/my-drupal8
cd my-drupal8
ddev composer install
# Check the status of the projects you work on
ddev list
# NAME TYPE LOCATION URL STATUS
# d8 php /var/www/html/d8 stopped
# projectname php /var/www/html/projectname stopped
# To start a specific project cd to that folder and run:
ddev start
# Configurations
# In every project folder there is a folder .ddev in which you find config.yml and docker-compose.yaml
# config.yml
# edit docroot: "path/to/project"
# Creating a CMS-specific settings file with
# DDEV credentials pre-populated
ddev config
# DDEV Commons
ddev start
ddev list
ddev describe [project-name]
# Using SSH in DDEV Containers
ddev ssh
# Executing Drush in DDEV Containers
ddev exec drush status
ddev exec drush cex
ddev exec drush site-install
ddev exec drush site-install standard \
--site-name='Drupal Site Install Test' \
--account-name=admin --account-pass=admin \
[email protected] -y
# Installing dependencies from a ddev container
ddev composer require drupal/devel
# Install a complete Drupal Site using ddev
# in a "single" instruction
mkdir NAMEPROJECT && cd NAMEPROJECT \
&& ddev config --project-type php \
--php-version 7.3 \
&& ddev composer create drupal-composer/drupal-project:8.x-dev \
--stability dev --no-interaction && ddev config \
--project-type drupal8 \
&& ddev exec drush site-install standard \
--site-name='NAMEPROJECT' \
--account-name=admin \
--account-pass=admin \
[email protected] -y \
&& ddev start \
&& sensible-browser http://NAMEPROJECT.ddev.local
# Get a list of projects using DDEV
ddev list
# Describe a project
ddev describe project-name
# Importing a database file (launch a prompt to set
# the location and values of the database dump)
ddev import-db
# Exporting a database
ddev export-db
# Importing files assets
ddev import-files
# Snapshotting your database
ddev snapshot // Same as in ddev stop --remove-data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment