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:

Mount a volume

${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:

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:

Specify the working directory

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:

docker run - environment variable - connect to AWS

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:

docker run - environment variable - connect to Azure

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:

docker login

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:

add a repository to the docker image tag

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:

docker push

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:

/
ansible
Dockerfile
Commands.sh
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
Build and Run a Container
Summary
Mark as Completed
Report an Issue