Docker Setup Lab - Zacham17/my-tech-journal GitHub Wiki

Summary

In this lab I set up a virtual machine to install docker on and perform containerization. Docker is a containerization software

Installing Docker on Ubuntu

  • Update your existing list of packages using sudo apt update
  • Install prerequisite packages which let apt use packages over HTTPS using the command sudo apt install apt-transport-https ca-certificates curl software-properties-common
  • Add the GPG key for the official Docker repository to your system using the command curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • Add the Docker repository to APT sources using the command sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
  • Make sure you are about to install from the Docker repo instead of the default Ubuntu repo using the command apt-cache policy docker-ce
  • Now you can install Docker using the command sudo apt install docker-ce
  • Docker should now be running. You can check using the command sudo systemctl status docker

Adding a User to the Docker Group

  • There are docker commands that can now be run now that you have docker installed
  • To run docker commands you must be a sudo user or in the docker group.
  • To add a user to the docker group, use the command, ```sudo usermod -aG docker ${USER}`` and replace ${USER} with the user you want to add to the group.
  • Apply the new group membership using the command su - ${USER} and replace ${USER} with the user you added to the group.

Using the Docker Command

Here is the syntax for the docker command: docker [option] [command] [arguments] To view all available subcommands, you can type docker

Here is a list of docker subcommands:

Output
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes
  • You can run the docker Hello-world application using the command docker run hello-world

Install Docker-Compose

  • Download the 1.27.4 release and save the executable file at /usr/local/bin/docker-compose using the command, ```sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose``
  • Set permissions on the file so that it is executable using the command sudo chmod +x /usr/local/bin/docker-compose
  • Run docker-compose --version to verify the installation was successful

Practice using Docker

  • Use the following command to pull down and Arch Linux Based docker image, invoke it in a container, and run /bin/echo "HELLO SYS265 SNOWY DAYS": docker run --rm archlinux:latest /bin/echo "HELLO SYS265 SNOWY DAYS"
  • You can see the downloaded docker images using the command docker images
  • Pull down the image, application and dependencies associated with a simple python web application using the command, docker run -d -P training/webapp python app.py
    • The -d stands for detached and it starts the container in detached mode, which means that the container started in detached mode will exit when the root process used to run the container exits.
    • The -P option publishes all exposed ports to the host interfaces.

Docker Networking

  • Use the command docker ps to see container information
  • You should see a port number in the output. Remember this port.
  • You can access your container in a browser, by typing the ip address of the host system followed by the port number from the previous step to view the docker container program. Ex: http://1.2.3.4:4321

Stopping the Docker Testapp:

  • In the output for the docker ps command, you should see a name.
  • To stop the Docker Testapp, use the command docker stop NAME and replace NAME with the name in the output from docker ps

Dockerized Wordpress:

  • Create an empty project directory. I did this using the command mkdir my_wordpress. Furture commands on this page will reflect how I named the directory.
  • Change into your project directory using cd my_wordpress
  • Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with volume mounts for data persistence. This can be done simply by using vi docker-compose.yml
  • Configure the file as follows:
version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}
  • Note: WordPress Multisite works only on ports 80 and 443.
  • To begin building the project, run docker-compose up -d from your project directory to run docker-compose up in detached mode, pulls the needed Docker images, and starts the WordPress and database containers.
  • Now you may access the WordPress multisite from a browser. Do this by typing the hostname of the host system followed by the port number used by WordPress multisite, which should be 8000. Ex. http://localhost:8000
  • Follow the on-screen directions and fill out the appropriate information to install WordPress.

WordPress Multisite Shutdown and cleanup

  • The command docker-compose down removes the containers and default network, but preserves your WordPress database.
  • The command docker-compose down --volumes removes the containers, default network, and the WordPress database.