In this lesson, we will dive further into the different configurations of Docker containers.

Mount volumes

Your Ansible environment is ready, but you have to use nano or some other editor to make changes to the files that are local to the container. Any change made outside the container has to be replicated inside the container as well.

That’s where mounted volumes come in. Docker allows you to mount a local directory to a directory inside the container.

You can mount a directory using the --volume option. The syntax for mounting a directory is --volume {host directory}:{container directory}

Image Destroyed!
In case you are returning to this lesson later than when you built the image, there is a chance that the image is destroyed, so you will have to run the build command again before executing the run command. Run docker images to make sure the image exists.

Run the following command below in the terminal:

Press + to interact
docker run -it --rm --volume "$(pwd)":/ansible ansible

${pwd} (or ${PWD} for Windows)
The pwd command outputs the current working directory.
Windows: Use ${PWD} to mount the working directory. You also might need to enable Shared Drives in Docker Desktop. Use the following command on Windows:

Press + to interact
docker run -it --rm --volume "$(PWD)":/ansible ansible

Specifying a working directory

One small inconvenience introduced by mounting a volume is that you have to change to the /ansible directory after starting the container. This can be resolved by using the --workdir option. This will start the container in the specified directory.

Use the following command to use the --workdir option:

Press + to interact
docker run -it --rm --volume "$(pwd)":/ansible --workdir /ansible ansible

Use environment variables

Applications leverage environment variables to store values that are used for application configuration. Ansible uses them for the same purpose as well as connecting with external platforms.

Docker uses the --env option to specify the environment variables when you start the container.

Connect to Amazon Web Services

You can provide the access keys for AWS using the environment variables. Ansible will use these keys to connect with AWS. Run the following command to provide the keys:

Press + to interact
# replace the <AWS_Access_Key_ID> and <AWS_Secret_Access_Key>
# with the actual keys
docker run -it --rm --volume "$(pwd)":/ansible --workdir /ansible \
--env "AWS_ACCESS_KEY_ID='<AWS_Access_Key_ID>'" \
--env "AWS_SECRET_ACCESS_KEY='<AWS_Secret_Access_Key>'" \
ansible

Setting up keys
Don’t be alarmed if you have not set up the keys yet. We will walk through the setup process in the next chapter. For now, you can provide dummy values to these variables.

Connect to Azure

To do the same for Azure, use the following command:

Press + to interact
# replace the <Azure_Subscription_ID>, <Service_Principal_Application_ID>,
# <Service_Principal_Password>, and <Azure_Tenant>
# with the actual keys
docker run -it --rm --volume "$(pwd)":/ansible --workdir /ansible \
--env "AZURE_SUBSCRIPTION_ID=<Azure_Subscription_ID>" \
--env "AZURE_CLIENT_ID=<Service_Principal_Application_ID>" \
--env "AZURE_SECRET=<Service_Principal_Password>" \
--env "AZURE_TENANT=<Azure_Tenant>" \
ansible

Push a Docker image to DockerHub

Creating and storing an image locally is a great first step, but you’ll want to use a Container Registry for long-term storage of an image.

Container Registries
Cloud platforms and other 3rd-party vendors provide options for private registries to store sensitive Docker images. Examples are Azure Container Registry, Amazon ECR, and Jfrog Artifactory.

You will push images to DockerHub, but you need to create an account on DockerHub.

Create a DockerHub account

You can create your account here.

Once created, you will receive a verification email.

Sign into DockerHub

Before you can push an image to DockerHub, you will need to sign in. You can do that by using the Docker Desktop GUI or using the following command:

Press + to interact
# replace <DockerHub-UserName> with your actual username.
docker login --username <DockerHub-UserName>

Add a repository to image tag

Docker uses another portion of the image tag, repository name, to indicate where the image will be uploaded.

You can add the repository name to the tag when building the image. Use the following command to build the image with the repository name:

Press + to interact
docker build -t <DockerHub-UserName>/ansible:latest .

The tag comprises three parts:

  • Repository Name - <DockerHub-UserName>
  • Image Name - ansible
  • Tag Name - latest

Docker push

You can push the image by using the command docker push. You have to provide a three-part tag along with the push command. Run the following command in the terminal:

Press + to interact
docker push <DockerHub-UserName>/ansible:latest

Replace the <DockerHub-UserName> with your DockerHub username.

Congratulations! With that, you have been able to push an image to DockerHub.

Click on the RUN button and practice all the commands one by one in the terminal of the SPA widget. We have provided a summarized view of all the commands below for your reference:

FROM ubuntu:latest
  
RUN apt-get update; \
    apt install -y openssh-client; \
    apt install -y python3-pip

RUN pip3 install --upgrade pip; \
    pip3 install "ansible==2.9.12"
Variables, Volumes, and Dockerfile

In this lesson, we explored volumes, environment variables, and pushing an image to DockerHub. We looked into the following commands:

  • --volume: To mount a volume.
  • --workdir: To start a container in the specified directory.
  • --env: To provide environment variables to store values.
  • login: To login into DockerHub.
  • push: To push the image to DockerHub.

You can use the shorthand run options as well. Find the options below:

Docker Option Shorthand
--volume -v
--workdir -w
--env -e

Get hands-on with 1300+ tech skills courses.