30 Oct 2020
Update and Install required packages for repositories over HTTPS
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common
Add the Docker repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
Install the Docker engine
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo docker run hello-world
Add user to groups
sudo usermod -aG ubridge $(whoami) sudo usermod -aG libvirt $(whoami) sudo usermod -aG kvm $(whoami) sudo usermod -aG wireshark $(whoami) sudo usermod -aG docker $(whoami)
Docker Compose required Docker to be installed, so make sure you have it installed and working before installing Compose.
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
Apply an execute permission
sudo chmod +x /usr/local/bin/docker-compose
Update and Install required packages then add the Ansible PPA
sudo apt-get update sudo apt-get install software-properties-common sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt-get install ansible -y
Pulling an image from the docker repository
docker pull debian
We can download images for use in our Docker environment using the Docker Hub, the example above will download the latest Debian image. We can download different versions by using supported tags such as:
docker pull debian:jessie
We can list our images by using the command.
Creating a Docker container
docker run -it debian
We can use the run command to create a new container, using the switches -i for interactive and -t for TTY (terminal).
There are many different options, use the Docker docs for more.
Entering a container
Using the exec command we can enter a running container
docker exec -it mycontainer /bin/bash
To list all active container processes use the command
To list all containers use an
docker ps -a
Changing the container - Saving
We can save a container as a new image using the command
docker commit mycontainer mynewcontainer:1.0
Stop and Start Docker containers
We can use their container ID or Names
docker stop 4afe090d56a8 docker start wizardly_tereshkova
We can prune images that are not being used by using the command
docker image prune
We can also remove an image by using the command, remember :jessie is the tag
docker image rm debian:jessie
To remove an Container use the following command:
docker rm <CONTAINER ID / NAME>
You can also query all containers and remove using that, be careful with this!
docker rm `docker ps -q -a`
Using a Dockerfile
A Dockerfile is a set of instructions used when building an image.
# Ubuntu FROM ubuntu # Update and Install net-tools, allowing use of the 'ifconfig' command RUN apt-get update && apt-get install net-tools -y
A quick example of how to a use a Dockerfile
make a directory for your container files.
move into that directory.
create your Dockerfile.
add the above example and save
build your image
docker build -t mycontainer:1.0 .
view your new image.
Building an Image
When building an image you need a Dockerfile and Context. A context is a location either a PATH on your local filesystem or a URL to a git repository.
This command will build from the current directory using the default Dockerfile called “Dockerfile”.
docker build .
Some common flags include:
using the -f flag we can point to a Dockerfile anywhere in your file system
docker build -f /home/user/Documents/Dockerfile-production .
using the -t flag save image using a repository and tag
docker build -t user/myapp:1.0 .
Docker Compose allows you to configure two or more containers that can work together, for example a web server with a SQL database with communication between them while still running in isolation.
A Docker Compose file can also state networks created and data stores or volumes.
By default a network is created between all containers within the docker-compose file.
Create a Docker-compose file
Make a docker-compose.yml file by using your preferred editor
Add the contents of a docker-compose, an extremely minimal example is:
version: "3.8" services: # creates a container called web web: # build is the location of the Dockerfile build: ./web # which image to use image: nginx # creates a container called db db: image: redis
What Version to use?
In the above sample there is a version number, this relates to your Docker Engine and Compose file format
docker version on your host machine and match your version number to the compose file format
Client: Docker Engine - Community Version: 19.03.13
|Compose file format||Docker Engine release|
Validate your YAML
Validate and view the compose file
Create the containers
While still in the same directory we can use the
docker-compose up command to bring both containers online.
We can see the containers running by using the process command