Skip to content

Instantly share code, notes, and snippets.

@bikram20
Last active February 26, 2023 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bikram20/f8394e222612961fd4c689f4d77ebd47 to your computer and use it in GitHub Desktop.
Save bikram20/f8394e222612961fd4c689f4d77ebd47 to your computer and use it in GitHub Desktop.
DigitalOcean Docker 1-click Test App Setup
# Install a Docker 1-click droplet. Choose at least 4cpu/8gb, as we will run multiple applications
# Create a non-root user
sudo adduser ubuntu --disabled-password
sudo usermod -aG docker ubuntu
cp -r /root/.docker /home/ubuntu/
chown -R ubuntu:ubuntu /home/ubuntu/.docker
su - ubuntu
#################################################################################
# Create 2 ubuntu containers with named volumes
docker run -d --name test_ubuntu1 -v named_volume_1:/data1 ubuntu tail -f /dev/null
docker run -d --name test_ubuntu5 -v named_volume_5:/data1 ubuntu tail -f /dev/null
# Add sample data to named volumes
docker exec -i test_ubuntu1 sh -c 'echo "This is a sample file" >> /data1/sample.txt'
docker exec -i test_ubuntu1 sh -c 'echo "This is a sample file" >> /data1/sample2.txt'
docker exec -i test_ubuntu5 bash -c 'echo "This is a sample file" >> /data1/sample.txt'
docker exec -i test_ubuntu5 bash -c 'echo "This is a sample file" >> /data1/sample2.txt'
# Verify that sample files are created
docker exec -i test_ubuntu5 bash -c 'cat /data1/sample.txt'
#################################################################################
# Create a postgres db
docker run -d --name test_postgres -v named_volume_2:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword postgres
# verify it is running
docker ps
# Download sample db to the host and unzip it (to a tar file). Copy to the container /tmp
curl -o pagila.zip https://www.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip
unzip pagila.zip
docker cp dvdrental.tar test_postgres:/tmp
# import DB
docker exec -it test_postgres bash -c 'createdb -U postgres dvdrental'
docker exec -it test_postgres bash -c 'pg_restore -U postgres -d dvdrental < /tmp/dvdrental.tar'
# Verify the import
ubuntu@docker-host1:~$ docker exec -it test_postgres psql -U postgres dvdrental -c '\dt'
#################################################################################
# Install mongodb into another container
docker run -d --name test_mongodb -v named_volume_3:/data/db -v named_volume_311:/data/configdb mongo
# Download sample database
curl -o zips.json https://media.mongodb.org/zips.json
docker cp zips.json test_mongodb:/tmp
docker exec -it test_mongodb bash -c 'mongoimport --db mydb --collection zips --file /tmp/zips.json'
# Verify the import
docker exec -it test_mongodb bash -c 'mongosh mydb --eval --eval "db.zips.find()"'
#################################################################################
# Install mysql into a container
docker run -d --name test_mysql -v named_volume_4:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword mysql:latest
# Download and import the sample db
curl -o sakila-db.zip https://downloads.mysql.com/docs/sakila-db.zip
docker cp sakila-db/sakila-schema.sql test_mysql:/sakila-schema.sql
docker cp sakila-db/sakila-data.sql test_mysql:/sakila-data.sql
docker exec -it test_mysql mysql -u root -pmysecretpassword -e 'CREATE DATABASE sakila; USE sakila; SOURCE /sakila-schema.sql; SOURCE /sakila-data.sql;'
# verify the import
docker exec -it test_mysql mysql -u root -pmysecretpassword -e 'USE sakila; show tables;'
#################################################################################
@bikram20
Copy link
Author

docker-compose.yml to bring up all containers with 1 command

version: '3'

services:
test_ubuntu1:
image: ubuntu
command: tail -f /dev/null
volumes:
- named_volume_1:/data1

test_ubuntu5:
image: ubuntu
command: tail -f /dev/null
volumes:
- named_volume_5:/data1

test_postgres:
image: postgres
restart: always
volumes:
- named_volume_2:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: mysecretpassword

test_mongodb:
image: mongo
restart: always
volumes:
- named_volume_3:/data/db
- named_volume_311:/data/configdb

test_mysql:
image: mysql:latest
restart: always
volumes:
- named_volume_4:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mysecretpassword

volumes:
named_volume_1:
named_volume_5:
named_volume_2:
named_volume_3:
named_volume_311:
named_volume_4:

@bikram20
Copy link
Author

bikram20 commented Feb 26, 2023

Backing up volume example

VOLNAME=named_volume_1
FILENAME=vol1.tar.gz

if ! docker volume inspect --format '{{.Name}}' "$VOLNAME";
then
echo "Error: Volume $VOLNAME not found!"
exit 1
fi

if ! docker run --rm -v "$VOLNAME":/backup-volume -v "$(pwd)":/backup-dir busybox tar -zcvf /backup-dir/"$FILENAME" /backup-volume;
then
echo "Error: Failed to start busybox backup container"
fi

echo "Copied $VOLNAME into $FILENAME"

@bikram20
Copy link
Author

bikram20 commented Feb 26, 2023

Restoring volume

VOLNAME=named_volume_1
FILENAME=vol1.tar.gz

if ! docker run --rm -v "$VOLNAME":/backup-volume -v "$(pwd)":/backup-dir busybox tar -xvzf /backup-dir/"$FILENAME" -C /;
then
echo "Error: Failed to start busybox backup container"
fi

echo "Copied $VOLNAME into $FILENAME"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment