Lab 06 - Docker

Docker

Installation

May not be required in the laboratory.

  1. Remove older versions of Docker:

    sudo apt remove docker docker-engine docker.io containerd runc
  2. Update and install required packages:

    sudo apt update
    
    sudo apt install ca-certificates curl gnupg lsb-release
  3. Add Docker’s official GPG key:

    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
  4. Set up the stable Docker repository:

    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
    $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
  5. Install Docker Engine, CLI, and Containerd:

    sudo apt update
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  6. Verify that Docker Engine is installed correctly by running the hello-world image:

        sudo docker run hello-world
  7. (Optional) To run Docker commands without sudo, create the docker group and add your user to it:

    sudo groupadd docker
    sudo usermod -aG docker $USER
    newgrp docker   

    You can test if the above step was successful by running:

    docker run hello-world

    ^ Notice that you don’t need sudo in front of the command.

The above steps and further documentation are also available here.

Docker Commands

List Docker images

docker images

List all Docker containers

docker ps -a

Start a container

docker start <container_name>

Run an interactive bash shell inside a container

docker exec -it <container_name> bash

Create a new container and run an interactive bash shell inside it

docker run -it <image_name> bash

Copy a file from the host to a container

docker cp path/to/file/on/host <container_name>:path/to/file/in/container

Copy a file from a container to the host

docker cp <container_name>:path/to/file/in/container path/to/file/on/host

List volumes

docker volume ls

Create a volume

docker volume create my_volume

Remove a volume

docker volume rm my_volume

🛠🔥 Task 1 🛠🔥

Read Docker’s get started (10 parts in total).

🛠🔥 Task 2 🛠🔥

Containerize the application from the previous class. The application should use bind mounts to write to a file located in the host’s filesystem.

Run the application and check the logs. Try enabling autostart of your container with system startup.

🛠🔥 Task 3 🛠🔥

Instead of mounting a host directory, you can use a Docker volume. Perform the following steps:

Think about whether it is still possible to read the text file you created after removing the container.
After thinking, check in the console

The following command will automatically remove the container after running and reading the text file:

docker run --rm -v my_volume:/data ubuntu cat /data/hello.txt